/bin/std/Std.hx

http://github.com/Yoomee/clippy · Haxe · 229 lines · 172 code · 9 blank · 48 comment · 14 complexity · a1b763a81f444cb6d24e05a44187af33 MD5 · raw file

  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. #elseif cpp
  43. __global__.__instanceof(v,t);
  44. #else
  45. false;
  46. #end
  47. }
  48. /**
  49. Convert any value to a String
  50. **/
  51. public static function string( s : Dynamic ) : String {
  52. return untyped
  53. #if flash
  54. flash.Boot.__string_rec(s,"");
  55. #elseif neko
  56. new String(__dollar__string(s));
  57. #elseif js
  58. js.Boot.__string_rec(s,"");
  59. #elseif php
  60. __call__("_hx_string_rec", s, '');
  61. #elseif cpp
  62. s==null ? "null" : s.toString();
  63. #else
  64. "";
  65. #end
  66. }
  67. /**
  68. Convert a Float to an Int, rounded down.
  69. **/
  70. public #if (flash9 || php) inline #end static function int( x : Float ) : Int {
  71. #if flash9
  72. return untyped __int__(x);
  73. #elseif php
  74. return untyped __php__("intval")(x);
  75. #elseif cpp
  76. return untyped __global__.__int__(x);
  77. #else
  78. if( x < 0 ) return Math.ceil(x);
  79. return Math.floor(x);
  80. #end
  81. }
  82. /**
  83. Convert a String to an Int, parsing different possible representations. Returns [null] if could not be parsed.
  84. **/
  85. public static function parseInt( x : String ) : Null<Int> {
  86. untyped {
  87. #if flash9
  88. var v = __global__["parseInt"](x);
  89. if( __global__["isNaN"](v) )
  90. return null;
  91. return v;
  92. #elseif flash
  93. var v = _global["parseInt"](x);
  94. if( Math.isNaN(v) )
  95. return null;
  96. return v;
  97. #elseif neko
  98. var t = __dollar__typeof(x);
  99. if( t == __dollar__tint )
  100. return x;
  101. if( t == __dollar__tfloat )
  102. return __dollar__int(x);
  103. if( t != __dollar__tobject )
  104. return null;
  105. return __dollar__int(x.__s);
  106. #elseif js
  107. var v = __js__("parseInt")(x);
  108. if( Math.isNaN(v) )
  109. return null;
  110. return v;
  111. #elseif php
  112. if(!__php__("is_numeric")(x)) return null;
  113. return x.substr(0, 2).toLowerCase() == "0x" ? __php__("intval(substr($x, 2), 16)") : __php__("intval($x)");
  114. #elseif cpp
  115. if (x==null) return null;
  116. return __global__.__hxcpp_parse_int(x);
  117. #else
  118. return 0;
  119. #end
  120. }
  121. }
  122. /**
  123. Convert a String to a Float, parsing different possible reprensations.
  124. **/
  125. public static function parseFloat( x : String ) : Float {
  126. return untyped
  127. #if flash9
  128. __global__["parseFloat"](x);
  129. #elseif flash
  130. _global["parseFloat"](x);
  131. #elseif neko
  132. __dollar__float(x.__s);
  133. #elseif js
  134. __js__("parseFloat")(x);
  135. #elseif php
  136. __php__("is_numeric($x) ? floatval($x) : acos(1.01)");
  137. #elseif cpp
  138. __global__.__hxcpp_parse_float(x.__s);
  139. #else
  140. 0;
  141. #end
  142. }
  143. /**
  144. Return a random integer between 0 included and x excluded.
  145. **/
  146. public static function random( x : Int ) : Int {
  147. return untyped
  148. #if flash9
  149. Math.floor(Math.random()*x);
  150. #elseif flash
  151. __random__(x);
  152. #elseif neko
  153. Math._rand_int(Math.__rnd,x);
  154. #elseif js
  155. Math.floor(Math.random()*x);
  156. #elseif php
  157. __call__("rand", 0, x-1);
  158. #elseif cpp
  159. __global__.rand() % x;
  160. #else
  161. 0;
  162. #end
  163. }
  164. /**
  165. Initialization the things needed for reflection
  166. **/
  167. static function __init__() untyped {
  168. #if js
  169. String.prototype.__class__ = String;
  170. String.__name__ = ["String"];
  171. Array.prototype.__class__ = Array;
  172. Array.__name__ = ["Array"];
  173. Int = { __name__ : ["Int"] };
  174. Dynamic = { __name__ : ["Dynamic"] };
  175. Float = __js__("Number");
  176. Float.__name__ = ["Float"];
  177. Bool = { __ename__ : ["Bool"] };
  178. Class = { __name__ : ["Class"] };
  179. Enum = {};
  180. Void = { __ename__ : ["Void"] };
  181. #elseif cpp
  182. null;
  183. #elseif flash9
  184. null;
  185. #elseif flash
  186. var g : Dynamic = _global;
  187. g["Int"] = { __name__ : ["Int"] };
  188. g["Bool"] = { __ename__ : ["Bool"] };
  189. g.Dynamic = { __name__ : [__unprotect__("Dynamic")] };
  190. g.Class = { __name__ : [__unprotect__("Class")] };
  191. g.Enum = {};
  192. g.Void = { __ename__ : [__unprotect__("Void")] };
  193. g["Float"] = _global["Number"];
  194. g["Float"][__unprotect__("__name__")] = ["Float"];
  195. Array.prototype[__unprotect__("__class__")] = Array;
  196. Array[__unprotect__("__name__")] = ["Array"];
  197. String.prototype[__unprotect__("__class__")] = String;
  198. String[__unprotect__("__name__")] = ["String"];
  199. g["ASSetPropFlags"](Array.prototype,null,7);
  200. #elseif neko
  201. Int = { __name__ : ["Int"] };
  202. Float = { __name__ : ["Float"] };
  203. Bool = { __ename__ : ["Bool"] };
  204. Dynamic = { __name__ : ["Dynamic"] };
  205. Class = { __name__ : ["Class"] };
  206. Enum = {};
  207. Void = { __ename__ : ["Void"] };
  208. var cl = neko.Boot.__classes;
  209. cl.String = String;
  210. cl.Array = Array;
  211. cl.Int = Int;
  212. cl.Float = Float;
  213. cl.Bool = Bool;
  214. cl.Dynamic = Dynamic;
  215. cl.Class = Class;
  216. cl.Enum = Enum;
  217. cl.Void = Void;
  218. #end
  219. }
  220. }