Std.hx 5.6 KB

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