Std.hx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 \'+\':
  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. @:functionCode('
  124. if (x == null) return java.lang.Double.NaN;
  125. x = x.trim();
  126. double ret = 0.0;
  127. double div = 0.0;
  128. double e = 0.0;
  129. int len = x.length();
  130. boolean foundAny = false;
  131. boolean isNeg = false;
  132. for (int i = 0; i < len; i++)
  133. {
  134. char c = x.charAt(i);
  135. if (!foundAny)
  136. {
  137. switch(c)
  138. {
  139. case \'-\':
  140. isNeg = true;
  141. continue;
  142. case \'+\':
  143. case \'\\n\':
  144. case \'\\t\':
  145. case \'\\r\':
  146. case \' \':
  147. if (isNeg) return java.lang.Double.NaN;
  148. continue;
  149. }
  150. }
  151. if (c == \'.\') {
  152. if (div != 0.0)
  153. break;
  154. div = 1.0;
  155. foundAny = true;
  156. continue;
  157. }
  158. if (c >= \'0\' && c <= \'9\')
  159. {
  160. if (!foundAny && c == \'0\')
  161. {
  162. foundAny = true;
  163. continue;
  164. }
  165. ret *= 10.0; foundAny = true; div *= 10.0;
  166. ret += ((int) (c - \'0\'));
  167. } else if (foundAny && c == \'E\' || c == \'e\') {
  168. boolean eNeg = false;
  169. boolean eFoundAny = false;
  170. char next = x.charAt(i + 1);
  171. if (i + 1 < len)
  172. {
  173. if (next == \'-\')
  174. {
  175. eNeg = true;
  176. i++;
  177. } else if (next == \'+\') {
  178. i++;
  179. }
  180. }
  181. while (++i < len)
  182. {
  183. c = x.charAt(i);
  184. if (c >= \'0\' && c <= \'9\')
  185. {
  186. if (!eFoundAny && c == \'0\')
  187. continue;
  188. eFoundAny = true;
  189. e *= 10.0;
  190. e += ((int) (c - \'0\'));
  191. } else {
  192. break;
  193. }
  194. }
  195. if (eNeg) e = -e;
  196. } else {
  197. break;
  198. }
  199. }
  200. if (div == 0.0) div = 1.0;
  201. if (foundAny)
  202. {
  203. ret = isNeg ? -(ret / div) : (ret / div);
  204. if (e != 0.0)
  205. {
  206. return ret * Math.pow(10.0, e);
  207. } else {
  208. return ret;
  209. }
  210. } else {
  211. return java.lang.Double.NaN;
  212. }
  213. ')
  214. public static function parseFloat( x : String ) : Float {
  215. return 0.0;
  216. }
  217. inline public static function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  218. return Std.is(value, c) ? cast value : null;
  219. }
  220. public static function random( x : Int ) : Int {
  221. if (x <= 0) return 0;
  222. return Std.int(Math.random() * x);
  223. }
  224. }