Std.hx 5.2 KB

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