2
0

Std.hx 4.9 KB

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