Std.hx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  26. return isOfType(v, t);
  27. }
  28. public static function isOfType(v:Dynamic, t:Dynamic):Bool {
  29. if (v == null)
  30. return false;
  31. if (t == null)
  32. return false;
  33. var clt = cs.Lib.as(t, cs.system.Type);
  34. if (clt == null)
  35. return false;
  36. switch (clt.ToString()) {
  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.isOfType(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)
  71. return null;
  72. var base = 10;
  73. var len = x.length;
  74. var foundCount = 0;
  75. var sign = 0;
  76. var firstDigitIndex = 0;
  77. var lastDigitIndex = -1;
  78. var previous = 0;
  79. for(i in 0...len) {
  80. var c = StringTools.fastCodeAt(x, i);
  81. switch c {
  82. case _ if((c > 8 && c < 14) || c == 32):
  83. if(foundCount > 0) {
  84. return null;
  85. }
  86. continue;
  87. case '-'.code if(foundCount == 0):
  88. sign = -1;
  89. case '+'.code if(foundCount == 0):
  90. sign = 1;
  91. case '0'.code if(foundCount == 0 || (foundCount == 1 && sign != 0)):
  92. case 'x'.code | 'X'.code if(previous == '0'.code && ((foundCount == 1 && sign == 0) || (foundCount == 2 && sign != 0))):
  93. base = 16;
  94. case _ if('0'.code <= c && c <= '9'.code):
  95. case _ if(base == 16 && (('a'.code <= c && c <= 'z'.code) || ('A'.code <= c && c <= 'Z'.code))):
  96. case _:
  97. break;
  98. }
  99. if((foundCount == 0 && sign == 0) || (foundCount == 1 && sign != 0)) {
  100. firstDigitIndex = i;
  101. }
  102. foundCount++;
  103. lastDigitIndex = i;
  104. previous = c;
  105. }
  106. if(firstDigitIndex <= lastDigitIndex) {
  107. var digits = x.substring(firstDigitIndex, lastDigitIndex + 1);
  108. return try {
  109. (sign == -1 ? -1 : 1) * cs.system.Convert.ToInt32(digits, base);
  110. } catch(e:cs.system.FormatException) {
  111. null;
  112. }
  113. }
  114. return null;
  115. }
  116. public static function parseFloat(x:String):Float {
  117. if (x == null)
  118. return Math.NaN;
  119. x = StringTools.ltrim(x);
  120. var found = false,
  121. hasDot = false,
  122. hasSign = false,
  123. hasE = false,
  124. hasESign = false,
  125. hasEData = false;
  126. var i = -1;
  127. inline function getch(i:Int):Int
  128. return cast((untyped x : cs.system.String)[i]);
  129. while (++i < x.length) {
  130. var chr = getch(i);
  131. if (chr >= '0'.code && chr <= '9'.code) {
  132. if (hasE) {
  133. hasEData = true;
  134. }
  135. found = true;
  136. } else
  137. switch (chr) {
  138. case 'e'.code | 'E'.code if (!hasE):
  139. hasE = true;
  140. case '.'.code if (!hasDot):
  141. hasDot = true;
  142. case '-'.code, '+'.code if (!found && !hasSign):
  143. hasSign = true;
  144. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  145. hasESign = true;
  146. case _:
  147. break;
  148. }
  149. }
  150. if (hasE && !hasEData) {
  151. i--;
  152. if (hasESign)
  153. i--;
  154. }
  155. if (i != x.length) {
  156. x = x.substr(0, i);
  157. }
  158. return try cs.system.Double.Parse(x, cs.system.globalization.CultureInfo.InvariantCulture) catch (e:Dynamic) Math.NaN;
  159. }
  160. extern inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  161. return cs.Lib.as(value, c);
  162. }
  163. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  164. extern inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  165. return downcast(value, c);
  166. }
  167. public static function random(x:Int):Int {
  168. if (x <= 0)
  169. return 0;
  170. return untyped Math.rand.Next(x);
  171. }
  172. }