Std.hx 5.0 KB

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