Std.hx 4.9 KB

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