PageRenderTime 29ms CodeModel.GetById 15ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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