Std.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. import jvm.Jvm;
  23. using StringTools;
  24. @:coreApi
  25. class Std {
  26. @:deprecated('Std.is is deprecated. Use Std.isOfType instead.')
  27. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  28. return isOfType(v, t);
  29. }
  30. public static function isOfType(v:Dynamic, t:Dynamic):Bool {
  31. if (v == null || t == null) {
  32. return false;
  33. }
  34. var clt:java.lang.Class<Dynamic> = cast t;
  35. if (clt == null) {
  36. return false;
  37. }
  38. if (clt == cast java.lang.Object) {
  39. return true;
  40. }
  41. clt = Jvm.getWrapperClass(clt);
  42. if (clt == cast java.lang.Double.DoubleClass) {
  43. // Haxe semantics: any number is assignable to Float
  44. clt = cast java.lang.Number;
  45. } else if (clt == cast java.lang.Integer) {
  46. if (!Jvm.instanceof(v, java.lang.Number)) {
  47. return false;
  48. }
  49. var n = (cast v : java.lang.Number);
  50. // Haxe semantics: 2.0 is an integer...
  51. return n.doubleValue() == n.intValue();
  52. }
  53. return clt.isAssignableFrom((v : java.lang.Object).getClass());
  54. }
  55. public static function string(s:Dynamic):String {
  56. return jvm.Jvm.toString(s);
  57. }
  58. public static function int(x:Float):Int {
  59. return cast x;
  60. }
  61. static inline function isSpaceChar(code:Int):Bool
  62. return (code > 8 && code < 14) || code == 32;
  63. static inline function isHexPrefix(cur:Int, next:Int):Bool
  64. return cur == '0'.code && (next == 'x'.code || next == 'X'.code);
  65. static inline function isDecimalDigit(code:Int):Bool
  66. return '0'.code <= code && code <= '9'.code;
  67. static inline function isHexadecimalDigit(code:Int):Bool
  68. return isDecimalDigit(code) || ('a'.code <= code && code <= 'f'.code) || ('A'.code <= code && code <= 'F'.code);
  69. public static function parseInt(x:String):Null<Int> {
  70. if (x == null)
  71. return null;
  72. final len = x.length;
  73. var index = 0;
  74. inline function hasIndex(index:Int)
  75. return index < len;
  76. // skip whitespace
  77. while (hasIndex(index)) {
  78. if (!isSpaceChar(x.unsafeCodeAt(index)))
  79. break;
  80. ++index;
  81. }
  82. // handle sign
  83. final isNegative = hasIndex(index) && {
  84. final sign = x.unsafeCodeAt(index);
  85. if (sign == '-'.code || sign == '+'.code) {
  86. ++index;
  87. }
  88. sign == '-'.code;
  89. } // handle base
  90. final isHexadecimal = hasIndex(index + 1) && isHexPrefix(x.unsafeCodeAt(index), x.unsafeCodeAt(index + 1));
  91. if (isHexadecimal)
  92. index += 2; // skip prefix
  93. // handle digits
  94. final firstInvalidIndex = {
  95. var cur = index;
  96. if (isHexadecimal) {
  97. while (hasIndex(cur)) {
  98. if (!isHexadecimalDigit(x.unsafeCodeAt(cur)))
  99. break;
  100. ++cur;
  101. }
  102. } else {
  103. while (hasIndex(cur)) {
  104. if (!isDecimalDigit(x.unsafeCodeAt(cur)))
  105. break;
  106. ++cur;
  107. }
  108. }
  109. cur;
  110. }
  111. // no valid digits
  112. if (index == firstInvalidIndex)
  113. return null;
  114. final result = java.lang.Integer.parseInt(x.substring(index, firstInvalidIndex), if (isHexadecimal) 16 else 10);
  115. return if (isNegative) -result else result;
  116. }
  117. public static function parseFloat(x:String):Float {
  118. if (x == null) {
  119. return Math.NaN;
  120. }
  121. x = StringTools.ltrim(x);
  122. var xn:java.lang.String = cast x;
  123. var found = false, hasDot = false, hasSign = false, hasE = false, hasESign = false, hasEData = false;
  124. var i = -1;
  125. while (++i < x.length) {
  126. var chr:Int = cast xn.charAt(i);
  127. if (chr >= '0'.code && chr <= '9'.code) {
  128. if (hasE) {
  129. hasEData = true;
  130. }
  131. found = true;
  132. } else
  133. switch (chr) {
  134. case 'e'.code | 'E'.code if (!hasE):
  135. hasE = true;
  136. case '.'.code if (!hasDot):
  137. hasDot = true;
  138. case '-'.code, '+'.code if (!found && !hasSign):
  139. hasSign = true;
  140. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  141. hasESign = true;
  142. case _:
  143. break;
  144. }
  145. }
  146. if (hasE && !hasEData) {
  147. i--;
  148. if (hasESign)
  149. i--;
  150. }
  151. if (i != x.length) {
  152. x = x.substr(0, i);
  153. }
  154. return try java.lang.Double.DoubleClass.parseDouble(x) catch (e:Dynamic) Math.NaN;
  155. }
  156. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  157. return Std.isOfType(value, c) ? cast value : null;
  158. }
  159. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  160. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  161. return downcast(value, c);
  162. }
  163. public static function random(x:Int):Int {
  164. if (x <= 0) {
  165. return 0;
  166. }
  167. return Std.int(Math.random() * x);
  168. }
  169. }