Std.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Copyright (C)2005-2012 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 inline 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 = false;
  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 \'\\n\':
  84. case \'\\t\':
  85. case \'\\r\':
  86. case \' \':
  87. if (isNeg) return null;
  88. continue;
  89. }
  90. }
  91. if (c >= \'0\' && c <= \'9\')
  92. {
  93. if (!foundAny && c == \'0\')
  94. {
  95. foundAny = true;
  96. continue;
  97. }
  98. ret *= base; foundAny = true;
  99. ret += ((int) (c - \'0\'));
  100. } else if (base == 16) {
  101. if (c >= \'a\' && c <= \'f\') {
  102. ret *= base; foundAny = true;
  103. ret += ((int) (c - \'a\')) + 10;
  104. } else if (c >= \'A\' && c <= \'F\') {
  105. ret *= base; foundAny = true;
  106. ret += ((int) (c - \'A\')) + 10;
  107. } else {
  108. break;
  109. }
  110. } else {
  111. break;
  112. }
  113. }
  114. if (foundAny)
  115. return isNeg ? -ret : ret;
  116. else
  117. return null;
  118. ')
  119. public static function parseInt( x : String ) : Null<Int> {
  120. return null;
  121. }
  122. @:functionCode('
  123. if (x == null) return java.lang.Double.NaN;
  124. x = x.trim();
  125. double ret = 0.0;
  126. double div = 0.0;
  127. double e = 0.0;
  128. int len = x.length();
  129. boolean foundAny = false;
  130. boolean isNeg = false;
  131. for (int i = 0; i < len; i++)
  132. {
  133. char c = x.charAt(i);
  134. if (!foundAny)
  135. {
  136. switch(c)
  137. {
  138. case \'-\':
  139. isNeg = true;
  140. continue;
  141. case \'\\n\':
  142. case \'\\t\':
  143. case \'\\r\':
  144. case \' \':
  145. if (isNeg) return java.lang.Double.NaN;
  146. continue;
  147. }
  148. }
  149. if (c == \'.\') {
  150. if (div != 0.0)
  151. break;
  152. div = 1.0;
  153. continue;
  154. }
  155. if (c >= \'0\' && c <= \'9\')
  156. {
  157. if (!foundAny && c == \'0\')
  158. {
  159. foundAny = true;
  160. continue;
  161. }
  162. ret *= 10.0; foundAny = true; div *= 10.0;
  163. ret += ((int) (c - \'0\'));
  164. } else if (foundAny && c == \'E\' || c == \'e\') {
  165. boolean eNeg = false;
  166. boolean eFoundAny = false;
  167. char next = x.charAt(i + 1);
  168. if (i + 1 < len)
  169. {
  170. if (next == \'-\')
  171. {
  172. eNeg = true;
  173. i++;
  174. } else if (next == \'+\') {
  175. i++;
  176. }
  177. }
  178. while (++i < len)
  179. {
  180. c = x.charAt(i);
  181. if (c >= \'0\' && c <= \'9\')
  182. {
  183. if (!eFoundAny && c == \'0\')
  184. continue;
  185. eFoundAny = true;
  186. e *= 10.0;
  187. e += ((int) (c - \'0\'));
  188. } else {
  189. break;
  190. }
  191. }
  192. if (eNeg) e = -e;
  193. } else {
  194. break;
  195. }
  196. }
  197. if (div == 0.0) div = 1.0;
  198. if (foundAny)
  199. {
  200. ret = isNeg ? -(ret / div) : (ret / div);
  201. if (e != 0.0)
  202. {
  203. return ret * Math.pow(10.0, e);
  204. } else {
  205. return ret;
  206. }
  207. } else {
  208. return java.lang.Double.NaN;
  209. }
  210. ')
  211. public static function parseFloat( x : String ) : Float {
  212. return 0.0;
  213. }
  214. public static function random( x : Int ) : Int {
  215. if (x <= 0) return 0;
  216. return Std.int(Math.random() * x);
  217. }
  218. }