Std.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. public static inline function is(v:Dynamic, t:Dynamic):Bool {
  48. return isOfType(v, t);
  49. }
  50. @:access(python.Boot)
  51. @:ifFeature("typed_cast")
  52. public static function isOfType(v:Dynamic, t:Dynamic):Bool {
  53. if (v == null && t == null) {
  54. return false;
  55. }
  56. if (t == null) {
  57. return false;
  58. }
  59. if (isMetaType(t, Dynamic)) {
  60. return v != null;
  61. }
  62. var isBool = UBuiltins.isinstance(v, UBuiltins.bool);
  63. if (isMetaType(t, Bool) && isBool) {
  64. return true;
  65. }
  66. if (!isBool && !isMetaType(t, Bool) && isMetaType(t, Int) && UBuiltins.isinstance(v, UBuiltins.int)) {
  67. return true;
  68. }
  69. var vIsFloat = UBuiltins.isinstance(v, UBuiltins.float);
  70. if (!isBool && vIsFloat && isMetaType(t, Int) && Math.isFinite(v) && v == Std.int(v) && v <= 2147483647 && v >= -2147483648) {
  71. return true;
  72. }
  73. if (!isBool && isMetaType(t, Float) && (UBuiltins.isinstance(v, python.Syntax.tuple(UBuiltins.float, UBuiltins.int)))) {
  74. return true;
  75. }
  76. if (isMetaType(t, UBuiltins.str)) {
  77. return UBuiltins.isinstance(v, String);
  78. }
  79. var isEnumType = isMetaType(t, Enum);
  80. if (isEnumType && Inspect.isclass(v) && Internal.hasConstructs(v))
  81. return true;
  82. if (isEnumType)
  83. return false;
  84. var isClassType = isMetaType(t, Class);
  85. if (isClassType
  86. && !UBuiltins.isinstance(v, Enum)
  87. && Inspect.isclass(v)
  88. && Internal.hasClassName(v)
  89. && !Internal.hasConstructs(v))
  90. return true;
  91. if (isClassType)
  92. return false;
  93. if (try UBuiltins.isinstance(v, t) catch (e:Dynamic) false) {
  94. return true;
  95. }
  96. if (Inspect.isclass(t)) {
  97. return Boot.implementsInterface(v, t);
  98. } else {
  99. return false;
  100. }
  101. }
  102. @:access(python.Boot)
  103. public static function string(s:Dynamic):String {
  104. return python.Boot.toString(s);
  105. }
  106. public static inline function int(x:Float):Int {
  107. try {
  108. return UBuiltins.int(x);
  109. } catch (e:Dynamic) {
  110. return null;
  111. }
  112. }
  113. public static function parseInt(x:String):Null<Int> {
  114. if (x == null)
  115. return null;
  116. try {
  117. return UBuiltins.int(x);
  118. } catch (e:Dynamic) {
  119. var base = 10;
  120. var len = x.length;
  121. var foundCount = 0;
  122. var sign = 0;
  123. var firstDigitIndex = 0;
  124. var lastDigitIndex = -1;
  125. var previous = 0;
  126. for(i in 0...len) {
  127. var c = StringTools.fastCodeAt(x, i);
  128. switch c {
  129. case _ if((c > 8 && c < 14) || c == 32):
  130. if(foundCount > 0) {
  131. return null;
  132. }
  133. continue;
  134. case '-'.code if(foundCount == 0):
  135. sign = -1;
  136. case '+'.code if(foundCount == 0):
  137. sign = 1;
  138. case '0'.code if(foundCount == 0 || (foundCount == 1 && sign != 0)):
  139. case 'x'.code | 'X'.code if(previous == '0'.code && ((foundCount == 1 && sign == 0) || (foundCount == 2 && sign != 0))):
  140. base = 16;
  141. case _ if('0'.code <= c && c <= '9'.code):
  142. case _ if(base == 16 && (('a'.code <= c && c <= 'z'.code) || ('A'.code <= c && c <= 'Z'.code))):
  143. case _:
  144. break;
  145. }
  146. if((foundCount == 0 && sign == 0) || (foundCount == 1 && sign != 0)) {
  147. firstDigitIndex = i;
  148. }
  149. foundCount++;
  150. lastDigitIndex = i;
  151. previous = c;
  152. }
  153. if(firstDigitIndex <= lastDigitIndex) {
  154. var digits = x.substring(firstDigitIndex, lastDigitIndex + 1);
  155. return try {
  156. (sign == -1 ? -1 : 1) * UBuiltins.int(digits, base);
  157. } catch(e:Dynamic) {
  158. null;
  159. }
  160. }
  161. return null;
  162. }
  163. }
  164. static function shortenPossibleNumber(x:String):String {
  165. var r = "";
  166. for (i in 0...x.length) {
  167. var c = x.charAt(i);
  168. switch (c.charCodeAt(0)) {
  169. case "0".code | "1".code | "2".code | "3".code | "4".code | "5".code | "6".code | "7".code | "8".code | "9".code | ".".code:
  170. r += c;
  171. case _:
  172. break;
  173. }
  174. }
  175. return r;
  176. }
  177. public static function parseFloat(x:String):Float {
  178. try {
  179. return UBuiltins.float(x);
  180. } catch (e:Dynamic) {
  181. if (x != null) {
  182. var r1 = shortenPossibleNumber(x);
  183. if (r1 != x) {
  184. return parseFloat(r1);
  185. }
  186. }
  187. return Math.NaN;
  188. }
  189. }
  190. public static inline function random(x:Int):Int {
  191. return if (x <= 0) 0 else python.internal.UBuiltins.int(Math.random() * x);
  192. }
  193. }