Std.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. @:functionCode('
  55. if (x == null) return null;
  56. int ret = 0;
  57. int base = 10;
  58. int i = 0;
  59. int len = x.length();
  60. if (x.startsWith("0") && len > 2)
  61. {
  62. char c = x.charAt(1);
  63. if (c == \'x\' || c == \'X\')
  64. {
  65. i = 2;
  66. base = 16;
  67. }
  68. }
  69. boolean foundAny = i != 0;
  70. boolean isNeg = false;
  71. for (; i < len; i++)
  72. {
  73. char c = x.charAt(i);
  74. if (!foundAny)
  75. {
  76. switch(c)
  77. {
  78. case \'-\':
  79. isNeg = true;
  80. continue;
  81. case \'+\':
  82. case \'\\n\':
  83. case \'\\t\':
  84. case \'\\r\':
  85. case \' \':
  86. if (isNeg) return null;
  87. continue;
  88. }
  89. }
  90. if (c >= \'0\' && c <= \'9\')
  91. {
  92. if (!foundAny && c == \'0\')
  93. {
  94. foundAny = true;
  95. continue;
  96. }
  97. ret *= base; foundAny = true;
  98. ret += ((int) (c - \'0\'));
  99. } else if (base == 16) {
  100. if (c >= \'a\' && c <= \'f\') {
  101. ret *= base; foundAny = true;
  102. ret += ((int) (c - \'a\')) + 10;
  103. } else if (c >= \'A\' && c <= \'F\') {
  104. ret *= base; foundAny = true;
  105. ret += ((int) (c - \'A\')) + 10;
  106. } else {
  107. break;
  108. }
  109. } else {
  110. break;
  111. }
  112. }
  113. if (foundAny)
  114. return isNeg ? -ret : ret;
  115. else
  116. return null;
  117. ')
  118. public static function parseInt(x:String):Null<Int> {
  119. return null;
  120. }
  121. public static function parseFloat(x:String):Float {
  122. if (x == null)
  123. return Math.NaN;
  124. x = StringTools.ltrim(x);
  125. var found = false,
  126. hasDot = false,
  127. hasSign = false,
  128. hasE = false,
  129. hasESign = false,
  130. hasEData = false;
  131. var i = -1;
  132. inline function getch(i:Int):Int
  133. return cast(untyped x._charAt(i) : java.StdTypes.Char16);
  134. while (++i < x.length) {
  135. var chr = getch(i);
  136. if (chr >= '0'.code && chr <= '9'.code) {
  137. if (hasE) {
  138. hasEData = true;
  139. }
  140. found = true;
  141. } else
  142. switch (chr) {
  143. case 'e'.code | 'E'.code if (!hasE):
  144. hasE = true;
  145. case '.'.code if (!hasDot):
  146. hasDot = true;
  147. case '-'.code, '+'.code if (!found && !hasSign):
  148. hasSign = true;
  149. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  150. hasESign = true;
  151. case _:
  152. break;
  153. }
  154. }
  155. if (hasE && !hasEData) {
  156. i--;
  157. if (hasESign)
  158. i--;
  159. }
  160. if (i != x.length) {
  161. x = x.substr(0, i);
  162. }
  163. return try java.lang.Double.DoubleClass.parseDouble(x) catch (e:Dynamic) Math.NaN;
  164. }
  165. inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  166. return Std.is(value, c) ? cast value : null;
  167. }
  168. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  169. inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  170. return downcast(value, c);
  171. }
  172. public static function random(x:Int):Int {
  173. if (x <= 0)
  174. return 0;
  175. return Std.int(Math.random() * x);
  176. }
  177. }