Std.hx 4.6 KB

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