Std.hx 4.9 KB

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