Std.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (C)2005-2017 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.Lib.as(t, cs.system.Type);
  33. if (clt == null)
  34. return false;
  35. switch(clt.ToString())
  36. {
  37. case "System.Double":
  38. return untyped __cs__('{0} is double || {0} is int', v);
  39. case "System.Int32":
  40. return cs.internal.Runtime.isInt(v);
  41. case "System.Boolean":
  42. return untyped __cs__('{0} is bool', v);
  43. case "System.Object":
  44. return true;
  45. }
  46. var vt = cs.Lib.getNativeType(v);
  47. if (clt.IsAssignableFrom(vt))
  48. return true;
  49. #if !erase_generics
  50. for (iface in clt.GetInterfaces()) {
  51. var g = cs.internal.Runtime.getGenericAttr(iface);
  52. if (g != null && g.generic == clt) {
  53. return iface.IsAssignableFrom(vt);
  54. }
  55. }
  56. #end
  57. return false;
  58. }
  59. public static function string( s : Dynamic ) : String {
  60. if (s == null)
  61. return "null";
  62. if (Std.is(s, Bool))
  63. return cast(s, Bool) ? "true" : "false";
  64. return s.ToString();
  65. }
  66. public static function int( x : Float ) : Int {
  67. return cast x;
  68. }
  69. public static function parseInt( x : String ) : Null<Int> {
  70. if (x == null) return null;
  71. var ret = 0;
  72. var base = 10;
  73. var i = -1;
  74. var len = x.length;
  75. if (StringTools.startsWith(x, "0") && len > 2)
  76. {
  77. var c:Int = cast untyped x[1];
  78. if (c == 'x'.code || c == 'X'.code)
  79. {
  80. i = 1;
  81. base = 16;
  82. }
  83. }
  84. var foundAny = i != -1;
  85. var isNeg = false;
  86. while (++i < len)
  87. {
  88. var c = cast(untyped x[i], Int); //fastCodeAt
  89. if (!foundAny)
  90. {
  91. switch(c)
  92. {
  93. case '-'.code:
  94. isNeg = true;
  95. continue;
  96. case ' '.code, '\t'.code, '\n'.code, '\r'.code, '+'.code:
  97. if (isNeg)
  98. return null;
  99. continue;
  100. }
  101. }
  102. if (c >= '0'.code && c <= '9'.code)
  103. {
  104. if (!foundAny && c == '0'.code)
  105. {
  106. foundAny = true;
  107. continue;
  108. }
  109. ret *= base; foundAny = true;
  110. ret += c - '0'.code;
  111. } else if (base == 16) {
  112. if (c >= 'a'.code && c <= 'f'.code) {
  113. ret *= base; foundAny = true;
  114. ret += c - 'a'.code + 10;
  115. } else if (c >= 'A'.code && c <= 'F'.code) {
  116. ret *= base; foundAny = true;
  117. ret += c - 'A'.code + 10;
  118. } else {
  119. break;
  120. }
  121. } else {
  122. break;
  123. }
  124. }
  125. if (foundAny)
  126. return isNeg ? -ret : ret;
  127. else
  128. return null;
  129. }
  130. public static function parseFloat( x : String ) : Float {
  131. if (x == null) return Math.NaN;
  132. x = StringTools.ltrim(x);
  133. var found = false, hasDot = false, hasSign = false,
  134. hasE = false, hasESign = false, hasEData = false;
  135. var i = -1;
  136. inline function getch(i:Int):Int return cast ((untyped x : cs.system.String)[i]);
  137. while (++i < x.length)
  138. {
  139. var chr = getch(i);
  140. if (chr >= '0'.code && chr <= '9'.code)
  141. {
  142. if (hasE)
  143. {
  144. hasEData = true;
  145. }
  146. found = true;
  147. } else switch (chr) {
  148. case 'e'.code | 'E'.code if(!hasE):
  149. hasE = true;
  150. case '.'.code if (!hasDot):
  151. hasDot = true;
  152. case '-'.code, '+'.code if (!found && !hasSign):
  153. hasSign = true;
  154. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  155. hasESign = true;
  156. case _:
  157. break;
  158. }
  159. }
  160. if (hasE && !hasEData)
  161. {
  162. i--;
  163. if (hasESign)
  164. i--;
  165. }
  166. if (i != x.length)
  167. {
  168. x = x.substr(0,i);
  169. }
  170. return try
  171. cs.system.Double.Parse(x, cs.system.globalization.CultureInfo.InvariantCulture)
  172. catch(e:Dynamic)
  173. Math.NaN;
  174. }
  175. @:extern inline public static function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  176. return cs.Lib.as(value,c);
  177. }
  178. public static function random( x : Int ) : Int {
  179. if (x <= 0) return 0;
  180. return untyped Math.rand.Next(x);
  181. }
  182. }