Std.hx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * Copyright (C)2005-2012 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.lib.Builtin;
  25. import python.lib.Inspect;
  26. import python.Boot;
  27. import python.Syntax;
  28. @:keepInit
  29. @:coreApi class Std {
  30. public static inline function instance<T:{}, S:T>( value : T, c : Class<S> ) : S {
  31. try {
  32. return Builtin.isinstance(value,c) ? cast value : null;
  33. } catch (e:Dynamic) {
  34. return null;
  35. }
  36. }
  37. @:access(python.Boot.getSuperClass)
  38. public static function is( v : Dynamic, t : Dynamic ) : Bool {
  39. if (v == null && t == null) {
  40. return false;
  41. }
  42. if (t == null) {
  43. return false;
  44. }
  45. if (t == Dynamic) {
  46. return true;
  47. }
  48. var isBool = Builtin.isinstance(v, Builtin.bool);
  49. if (t == Bool && isBool) {
  50. return true;
  51. }
  52. if (!isBool && t != Bool && t == Int && Builtin.isinstance(v, Builtin.int )) {
  53. return true;
  54. }
  55. var vIsFloat = Builtin.isinstance(v, Builtin.float);
  56. if (!isBool && vIsFloat && t == Int && Math.isFinite(v) && v == Std.int(v) && v <= 2147483647 && v >= -2147483648) {
  57. return true;
  58. }
  59. if (!isBool && t == Float && ( Builtin.isinstance(v, python.Syntax.pythonCode("(float,int)")))) {
  60. return true;
  61. }
  62. if ( t == Builtin.str) {
  63. return Builtin.isinstance(v, String);
  64. }
  65. if (t == Enum && Inspect.isclass(v) && Internal.hasConstructs(v)) return true;
  66. if (t == Enum) return false;
  67. if (t == Class && !Builtin.isinstance(v, Enum) && Inspect.isclass(v) && Internal.hasClassName(v) && !Internal.hasConstructs(v)) return true;
  68. if (t == Class) return false;
  69. if (try Builtin.isinstance(v, t) catch (e:Dynamic) false) {
  70. return true;
  71. }
  72. if (Inspect.isclass(t)) {
  73. function loop (intf)
  74. {
  75. var f:Array<Dynamic> = if (Internal.hasInterfaces(intf)) Internal.fieldInterfaces(intf) else [];
  76. if (f != null) {
  77. for (i in f) {
  78. if ( i == t) {
  79. return true;
  80. } else {
  81. var l = loop(i);
  82. if (l) {
  83. return true;
  84. }
  85. }
  86. }
  87. return false;
  88. } else {
  89. return false;
  90. }
  91. }
  92. var currentClass = Syntax.field(v, "__class__");
  93. while(currentClass != null) {
  94. if (loop(currentClass)) {
  95. return true;
  96. }
  97. currentClass = python.Boot.getSuperClass(currentClass);
  98. }
  99. return false;
  100. } else {
  101. return false;
  102. }
  103. }
  104. @:access(python.Boot)
  105. @:keep
  106. public static function string( s : Dynamic ) : String
  107. {
  108. return python.Boot.toString(s);
  109. }
  110. public static inline function int( x : Float ) : Int
  111. {
  112. try {
  113. return Builtin.int(x);
  114. } catch (e:Dynamic) {
  115. return null;
  116. }
  117. }
  118. public static function parseInt( x : String ) : Null<Int> {
  119. if (x == null) return null;
  120. try {
  121. return Builtin.int(x);
  122. } catch (e:Dynamic) {
  123. try {
  124. var prefix = x.substr(0,2).toLowerCase();
  125. if (prefix == "0x") {
  126. return Builtin.int(x,16);
  127. }
  128. throw "fail";
  129. } catch (e:Dynamic) {
  130. var r = int(parseFloat(x));
  131. if (r == null) {
  132. var r1 = shortenPossibleNumber(x);
  133. if (r1 != x) {
  134. return parseInt(r1);
  135. } else {
  136. return null;
  137. }
  138. }
  139. return r;
  140. }
  141. }
  142. }
  143. static function shortenPossibleNumber (x:String):String
  144. {
  145. var r = "";
  146. for (i in 0...x.length) {
  147. var c = x.charAt(i);
  148. switch (c.charCodeAt(0)) {
  149. case "0".code
  150. | "1".code
  151. | "2".code
  152. | "3".code
  153. | "4".code
  154. | "5".code
  155. | "6".code
  156. | "7".code
  157. | "8".code
  158. | "9".code
  159. | ".".code : r += c;
  160. case _ : break;
  161. }
  162. }
  163. return r;
  164. }
  165. public static function parseFloat( x : String ) : Float
  166. {
  167. try {
  168. return Builtin.float(x);
  169. } catch (e:Dynamic) {
  170. if (x != null) {
  171. var r1 = shortenPossibleNumber(x);
  172. if (r1 != x) {
  173. return parseFloat(r1);
  174. }
  175. }
  176. return Math.NaN;
  177. }
  178. }
  179. public static inline function random( x : Int ) : Int {
  180. if (x <= 0) {
  181. return 0;
  182. } else {
  183. return int(Math.random()*x);
  184. }
  185. }
  186. }