Std.hx 5.2 KB

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