Std.hx 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 haxe.lang.Exceptions;
  28. @:core_api @:nativegen class Std {
  29. public static function is( v : Dynamic, t : Dynamic ) : Bool
  30. {
  31. if (v == null)
  32. return v == t;
  33. var clt:Class<Dynamic> = cast t;
  34. if (clt == null)
  35. return false;
  36. var name = untyped __java__("clt.getName()");
  37. switch(name)
  38. {
  39. case "double", "java.lang.Double":
  40. return untyped __java__('haxe.lang.Runtime.isDouble(v)');
  41. case "int", "java.lang.Integer":
  42. return untyped __java__('haxe.lang.Runtime.isInt(v)');
  43. case "boolean", "java.lang.Boolean":
  44. return untyped __java__('v instanceof java.lang.Boolean');
  45. }
  46. var clv:Class<Dynamic> = untyped __java__('v.getClass()');
  47. return untyped clt.isAssignableFrom(clv);
  48. }
  49. public static inline function string( s : Dynamic ) : String {
  50. return cast s;
  51. }
  52. public static inline function int( x : Float ) : Int {
  53. return cast x;
  54. }
  55. @:functionBody('
  56. if (x == null) return null;
  57. int ret = 0;
  58. int base = 10;
  59. if (x.startsWith("0x"))
  60. {
  61. x = x.substring(2);
  62. base = 16;
  63. }
  64. int len = x.length();
  65. boolean foundAny = false;
  66. boolean isNeg = false;
  67. for (int i = 0; i < len; i++)
  68. {
  69. char c = x.charAt(i);
  70. if (!foundAny)
  71. {
  72. switch(c)
  73. {
  74. case \'-\':
  75. isNeg = true;
  76. continue;
  77. case \'\\n\':
  78. case \'\\t\':
  79. case \'\\r\':
  80. case \' \':
  81. if (isNeg) return null;
  82. continue;
  83. }
  84. }
  85. if (c >= \'0\' && c <= \'9\')
  86. {
  87. if (!foundAny && c == \'0\')
  88. {
  89. foundAny = true;
  90. continue;
  91. }
  92. ret *= base; foundAny = true;
  93. ret += ((int) (c - \'0\'));
  94. } else if (base == 16) {
  95. if (c >= \'a\' && c <= \'f\') {
  96. ret *= base; foundAny = true;
  97. ret += ((int) (c - \'a\')) + 10;
  98. } else if (c >= \'A\' && c <= \'F\') {
  99. ret *= base; foundAny = true;
  100. ret += ((int) (c - \'A\')) + 10;
  101. } else {
  102. break;
  103. }
  104. } else {
  105. break;
  106. }
  107. }
  108. if (foundAny)
  109. return isNeg ? -ret : ret;
  110. else
  111. return null;
  112. ')
  113. public static function parseInt( x : String ) : Null<Int> {
  114. return null;
  115. }
  116. @:functionBody('
  117. if (x == null) return java.lang.Double.NaN;
  118. x = x.trim();
  119. double ret = 0.0;
  120. double div = 0.0;
  121. double e = 0.0;
  122. int len = x.length();
  123. boolean foundAny = false;
  124. boolean isNeg = false;
  125. for (int i = 0; i < len; i++)
  126. {
  127. char c = x.charAt(i);
  128. if (!foundAny)
  129. {
  130. switch(c)
  131. {
  132. case \'-\':
  133. isNeg = true;
  134. continue;
  135. case \'\\n\':
  136. case \'\\t\':
  137. case \'\\r\':
  138. case \' \':
  139. if (isNeg) return java.lang.Double.NaN;
  140. continue;
  141. }
  142. }
  143. if (c == \'.\') {
  144. if (div != 0.0)
  145. break;
  146. div = 1.0;
  147. continue;
  148. }
  149. if (c >= \'0\' && c <= \'9\')
  150. {
  151. if (!foundAny && c == \'0\')
  152. {
  153. foundAny = true;
  154. continue;
  155. }
  156. ret *= 10.0; foundAny = true; div *= 10.0;
  157. ret += ((int) (c - \'0\'));
  158. } else if (foundAny && c == \'E\' || c == \'e\') {
  159. boolean eNeg = false;
  160. boolean eFoundAny = false;
  161. char next = x.charAt(i + 1);
  162. if (i + 1 < len)
  163. {
  164. if (next == \'-\')
  165. {
  166. eNeg = true;
  167. i++;
  168. } else if (next == \'+\') {
  169. i++;
  170. }
  171. }
  172. while (++i < len)
  173. {
  174. c = x.charAt(i);
  175. if (c >= \'0\' && c <= \'9\')
  176. {
  177. if (!eFoundAny && c == \'0\')
  178. continue;
  179. eFoundAny = true;
  180. e *= 10.0;
  181. e += ((int) (c - \'0\'));
  182. } else {
  183. break;
  184. }
  185. }
  186. if (eNeg) e = -e;
  187. } else {
  188. break;
  189. }
  190. }
  191. if (div == 0.0) div = 1.0;
  192. if (foundAny)
  193. {
  194. ret = isNeg ? -(ret / div) : (ret / div);
  195. if (e != 0.0)
  196. {
  197. return ret * Math.pow(10.0, e);
  198. } else {
  199. return ret;
  200. }
  201. } else {
  202. return java.lang.Double.NaN;
  203. }
  204. ')
  205. public static function parseFloat( x : String ) : Float {
  206. return 0.0;
  207. }
  208. public static function random( x : Int ) : Int {
  209. return Std.int(Math.random() * x);
  210. }
  211. @:macro public static function format( fmt : haxe.macro.Expr.ExprRequire<String> ) : haxe.macro.Expr.ExprRequire<String> {
  212. return haxe.macro.Format.format(fmt);
  213. }
  214. }