Std.hx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. package;
  23. import python.internal.Internal;
  24. import python.internal.UBuiltins;
  25. import python.lib.Inspect;
  26. import python.Boot;
  27. import python.Syntax;
  28. @:keepInit
  29. @:coreApi class Std {
  30. @:access(python.Boot)
  31. public static function downcast<T:{}, S:T>(value:T, c:Class<S>):S {
  32. try {
  33. return UBuiltins.isinstance(value, c) || (Inspect.isInterface(c) && Boot.implementsInterface(value, c)) ? cast value : null;
  34. } catch (e:Dynamic) {
  35. return null;
  36. }
  37. }
  38. @:deprecated('Std.instance() is deprecated. Use Std.downcast() instead.')
  39. public static inline function instance<T:{}, S:T>(value:T, c:Class<S>):S {
  40. return downcast(value, c);
  41. }
  42. @:access(python.Boot)
  43. static inline function isMetaType(v:Dynamic, t:Dynamic):Bool {
  44. return Boot.isMetaType(v, t);
  45. }
  46. @:ifFeature("typed_cast")
  47. @:deprecated('Std.is is deprecated. Use Std.isOfType instead.')
  48. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  49. return isOfType(v, t);
  50. }
  51. @:access(python.Boot)
  52. @:ifFeature("typed_cast")
  53. public static function isOfType(v:Dynamic, t:Dynamic):Bool {
  54. if (v == null && t == null) {
  55. return false;
  56. }
  57. if (t == null) {
  58. return false;
  59. }
  60. if (isMetaType(t, Dynamic)) {
  61. return v != null;
  62. }
  63. var isBool = UBuiltins.isinstance(v, UBuiltins.bool);
  64. if (isMetaType(t, Bool) && isBool) {
  65. return true;
  66. }
  67. if (!isBool && !isMetaType(t, Bool) && isMetaType(t, Int) && UBuiltins.isinstance(v, UBuiltins.int)) {
  68. return true;
  69. }
  70. var vIsFloat = UBuiltins.isinstance(v, UBuiltins.float);
  71. if (!isBool && vIsFloat && isMetaType(t, Int) && Math.isFinite(v) && v == Std.int(v) && v <= 2147483647 && v >= -2147483648) {
  72. return true;
  73. }
  74. if (!isBool && isMetaType(t, Float) && (UBuiltins.isinstance(v, python.Syntax.tuple(UBuiltins.float, UBuiltins.int)))) {
  75. return true;
  76. }
  77. if (isMetaType(t, UBuiltins.str)) {
  78. return UBuiltins.isinstance(v, String);
  79. }
  80. var isEnumType = isMetaType(t, Enum);
  81. if (isEnumType && Inspect.isclass(v) && Internal.hasConstructs(v))
  82. return true;
  83. if (isEnumType)
  84. return false;
  85. var isClassType = isMetaType(t, Class);
  86. if (isClassType
  87. && !UBuiltins.isinstance(v, Enum)
  88. && Inspect.isclass(v)
  89. && Internal.hasClassName(v)
  90. && !Internal.hasConstructs(v))
  91. return true;
  92. if (isClassType)
  93. return false;
  94. if (try UBuiltins.isinstance(v, t) catch (e:Dynamic) false) {
  95. return true;
  96. }
  97. if (Inspect.isclass(t)) {
  98. return Boot.implementsInterface(v, t);
  99. } else {
  100. return false;
  101. }
  102. }
  103. @:access(python.Boot)
  104. public static function string(s:Dynamic):String {
  105. return python.Boot.toString(s);
  106. }
  107. public static inline function int(x:Float):Int {
  108. try {
  109. return UBuiltins.int(x);
  110. } catch (e:Dynamic) {
  111. return null;
  112. }
  113. }
  114. static inline function isSpaceChar(char:String):Bool
  115. return Syntax.isIn(char, " \n\r\t\x0b\x0c");
  116. static inline function isHexPrefix(cur:String, next:String):Bool
  117. return cur == '0' && (next == 'x' || next == 'X');
  118. static inline function isDecimalDigit(char:String):Bool
  119. return Syntax.isIn(char, "0123456789");
  120. static inline function isHexadecimalDigit(char:String):Bool
  121. return Syntax.isIn(char, "0123456789abcdefABCDEF");
  122. public static function parseInt(x:String):Null<Int> {
  123. if (x == null)
  124. return null;
  125. final len = x.length;
  126. var index = 0;
  127. inline function hasIndex(index:Int)
  128. return index < len;
  129. // skip whitespace
  130. while (hasIndex(index)) {
  131. if (!isSpaceChar(Syntax.arrayAccess(x, index)))
  132. break;
  133. ++index;
  134. }
  135. // handle sign
  136. final isNegative = hasIndex(index) && {
  137. final sign = Syntax.arrayAccess(x, index);
  138. if (sign == '-' || sign == '+') {
  139. ++index;
  140. }
  141. sign == '-';
  142. }
  143. // handle base
  144. final isHexadecimal = hasIndex(index + 1) && isHexPrefix(Syntax.arrayAccess(x, index), Syntax.arrayAccess(x, index + 1));
  145. if (isHexadecimal)
  146. index += 2; // skip prefix
  147. // handle digits
  148. final firstInvalidIndex = {
  149. var cur = index;
  150. if (isHexadecimal) {
  151. while (hasIndex(cur)) {
  152. if (!isHexadecimalDigit(Syntax.arrayAccess(x, cur)))
  153. break;
  154. ++cur;
  155. }
  156. } else {
  157. while (hasIndex(cur)) {
  158. if (!isDecimalDigit(Syntax.arrayAccess(x, cur)))
  159. break;
  160. ++cur;
  161. }
  162. }
  163. cur;
  164. }
  165. // no valid digits
  166. if (index == firstInvalidIndex)
  167. return null;
  168. final result = python.internal.UBuiltins.int(x.substring(index, firstInvalidIndex), if (isHexadecimal) 16 else 10);
  169. return if (isNegative) -result else result;
  170. }
  171. static function shortenPossibleNumber(x:String):String {
  172. var r = "";
  173. for (i in 0...x.length) {
  174. var c = x.charAt(i);
  175. switch (c.charCodeAt(0)) {
  176. case "0".code | "1".code | "2".code | "3".code | "4".code | "5".code | "6".code | "7".code | "8".code | "9".code | ".".code:
  177. r += c;
  178. case _:
  179. break;
  180. }
  181. }
  182. return r;
  183. }
  184. public static function parseFloat(x:String):Float {
  185. try {
  186. return UBuiltins.float(x);
  187. } catch (e:Dynamic) {
  188. if (x != null) {
  189. var r1 = shortenPossibleNumber(x);
  190. if (r1 != x) {
  191. return parseFloat(r1);
  192. }
  193. }
  194. return Math.NaN;
  195. }
  196. }
  197. public static inline function random(x:Int):Int {
  198. return if (x <= 0) 0 else python.internal.UBuiltins.int(Math.random() * x);
  199. }
  200. }