Std.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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.IntegerClass) {
  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. }
  90. // handle base
  91. final isHexadecimal = hasIndex(index + 1) && isHexPrefix(x.unsafeCodeAt(index), x.unsafeCodeAt(index + 1));
  92. if (isHexadecimal)
  93. index += 2; // skip prefix
  94. // handle digits
  95. final firstInvalidIndex = {
  96. var cur = index;
  97. if (isHexadecimal) {
  98. while (hasIndex(cur)) {
  99. if (!isHexadecimalDigit(x.unsafeCodeAt(cur)))
  100. break;
  101. ++cur;
  102. }
  103. } else {
  104. while (hasIndex(cur)) {
  105. if (!isDecimalDigit(x.unsafeCodeAt(cur)))
  106. break;
  107. ++cur;
  108. }
  109. }
  110. cur;
  111. }
  112. // no valid digits
  113. if (index == firstInvalidIndex)
  114. return null;
  115. final result = java.lang.Integer.parseInt(x.substring(index, firstInvalidIndex), if (isHexadecimal) 16 else 10);
  116. return if (isNegative) -result else result;
  117. }
  118. public static function parseFloat(x:String):Float {
  119. if (x == null) {
  120. return Math.NaN;
  121. }
  122. x = StringTools.ltrim(x);
  123. var xn:java.NativeString = cast x;
  124. var found = false,
  125. hasDot = false,
  126. hasSign = false,
  127. hasE = false,
  128. hasESign = false,
  129. hasEData = false;
  130. var i = -1;
  131. while (++i < x.length) {
  132. var chr:Int = cast xn.charAt(i);
  133. if (chr >= '0'.code && chr <= '9'.code) {
  134. if (hasE) {
  135. hasEData = true;
  136. }
  137. found = true;
  138. } else
  139. switch (chr) {
  140. case 'e'.code | 'E'.code if (!hasE):
  141. hasE = true;
  142. case '.'.code if (!hasDot):
  143. hasDot = true;
  144. case '-'.code, '+'.code if (!found && !hasSign):
  145. hasSign = true;
  146. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  147. hasESign = true;
  148. case _:
  149. break;
  150. }
  151. }
  152. if (hasE && !hasEData) {
  153. i--;
  154. if (hasESign)
  155. i--;
  156. }
  157. if (i != x.length) {
  158. x = x.substr(0, i);
  159. }
  160. return try java.lang.Double.DoubleClass.parseDouble(x) catch (e:Dynamic) Math.NaN;
  161. }
  162. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  163. return Std.isOfType(value, c) ? cast value : null;
  164. }
  165. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  166. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  167. return downcast(value, c);
  168. }
  169. public static function random(x:Int):Int {
  170. if (x <= 0) {
  171. return 0;
  172. }
  173. return Std.int(Math.random() * x);
  174. }
  175. }