Std.hx 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. The Std class provides standard methods for manipulating basic types.
  27. **/
  28. class Std {
  29. /**
  30. Tells if a value v is of the type t.
  31. **/
  32. public static function is( v : Dynamic, t : Dynamic ) : Bool {
  33. return untyped
  34. #if flash
  35. flash.Boot.__instanceof(v,t);
  36. #elseif neko
  37. neko.Boot.__instanceof(v,t);
  38. #elseif js
  39. js.Boot.__instanceof(v,t);
  40. #elseif php
  41. untyped __call__("_hx_instanceof", v,t);
  42. #else
  43. false;
  44. #end
  45. }
  46. /**
  47. Convert any value to a String
  48. **/
  49. public static function string( s : Dynamic ) : String {
  50. return untyped
  51. #if flash
  52. flash.Boot.__string_rec(s,"");
  53. #elseif neko
  54. new String(__dollar__string(s));
  55. #elseif js
  56. js.Boot.__string_rec(s,"");
  57. #elseif php
  58. __call__("_hx_string_rec", s, '');
  59. #else
  60. "";
  61. #end
  62. }
  63. /**
  64. Convert a Float to an Int, rounded down.
  65. **/
  66. public #if (flash9 || php) inline #end static function int( x : Float ) : Int {
  67. #if flash9
  68. return untyped __int__(x);
  69. #elseif php
  70. return untyped __php__("intval")(x);
  71. #else
  72. if( x < 0 ) return Math.ceil(x);
  73. return Math.floor(x);
  74. #end
  75. }
  76. /**
  77. Convert a String to an Int, parsing different possible representations. Returns [null] if could not be parsed.
  78. **/
  79. public static function parseInt( x : String ) : Null<Int> {
  80. untyped {
  81. #if flash9
  82. var v = __global__["parseInt"](x);
  83. if( __global__["isNaN"](v) )
  84. return null;
  85. return v;
  86. #elseif flash
  87. var v = _global["parseInt"](x);
  88. if( Math.isNaN(v) )
  89. return null;
  90. return v;
  91. #elseif neko
  92. var t = __dollar__typeof(x);
  93. if( t == __dollar__tint )
  94. return x;
  95. if( t == __dollar__tfloat )
  96. return __dollar__int(x);
  97. if( t != __dollar__tobject )
  98. return null;
  99. return __dollar__int(x.__s);
  100. #elseif js
  101. var v = __js__("parseInt")(x);
  102. if( Math.isNaN(v) )
  103. return null;
  104. return v;
  105. #elseif php
  106. if(!__php__("is_numeric")(x)) return null;
  107. return x.substr(0, 2).toLowerCase() == "0x" ? __php__("intval(substr($x, 2), 16)") : __php__("intval($x)");
  108. #else
  109. return 0;
  110. #end
  111. }
  112. }
  113. /**
  114. Convert a String to a Float, parsing different possible reprensations.
  115. **/
  116. public static function parseFloat( x : String ) : Float {
  117. return untyped
  118. #if flash9
  119. __global__["parseFloat"](x);
  120. #elseif flash
  121. _global["parseFloat"](x);
  122. #elseif neko
  123. __dollar__float(x.__s);
  124. #elseif js
  125. __js__("parseFloat")(x);
  126. #elseif php
  127. __php__("is_numeric($x) ? floatval($x) : acos(1.01)");
  128. #else
  129. 0;
  130. #end
  131. }
  132. /**
  133. Return a random integer between 0 included and x excluded.
  134. **/
  135. public static function random( x : Int ) : Int {
  136. return untyped
  137. #if flash9
  138. Math.floor(Math.random()*x);
  139. #elseif flash
  140. __random__(x);
  141. #elseif neko
  142. Math._rand_int(Math.__rnd,x);
  143. #elseif js
  144. Math.floor(Math.random()*x);
  145. #elseif php
  146. __call__("rand", 0, x-1);
  147. #else
  148. 0;
  149. #end
  150. }
  151. /**
  152. Initialization the things needed for reflection
  153. **/
  154. static function __init__() untyped {
  155. #if js
  156. String.prototype.__class__ = String;
  157. String.__name__ = ["String"];
  158. Array.prototype.__class__ = Array;
  159. Array.__name__ = ["Array"];
  160. Int = { __name__ : ["Int"] };
  161. Dynamic = { __name__ : ["Dynamic"] };
  162. Float = __js__("Number");
  163. Float.__name__ = ["Float"];
  164. Bool = { __ename__ : ["Bool"] };
  165. Class = { __name__ : ["Class"] };
  166. Enum = {};
  167. Void = { __ename__ : ["Void"] };
  168. #elseif as3gen
  169. null;
  170. #elseif flash9
  171. Bool = __global__["Boolean"];
  172. Int = __global__["int"];
  173. Float = __global__["Number"];
  174. #elseif flash
  175. var g : Dynamic = _global;
  176. g["Int"] = { __name__ : ["Int"] };
  177. g["Bool"] = { __ename__ : ["Bool"] };
  178. g.Dynamic = { __name__ : [__unprotect__("Dynamic")] };
  179. g.Class = { __name__ : [__unprotect__("Class")] };
  180. g.Enum = {};
  181. g.Void = { __ename__ : [__unprotect__("Void")] };
  182. g["Float"] = _global["Number"];
  183. g["Float"][__unprotect__("__name__")] = ["Float"];
  184. Array.prototype[__unprotect__("__class__")] = Array;
  185. Array[__unprotect__("__name__")] = ["Array"];
  186. String.prototype[__unprotect__("__class__")] = String;
  187. String[__unprotect__("__name__")] = ["String"];
  188. g["ASSetPropFlags"](Array.prototype,null,7);
  189. #elseif neko
  190. Int = { __name__ : ["Int"] };
  191. Float = { __name__ : ["Float"] };
  192. Bool = { __ename__ : ["Bool"] };
  193. Dynamic = { __name__ : ["Dynamic"] };
  194. Class = { __name__ : ["Class"] };
  195. Enum = {};
  196. Void = { __ename__ : ["Void"] };
  197. var cl = neko.Boot.__classes;
  198. cl.String = String;
  199. cl.Array = Array;
  200. cl.Int = Int;
  201. cl.Float = Float;
  202. cl.Bool = Bool;
  203. cl.Dynamic = Dynamic;
  204. cl.Class = Class;
  205. cl.Enum = Enum;
  206. cl.Void = Void;
  207. #end
  208. }
  209. }