Std.hx 5.0 KB

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