2
0

Std.hx 5.0 KB

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