Std.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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 ret = 0;
  71. var base = 10;
  72. var i = -1;
  73. var len = x.length;
  74. if (StringTools.startsWith(x, "0") && len > 2) {
  75. var c:Int = cast untyped x[1];
  76. if (c == 'x'.code || c == 'X'.code) {
  77. i = 1;
  78. base = 16;
  79. }
  80. }
  81. var foundAny = i != -1;
  82. var isNeg = false;
  83. while (++i < len) {
  84. var c = cast(untyped x[i], Int); // fastCodeAt
  85. if (!foundAny) {
  86. switch (c) {
  87. case '-'.code:
  88. isNeg = true;
  89. continue;
  90. case ' '.code, '\t'.code, '\n'.code, '\r'.code, '+'.code:
  91. if (isNeg)
  92. return null;
  93. continue;
  94. }
  95. }
  96. if (c >= '0'.code && c <= '9'.code) {
  97. if (!foundAny && c == '0'.code) {
  98. foundAny = true;
  99. continue;
  100. }
  101. ret *= base;
  102. foundAny = true;
  103. ret += c - '0'.code;
  104. } else if (base == 16) {
  105. if (c >= 'a'.code && c <= 'f'.code) {
  106. ret *= base;
  107. foundAny = true;
  108. ret += c - 'a'.code + 10;
  109. } else if (c >= 'A'.code && c <= 'F'.code) {
  110. ret *= base;
  111. foundAny = true;
  112. ret += c - 'A'.code + 10;
  113. } else {
  114. break;
  115. }
  116. } else {
  117. break;
  118. }
  119. }
  120. if (foundAny)
  121. return isNeg ? -ret : ret;
  122. else
  123. return null;
  124. }
  125. public static function parseFloat(x:String):Float {
  126. if (x == null)
  127. return Math.NaN;
  128. x = StringTools.ltrim(x);
  129. var found = false,
  130. hasDot = false,
  131. hasSign = false,
  132. hasE = false,
  133. hasESign = false,
  134. hasEData = false;
  135. var i = -1;
  136. inline function getch(i:Int):Int
  137. return cast((untyped x : cs.system.String)[i]);
  138. while (++i < x.length) {
  139. var chr = getch(i);
  140. if (chr >= '0'.code && chr <= '9'.code) {
  141. if (hasE) {
  142. hasEData = true;
  143. }
  144. found = true;
  145. } else
  146. switch (chr) {
  147. case 'e'.code | 'E'.code if (!hasE):
  148. hasE = true;
  149. case '.'.code if (!hasDot):
  150. hasDot = true;
  151. case '-'.code, '+'.code if (!found && !hasSign):
  152. hasSign = true;
  153. case '-'.code | '+'.code if (found && !hasESign && hasE && !hasEData):
  154. hasESign = true;
  155. case _:
  156. break;
  157. }
  158. }
  159. if (hasE && !hasEData) {
  160. i--;
  161. if (hasESign)
  162. i--;
  163. }
  164. if (i != x.length) {
  165. x = x.substr(0, i);
  166. }
  167. return try cs.system.Double.Parse(x, cs.system.globalization.CultureInfo.InvariantCulture) catch (e:Dynamic) Math.NaN;
  168. }
  169. extern inline public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  170. return cs.Lib.as(value, c);
  171. }
  172. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  173. extern inline public static function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  174. return downcast(value, c);
  175. }
  176. public static function random(x:Int):Int {
  177. if (x <= 0)
  178. return 0;
  179. return untyped Math.rand.Next(x);
  180. }
  181. }