Std.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. var clv:cs.system.Type = untyped __cs__('v.GetType()');
  48. return clt.IsAssignableFrom(clv);
  49. }
  50. public static function string( s : Dynamic ) : String {
  51. if (s == null)
  52. return "null";
  53. if (Std.is(s, Bool))
  54. return cast(s, Bool) ? "true" : "false";
  55. return s.ToString();
  56. }
  57. public static inline function int( x : Float ) : Int {
  58. return cast x;
  59. }
  60. public static function parseInt( x : String ) : Null<Int> {
  61. if (x == null) return null;
  62. var ret = 0;
  63. var base = 10;
  64. var i = -1;
  65. var len = x.length;
  66. if (StringTools.startsWith(x, "0") && len > 2)
  67. {
  68. var c:Int = cast untyped x[1];
  69. if (c == 'x'.code || c == 'X'.code)
  70. {
  71. i = 1;
  72. base = 16;
  73. }
  74. }
  75. var foundAny = false;
  76. var isNeg = false;
  77. while (++i < len)
  78. {
  79. var c = cast(untyped x[i], Int); //fastCodeAt
  80. if (!foundAny)
  81. {
  82. switch(c)
  83. {
  84. case '-'.code:
  85. isNeg = true;
  86. continue;
  87. case ' '.code, '\t'.code, '\n'.code, '\r'.code, '+'.code:
  88. if (isNeg)
  89. return null;
  90. continue;
  91. }
  92. }
  93. if (c >= '0'.code && c <= '9'.code)
  94. {
  95. if (!foundAny && c == '0'.code)
  96. {
  97. foundAny = true;
  98. continue;
  99. }
  100. ret *= base; foundAny = true;
  101. ret += c - '0'.code;
  102. } else if (base == 16) {
  103. if (c >= 'a'.code && c <= 'f'.code) {
  104. ret *= base; foundAny = true;
  105. ret += c - 'a'.code + 10;
  106. } else if (c >= 'A'.code && c <= 'F'.code) {
  107. ret *= base; foundAny = true;
  108. ret += c - 'A'.code + 10;
  109. } else {
  110. break;
  111. }
  112. } else {
  113. break;
  114. }
  115. }
  116. if (foundAny)
  117. return isNeg ? -ret : ret;
  118. else
  119. return null;
  120. }
  121. public static function parseFloat( x : String ) : Float {
  122. if (x == null) return Math.NaN;
  123. x = StringTools.ltrim(x);
  124. var ret = 0.0;
  125. var div = 0.0;
  126. var e = 0.0;
  127. var len = x.length;
  128. var foundAny = false;
  129. var isNeg = false;
  130. var i = -1;
  131. while (++i < len)
  132. {
  133. var c = cast(untyped x[i], Int); //fastCodeAt
  134. if (!foundAny)
  135. {
  136. switch(c)
  137. {
  138. case '-'.code:
  139. isNeg = true;
  140. continue;
  141. case ' '.code, '\t'.code, '\n'.code, '\r'.code, '+'.code:
  142. if (isNeg)
  143. return Math.NaN;
  144. continue;
  145. }
  146. }
  147. if (c == '.'.code)
  148. {
  149. if (div != 0.0)
  150. break;
  151. div = 1.0;
  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. public static function instance<T>( v : { }, c : Class<T> ) : T {
  211. return Std.is(v, c) ? cast v : null;
  212. }
  213. public static function random( x : Int ) : Int {
  214. if (x <= 0) return 0;
  215. return untyped Math.rand.Next(x);
  216. }
  217. }