Std.hx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C)2005-2015 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. {
  28. if (v == null)
  29. return t == Dynamic;
  30. if (t == null)
  31. return false;
  32. var clt:cs.system.Type = cast t;
  33. if (clt == null)
  34. return false;
  35. var name:String = cast clt;
  36. switch(name)
  37. {
  38. case "System.Double":
  39. return untyped __cs__('v is double || v is int');
  40. case "System.Int32":
  41. return untyped __cs__('haxe.lang.Runtime.isInt(v)');
  42. case "System.Boolean":
  43. return untyped __cs__('v is bool');
  44. case "System.Object":
  45. return true;
  46. }
  47. return clt.IsAssignableFrom(cs.Lib.getNativeType(v));
  48. }
  49. public static function string( s : Dynamic ) : String {
  50. if (s == null)
  51. return "null";
  52. if (Std.is(s, Bool))
  53. return cast(s, Bool) ? "true" : "false";
  54. return s.ToString();
  55. }
  56. public static inline function int( x : Float ) : Int {
  57. return cast x;
  58. }
  59. public static function parseInt( x : String ) : Null<Int> {
  60. if (x == null) return null;
  61. var ret = 0;
  62. var base = 10;
  63. var i = -1;
  64. var len = x.length;
  65. if (StringTools.startsWith(x, "0") && len > 2)
  66. {
  67. var c:Int = cast untyped x[1];
  68. if (c == 'x'.code || c == 'X'.code)
  69. {
  70. i = 1;
  71. base = 16;
  72. }
  73. }
  74. var foundAny = i != -1;
  75. var isNeg = false;
  76. while (++i < len)
  77. {
  78. var c = cast(untyped x[i], Int); //fastCodeAt
  79. if (!foundAny)
  80. {
  81. switch(c)
  82. {
  83. case '-'.code:
  84. isNeg = true;
  85. continue;
  86. case ' '.code, '\t'.code, '\n'.code, '\r'.code, '+'.code:
  87. if (isNeg)
  88. return null;
  89. continue;
  90. }
  91. }
  92. if (c >= '0'.code && c <= '9'.code)
  93. {
  94. if (!foundAny && c == '0'.code)
  95. {
  96. foundAny = true;
  97. continue;
  98. }
  99. ret *= base; foundAny = true;
  100. ret += c - '0'.code;
  101. } else if (base == 16) {
  102. if (c >= 'a'.code && c <= 'f'.code) {
  103. ret *= base; foundAny = true;
  104. ret += c - 'a'.code + 10;
  105. } else if (c >= 'A'.code && c <= 'F'.code) {
  106. ret *= base; foundAny = true;
  107. ret += c - 'A'.code + 10;
  108. } else {
  109. break;
  110. }
  111. } else {
  112. break;
  113. }
  114. }
  115. if (foundAny)
  116. return isNeg ? -ret : ret;
  117. else
  118. return null;
  119. }
  120. public static function parseFloat( x : String ) : Float {
  121. if (x == null) return Math.NaN;
  122. x = StringTools.ltrim(x);
  123. var found = false, hasDot = false, hasSign = false,
  124. hasE = false, hasESign = false, hasEData = false;
  125. var i = -1;
  126. inline function getch(i:Int):Int return cast ((untyped x : cs.system.String)[i]);
  127. while (++i < x.length)
  128. {
  129. var chr = getch(i);
  130. if (chr >= '0'.code && chr <= '9'.code)
  131. {
  132. if (hasE)
  133. {
  134. hasEData = true;
  135. }
  136. found = true;
  137. } else 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. {
  152. i--;
  153. if (hasESign)
  154. i--;
  155. }
  156. if (i != x.length)
  157. {
  158. x = x.substr(0,i);
  159. }
  160. return try
  161. cs.system.Double.Parse(x, cs.system.globalization.CultureInfo.InvariantCulture)
  162. catch(e:Dynamic)
  163. Math.NaN;
  164. }
  165. @:extern inline public static function instance<T:{},S:T>( value : T, c : Class<S> ) : S {
  166. return cs.Lib.as(value,c);
  167. }
  168. public static function random( x : Int ) : Int {
  169. if (x <= 0) return 0;
  170. return untyped Math.rand.Next(x);
  171. }
  172. }