Browse Source

Lua : migrate some methods out of Boot to StringTools, add some helper methods, add some simple haxedoc

Justin Donaldson 9 years ago
parent
commit
cd2cd10313
2 changed files with 87 additions and 30 deletions
  1. 14 5
      std/StringTools.hx
  2. 73 25
      std/lua/Boot.hx

+ 14 - 5
std/StringTools.hx

@@ -33,7 +33,7 @@ class StringTools {
 	/**
 		Encode an URL by using the standard format.
 	**/
-	#if (!java && !cpp) inline #end public static function urlEncode( s : String ) : String {
+	#if (!java && !cpp && !lua) inline #end public static function urlEncode( s : String ) : String {
 		#if flash
 			return untyped __global__["encodeURIComponent"](s);
 		#elseif neko
@@ -55,7 +55,12 @@ class StringTools {
 			var b = @:privateAccess s.bytes.urlEncode(len);
 			return @:privateAccess String.__alloc__(b,len);
 		#elseif lua
-			return lua.Boot.urlEncode(s);
+			s = lua.NativeStringTools.gsub(s, "\n", "\r\n");
+			s = lua.NativeStringTools.gsub(s, "([^%w %-%_%.%~])", function (c) {
+				return lua.NativeStringTools.format("%%%02X", lua.NativeStringTools.byte(c) + '');
+			});
+			s = lua.NativeStringTools.gsub(s, " ", "+");
+			return s;
 		#else
 			return null;
 		#end
@@ -64,7 +69,7 @@ class StringTools {
 	/**
 		Decode an URL using the standard format.
 	**/
-	#if (!java && !cpp) inline #end public static function urlDecode( s : String ) : String {
+	#if (!java && !cpp && !lua) inline #end public static function urlDecode( s : String ) : String {
 		#if flash
 			return untyped __global__["decodeURIComponent"](s.split("+").join(" "));
 		#elseif neko
@@ -85,8 +90,12 @@ class StringTools {
 			var len = 0;
 			var b = @:privateAccess s.bytes.urlDecode(len);
 			return @:privateAccess String.__alloc__(b,len);
-		#elseif lua 
-			return lua.Boot.urlDecode(s);
+		#elseif lua
+			s = lua.NativeStringTools.gsub (s, "+", " ");
+			s = lua.NativeStringTools.gsub (s, "%%(%x%x)",
+				function(h) {return lua.NativeStringTools.char(lua.Lua.tonumber(h,16));});
+			s = lua.NativeStringTools.gsub (s, "\r\n", "\n");
+			return s;
 		#else
 			return null;
 		#end

+ 73 - 25
std/lua/Boot.hx

@@ -26,6 +26,7 @@ package lua;
 import lua.Bit;
 import lua.Table;
 import lua.Thread;
+import haxe.io.Path;
 
 import haxe.Constraints.Function;
 using lua.PairTools;
@@ -46,6 +47,9 @@ class Boot {
 	static function __unhtml(s : String)
 		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
 
+	/*
+	   Binds a function definition to the given instance
+	*/
 	@:keep
 	public static function bind(o:Dynamic, m: Function) : Function{
 		if (m == null) return null;
@@ -70,6 +74,10 @@ class Boot {
 		else return untyped __define_feature__("lua.Boot.isEnum", e.__ename__);
 	}
 
+	/*
+	   Returns the class of a given object, and defines the getClass feature
+	   for the given class.
+	*/
 	static inline public function getClass(o:Dynamic) : Dynamic {
 		if (Std.is(o, Array)) return Array;
 		else {
@@ -79,6 +87,9 @@ class Boot {
 		}
 	}
 
+	/*
+	   Indicates if the given object is an instance of the given Type
+	*/
 	@:ifFeature("typed_catch")
 	private static function __instanceof(o : Dynamic, cl : Dynamic) {
 		if( cl == null ) return false;
@@ -122,6 +133,10 @@ class Boot {
 			}
 		}
 	}
+
+	/*
+	   Indicates if the given object inherits from the given class
+	*/
 	static function inheritsFrom(o:Dynamic, cl:Class<Dynamic>) : Bool {
 		while (Lua.getmetatable(o) != null && Lua.getmetatable(o).__index != null){
 			if (Lua.getmetatable(o).__index == untyped cl.prototype) return true;
@@ -136,25 +151,9 @@ class Boot {
 		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
 	}
 
-
-
-	public static function urlDecode(str:String){
-		str = NativeStringTools.gsub (str, "+", " ");
-		str = NativeStringTools.gsub (str, "%%(%x%x)",
-				function(h) {return NativeStringTools.char(lua.Lua.tonumber(h,16));});
-		str = NativeStringTools.gsub (str, "\r\n", "\n");
-		return str;
-	}
-
-	public static function urlEncode(str:String){
-		str = NativeStringTools.gsub(str, "\n", "\r\n");
-		str = NativeStringTools.gsub(str, "([^%w %-%_%.%~])", function (c) {
-			return NativeStringTools.format("%%%02X", NativeStringTools.byte(c) + '');
-		});
-		str = NativeStringTools.gsub(str, " ", "+");
-		return str;
-	}
-
+	/*
+	   Helper method to generate a string representation of an enum
+	*/
 	static function printEnum(o:Array<Dynamic>, s : String){
 		if (o.length == 2){
 			return o[0];
@@ -172,11 +171,17 @@ class Boot {
 		}
 	}
 
+	/*
+	   Helper method to generate a string representation of a class
+	*/
 	static inline function printClass(c:Table<String,Dynamic>, s : String) : String {
 		return '{${printClassRec(c,'',s)}}';
 
 	}
 
+	/*
+	   Helper method to generate a string representation of a class
+	*/
 	static function printClassRec(c:Table<String,Dynamic>, result='', s : String) : String {
 		c.pairsEach(function(k,v){
 			if (result != "")
@@ -186,6 +191,9 @@ class Boot {
 		return result;
 	}
 
+	/*
+	   Generate a string representation for arbitrary object.
+	*/
 	@:ifFeature("has_enum")
 	static function __string_rec(o : Dynamic, s:String = "") {
 		return switch(untyped __type__(o)){
@@ -235,15 +243,17 @@ class Boot {
 
 	}
 
-	public inline static function tableToArray<T>(t:Table<Int,T>, ?length:Int) : Array<T> {
-		if (length == null) length = Table.maxn(t);
-		return cast defArray(t,length);
-	}
-
-	public inline static function defArray<T>(tab: Table<Int,T>, length : Int) : Array<T> {
+	/*
+	   Define an array from the given table
+	*/
+	public inline static function defArray<T>(tab: Table<Int,T>, ?length : Int) : Array<T> {
+		if (length == null) length = Table.maxn(tab);
 		return untyped _hx_tabArray(tab, length);
 	}
 
+	/*
+	   Create a Haxe object from the given table structure
+	*/
 	public inline static function tableToObject<T>(t:Table<String,T>) : Dynamic<T> {
 		return untyped _hx_o(t);
 	}
@@ -262,10 +272,16 @@ class Boot {
 			+":"+(if( s < 10 ) "0"+s else ""+s);
 	}
 
+	/*
+	   A 32 bit clamp function for integers
+	*/
 	public inline static function clamp(x:Int){
 		return untyped _hx_bit_clamp(x);
 	}
 
+	/*
+	   Create a standard date object from a lua string representation
+	*/
 	public static function strDate( s : String ) : std.Date {
 		switch( s.length ) {
 		case 8: // hh:mm:ss
@@ -292,10 +308,42 @@ class Boot {
 		}
 	}
 
+	/*
+	   create an empty table.
+	*/
 	public inline static function createTable<K,V>() : Table<K,V> {
 		return untyped __lua__("{}");
 	}
 
+	/*
+		Returns a shell escaped version of "cmd" along with any args
+	*/
+	public static function shellEscapeCmd(cmd : String, ?args : Array<String>){
+		if (args != null) {
+			switch (Sys.systemName()) {
+				case "Windows":
+					cmd = [
+						for (a in [StringTools.replace(cmd, "/", "\\")].concat(args))
+						StringTools.quoteWinArg(a, true)
+					].join(" ");
+				case _:
+					cmd = [cmd].concat(args).map(StringTools.quoteUnixArg).join(" ");
+			}
+		}
+		return cmd;
+	}
+
+	/*
+	   Returns a temp file path that can be used for reading and writing
+	*/
+	public static function tempFile() : String {
+		switch (Sys.systemName()){
+			case "Windows" : return Path.join([Os.getenv("TMP"), Os.tmpname()]);
+			default : return Os.tmpname();
+		}
+	}
+
+
 	public static function __init__(){
 		// anonymous to instance method wrapper
 		haxe.macro.Compiler.includeFile("lua/_lua/_hx_function_to_instance_function.lua");