Std.hx 4.9 KB

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