Std.hx 5.5 KB

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