Std.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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 ret = 0;
  58. var base = 10;
  59. var len = x.length;
  60. var foundCount = 0;
  61. var sign = 0;
  62. var firstDigitIndex = 0;
  63. var lastDigitIndex = -1;
  64. var previous = 0;
  65. for(i in 0...len) {
  66. var c = StringTools.fastCodeAt(x, i);
  67. switch c {
  68. case _ if((c > 8 && c < 14) || c == 32):
  69. if(foundCount > 0) {
  70. return null;
  71. }
  72. continue;
  73. case '-'.code if(foundCount == 0):
  74. sign = -1;
  75. case '+'.code if(foundCount == 0):
  76. sign = 1;
  77. case '0'.code if(foundCount == 0 || (foundCount == 1 && sign != 0)):
  78. case 'x'.code | 'X'.code if(previous == '0'.code && ((foundCount == 1 && sign == 0) || (foundCount == 2 && sign != 0))):
  79. base = 16;
  80. case _ if('0'.code <= c && c <= '9'.code):
  81. case _ if(base == 16 && (('a'.code <= c && c <= 'z'.code) || ('A'.code <= c && c <= 'Z'.code))):
  82. case _:
  83. break;
  84. }
  85. if((foundCount == 0 && sign == 0) || (foundCount == 1 && sign != 0)) {
  86. firstDigitIndex = i;
  87. }
  88. foundCount++;
  89. lastDigitIndex = i;
  90. previous = c;
  91. }
  92. if(firstDigitIndex <= lastDigitIndex) {
  93. var digits = x.substring(firstDigitIndex + (base == 16 ? 2 : 0), lastDigitIndex + 1);
  94. return try {
  95. (sign == -1 ? -1 : 1) * java.lang.Integer.parseInt(digits, base);
  96. } catch(e:java.lang.NumberFormatException) {
  97. null;
  98. }
  99. }
  100. return null;
  101. }
  102. public static function parseFloat(x:String):Float {
  103. if (x == null)
  104. return Math.NaN;
  105. x = StringTools.ltrim(x);
  106. var found = false,
  107. hasDot = false,
  108. hasSign = false,
  109. hasE = false,
  110. hasESign = false,
  111. hasEData = false;
  112. var i = -1;
  113. inline function getch(i:Int):Int
  114. return cast(untyped x._charAt(i) : java.StdTypes.Char16);
  115. while (++i < x.length) {
  116. var chr = getch(i);
  117. if (chr >= '0'.code && chr <= '9'.code) {
  118. if (hasE) {
  119. hasEData = true;
  120. }
  121. found = true;
  122. } else
  123. switch (chr) {
  124. case 'e'.code | 'E'.code if (!hasE):
  125. hasE = true;
  126. case '.'.code if (!hasDot):
  127. hasDot = true;
  128. case '-'.code, '+'.code if (!found && !hasSign):
  129. hasSign = true;
  130. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  131. hasESign = true;
  132. case _:
  133. break;
  134. }
  135. }
  136. if (hasE && !hasEData) {
  137. i--;
  138. if (hasESign)
  139. i--;
  140. }
  141. if (i != x.length) {
  142. x = x.substr(0, i);
  143. }
  144. return try java.lang.Double.DoubleClass.parseDouble(x) catch (e:Dynamic) Math.NaN;
  145. }
  146. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  147. return Std.is(value, c) ? cast value : null;
  148. }
  149. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  150. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  151. return downcast(value, c);
  152. }
  153. public static function random(x:Int):Int {
  154. if (x <= 0)
  155. return 0;
  156. return Std.int(Math.random() * x);
  157. }
  158. }