Browse Source

Lua : Simple urlEncode

I added in a simple urlEncode implementation for lua, based heavily on
gsub extern use.  It seems to be the fastest.
Justin Donaldson 10 years ago
parent
commit
c5ff9829eb
2 changed files with 17 additions and 4 deletions
  1. 2 0
      std/StringTools.hx
  2. 15 4
      std/lua/Boot.hx

+ 2 - 0
std/StringTools.hx

@@ -54,6 +54,8 @@ class StringTools {
 			var len = 0;
 			var b = @:privateAccess s.bytes.urlEncode(len);
 			return @:privateAccess String.__alloc__(b,len);
+		#elseif lua
+			return lua.Boot.urlEncode(s);
 		#else
 			return null;
 		#end

+ 15 - 4
std/lua/Boot.hx

@@ -27,11 +27,11 @@ class Boot {
 	}
 	public static var unpack : Dynamic->lua.Table<Int,Dynamic> = untyped __lua__("function(...) return {...} end");
 
-	private static function __unhtml(s : String) {
+	static function __unhtml(s : String) {
 		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
 	}
 
-	private static function __trace(v,i : haxe.PosInfos) {
+	static function __trace(v,i : haxe.PosInfos) {
 		untyped {
 			var msg = if( i != null ) i.fileName+":"+i.lineNumber+": " else "";
 			msg += __string_rec(v, "");
@@ -46,7 +46,7 @@ class Boot {
 		}
 	}
 
-	private static function __clear_trace() {
+	static function __clear_trace() {
 		untyped {
 			var d = document.getElementById("haxe:trace");
 			if( d != null )
@@ -93,8 +93,19 @@ class Boot {
 	return nil");
 	}
 
+	public static function urlEncode(str:String){
+	if (str != null) {
+		str = lua.StringTools.gsub(str, "\n", "\r\n");
+		str = lua.StringTools.gsub(str, "([^%w %-%_%.%~])", function (c) {
+			return lua.StringTools.format("%%%02X", lua.StringTools.byte(c) + '');
+		});
+		str = lua.StringTools.gsub(str, " ", "+");
+	}
+	return str;
+	}
+
 	@:ifFeature("may_print_enum")
-	private static function __string_rec(o : Dynamic, s = '') {
+	static function __string_rec(o : Dynamic, s = '') {
 		untyped {
 			switch(__type__(o)){
 				case "nil": return "null";