Std.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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 java.Boot;
  23. import java.Lib;
  24. using StringTools;
  25. @:coreApi @:nativeGen 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)
  32. return false;
  33. if (t == null)
  34. return false;
  35. var clt:java.lang.Class<Dynamic> = cast t;
  36. if (clt == null)
  37. return false;
  38. var name:String = clt.getName();
  39. switch (name) {
  40. case "double", "java.lang.Double":
  41. return untyped __java__('haxe.lang.Runtime.isDouble(v)');
  42. case "int", "java.lang.Integer":
  43. return untyped __java__('haxe.lang.Runtime.isInt(v)');
  44. case "boolean", "java.lang.Boolean":
  45. return untyped __java__('v instanceof java.lang.Boolean');
  46. case "java.lang.Object":
  47. return true;
  48. }
  49. var clv:java.lang.Class<Dynamic> = untyped __java__('v.getClass()');
  50. return clt.isAssignableFrom(clv);
  51. }
  52. public static function string(s:Dynamic):String {
  53. return cast(s, String) + "";
  54. }
  55. public static function int(x:Float):Int {
  56. return cast x;
  57. }
  58. static inline function isSpaceChar(code:Int):Bool
  59. return (code > 8 && code < 14) || code == 32;
  60. static inline function isHexPrefix(cur:Int, next:Int):Bool
  61. return cur == '0'.code && (next == 'x'.code || next == 'X'.code);
  62. static inline function isDecimalDigit(code:Int):Bool
  63. return '0'.code <= code && code <= '9'.code;
  64. static inline function isHexadecimalDigit(code:Int):Bool
  65. return isDecimalDigit(code) || ('a'.code <= code && code <= 'f'.code) || ('A'.code <= code && code <= 'F'.code);
  66. public static function parseInt(x:String):Null<Int> {
  67. if (x == null)
  68. return null;
  69. final len = x.length;
  70. var index = 0;
  71. inline function hasIndex(index:Int)
  72. return index < len;
  73. // skip whitespace
  74. while (hasIndex(index)) {
  75. if (!isSpaceChar(x.unsafeCodeAt(index)))
  76. break;
  77. ++index;
  78. }
  79. // handle sign
  80. final isNegative = hasIndex(index) && {
  81. final sign = x.unsafeCodeAt(index);
  82. if (sign == '-'.code || sign == '+'.code) {
  83. ++index;
  84. }
  85. sign == '-'.code;
  86. }
  87. // handle base
  88. final isHexadecimal = hasIndex(index + 1) && isHexPrefix(x.unsafeCodeAt(index), x.unsafeCodeAt(index + 1));
  89. if (isHexadecimal)
  90. index += 2; // skip prefix
  91. // handle digits
  92. final firstInvalidIndex = {
  93. var cur = index;
  94. if (isHexadecimal) {
  95. while (hasIndex(cur)) {
  96. if (!isHexadecimalDigit(x.unsafeCodeAt(cur)))
  97. break;
  98. ++cur;
  99. }
  100. } else {
  101. while (hasIndex(cur)) {
  102. if (!isDecimalDigit(x.unsafeCodeAt(cur)))
  103. break;
  104. ++cur;
  105. }
  106. }
  107. cur;
  108. }
  109. // no valid digits
  110. if (index == firstInvalidIndex)
  111. return null;
  112. final result = java.lang.Integer.parseInt(x.substring(index, firstInvalidIndex), if (isHexadecimal) 16 else 10);
  113. return if (isNegative) -result else result;
  114. }
  115. public static function parseFloat(x:String):Float {
  116. if (x == null)
  117. return Math.NaN;
  118. x = StringTools.ltrim(x);
  119. var found = false,
  120. hasDot = false,
  121. hasSign = false,
  122. hasE = false,
  123. hasESign = false,
  124. hasEData = false;
  125. var i = -1;
  126. inline function getch(i:Int):Int
  127. return cast(untyped x._charAt(i) : java.StdTypes.Char16);
  128. while (++i < x.length) {
  129. var chr = getch(i);
  130. if (chr >= '0'.code && chr <= '9'.code) {
  131. if (hasE) {
  132. hasEData = true;
  133. }
  134. found = true;
  135. } else
  136. switch (chr) {
  137. case 'e'.code | 'E'.code if (!hasE):
  138. hasE = true;
  139. case '.'.code if (!hasDot):
  140. hasDot = true;
  141. case '-'.code, '+'.code if (!found && !hasSign):
  142. hasSign = true;
  143. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  144. hasESign = true;
  145. case _:
  146. break;
  147. }
  148. }
  149. if (hasE && !hasEData) {
  150. i--;
  151. if (hasESign)
  152. i--;
  153. }
  154. if (i != x.length) {
  155. x = x.substr(0, i);
  156. }
  157. return try java.lang.Double.DoubleClass.parseDouble(x) catch (e:Dynamic) Math.NaN;
  158. }
  159. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  160. return Std.isOfType(value, c) ? cast value : null;
  161. }
  162. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  163. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  164. return downcast(value, c);
  165. }
  166. public static function random(x:Int):Int {
  167. if (x <= 0)
  168. return 0;
  169. return Std.int(Math.random() * x);
  170. }
  171. }