Std.hx 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. import java.Boot;
  26. import java.Lib;
  27. import java.internal.Exceptions;
  28. @:core_api @:nativegen class Std {
  29. public static function is( v : Dynamic, t : Dynamic ) : Bool
  30. {
  31. if (v == null)
  32. return t == Dynamic;
  33. if (t == null)
  34. return false;
  35. var clt:Class<Dynamic> = cast t;
  36. if (clt == null)
  37. return false;
  38. var name:String = untyped __java__("clt.getName()");
  39. switch(name)
  40. {
  41. case "double", "java.lang.Double":
  42. return untyped __java__('haxe.lang.Runtime.isDouble(v)');
  43. case "int", "java.lang.Integer":
  44. return untyped __java__('haxe.lang.Runtime.isInt(v)');
  45. case "boolean", "java.lang.Boolean":
  46. return untyped __java__('v instanceof java.lang.Boolean');
  47. case "java.lang.Object":
  48. return true;
  49. }
  50. var clv:Class<Dynamic> = untyped __java__('v.getClass()');
  51. return untyped clt.isAssignableFrom(clv);
  52. }
  53. public static inline function string( s : Dynamic ) : String {
  54. return cast s;
  55. }
  56. public static inline function int( x : Float ) : Int {
  57. return cast x;
  58. }
  59. @:functionBody('
  60. if (x == null) return null;
  61. int ret = 0;
  62. int base = 10;
  63. int i = 0;
  64. int len = x.length();
  65. if (x.startsWith("0") && len > 2)
  66. {
  67. char c = x.charAt(1);
  68. if (c == \'x\' || c == \'X\')
  69. {
  70. i = 2;
  71. base = 16;
  72. }
  73. }
  74. boolean foundAny = false;
  75. boolean isNeg = false;
  76. for (; i < len; i++)
  77. {
  78. char c = x.charAt(i);
  79. if (!foundAny)
  80. {
  81. switch(c)
  82. {
  83. case \'-\':
  84. isNeg = true;
  85. continue;
  86. case \'\\n\':
  87. case \'\\t\':
  88. case \'\\r\':
  89. case \' \':
  90. if (isNeg) return null;
  91. continue;
  92. }
  93. }
  94. if (c >= \'0\' && c <= \'9\')
  95. {
  96. if (!foundAny && c == \'0\')
  97. {
  98. foundAny = true;
  99. continue;
  100. }
  101. ret *= base; foundAny = true;
  102. ret += ((int) (c - \'0\'));
  103. } else if (base == 16) {
  104. if (c >= \'a\' && c <= \'f\') {
  105. ret *= base; foundAny = true;
  106. ret += ((int) (c - \'a\')) + 10;
  107. } else if (c >= \'A\' && c <= \'F\') {
  108. ret *= base; foundAny = true;
  109. ret += ((int) (c - \'A\')) + 10;
  110. } else {
  111. break;
  112. }
  113. } else {
  114. break;
  115. }
  116. }
  117. if (foundAny)
  118. return isNeg ? -ret : ret;
  119. else
  120. return null;
  121. ')
  122. public static function parseInt( x : String ) : Null<Int> {
  123. return null;
  124. }
  125. @:functionBody('
  126. if (x == null) return java.lang.Double.NaN;
  127. x = x.trim();
  128. double ret = 0.0;
  129. double div = 0.0;
  130. double e = 0.0;
  131. int len = x.length();
  132. boolean foundAny = false;
  133. boolean isNeg = false;
  134. for (int i = 0; i < len; i++)
  135. {
  136. char c = x.charAt(i);
  137. if (!foundAny)
  138. {
  139. switch(c)
  140. {
  141. case \'-\':
  142. isNeg = true;
  143. continue;
  144. case \'\\n\':
  145. case \'\\t\':
  146. case \'\\r\':
  147. case \' \':
  148. if (isNeg) return java.lang.Double.NaN;
  149. continue;
  150. }
  151. }
  152. if (c == \'.\') {
  153. if (div != 0.0)
  154. break;
  155. div = 1.0;
  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. public static function random( x : Int ) : Int {
  218. if (x <= 0) return 0;
  219. return Std.int(Math.random() * x);
  220. }
  221. @:macro public static function format( fmt : haxe.macro.Expr.ExprRequire<String> ) : haxe.macro.Expr.ExprRequire<String> {
  222. return haxe.macro.Format.format(fmt);
  223. }
  224. }