Browse Source

centralize path escaping in haxe.io.Path (non-public) and do not escape dots

Simon Krajewski 10 years ago
parent
commit
ac2055bd15
5 changed files with 24 additions and 42 deletions
  1. 1 1
      codegen.ml
  2. 4 14
      std/cs/_std/haxe/Resource.hx
  3. 11 0
      std/haxe/io/Path.hx
  4. 4 14
      std/java/_std/haxe/Resource.hx
  5. 4 13
      std/php/_std/haxe/Resource.hx

+ 1 - 1
codegen.ml

@@ -123,7 +123,7 @@ let is_removable_field ctx f =
 
 
 let escape_res_name name allow_dirs =
 let escape_res_name name allow_dirs =
 	ExtString.String.replace_chars (fun chr ->
 	ExtString.String.replace_chars (fun chr ->
-		if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' then
+		if (chr >= 'a' && chr <= 'z') || (chr >= 'A' && chr <= 'Z') || (chr >= '0' && chr <= '9') || chr = '_' || chr = '.' then
 			Char.escaped chr
 			Char.escaped chr
 		else if chr = '/' && allow_dirs then
 		else if chr = '/' && allow_dirs then
 			"/"
 			"/"

+ 4 - 14
std/cs/_std/haxe/Resource.hx

@@ -44,20 +44,9 @@ package haxe;
 		return content.copy();
 		return content.copy();
 	}
 	}
 
 
-	private static function unescapeName( name : String ) : String
-	{
-		var regex = ~/-x([0-9]+)/g;
-		return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
-	}
-
-	private static function escapeName( name : String ) : String
-	{
-		var regex = ~/[^A-Za-z0-9_\/]/g;
-		return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
-	}
-
+	@:access(haxe.io.Path.escape)
 	public static function getString( name : String ) : String {
 	public static function getString( name : String ) : String {
-		name = escapeName(name);
+		name = haxe.io.Path.escape(name, true);
 		var path = getPaths().get(name);
 		var path = getPaths().get(name);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		if (str != null)
 		if (str != null)
@@ -65,8 +54,9 @@ package haxe;
 		return null;
 		return null;
 	}
 	}
 
 
+	@:access(haxe.io.Path.escape)
 	public static function getBytes( name : String ) : haxe.io.Bytes {
 	public static function getBytes( name : String ) : haxe.io.Bytes {
-		name = escapeName(name);
+		name = haxe.io.Path.escape(name, true);
 		var path = getPaths().get(name);
 		var path = getPaths().get(name);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		var str = cs.Lib.toNativeType(haxe.Resource).Assembly.GetManifestResourceStream(path);
 		if (str != null)
 		if (str != null)

+ 11 - 0
std/haxe/io/Path.hx

@@ -303,9 +303,20 @@ class Path {
 	/**
 	/**
 		Returns true if the path is an absolute path, and false otherwise.
 		Returns true if the path is an absolute path, and false otherwise.
 	**/
 	**/
+	@:require(haxe_ver >= 3.2)
 	public static function isAbsolute ( path : String ) : Bool {
 	public static function isAbsolute ( path : String ) : Bool {
 		if (StringTools.startsWith(path, '/')) return true;
 		if (StringTools.startsWith(path, '/')) return true;
 		if (path.charAt(1) == ':') return true;
 		if (path.charAt(1) == ':') return true;
 		return false;
 		return false;
 	}
 	}
+
+	private static function unescape( path : String ) : String {
+		var regex = ~/-x([0-9][0-9])/g;
+		return regex.map(path, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
+	}
+
+	private static function escape( path : String, allowSlashes : Bool = false ) : String {
+		var regex = allowSlashes ? ~/[^A-Za-z0-9_\/\\\.]/g : ~/[^A-Za-z0-9_\.]/g;
+		return regex.map(path, function(v) return '-x' + v.matched(0).charCodeAt(0));
+	}
 }
 }

+ 4 - 14
std/java/_std/haxe/Resource.hx

@@ -29,20 +29,9 @@ package haxe;
 		return content.copy();
 		return content.copy();
 	}
 	}
 
 
-	private static function unescapeName( name : String ) : String
-	{
-		var regex = ~/-x([0-9]+)/g;
-		return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
-	}
-
-	private static function escapeName( name : String ) : String
-	{
-		var regex = ~/[^A-Za-z0-9_\/]/g;
-		return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
-	}
-
+	@:access(haxe.io.Path.escape)
 	public static function getString( name : String ) : String {
 	public static function getString( name : String ) : String {
-		name = escapeName(name);
+		name = haxe.io.Path.escape(name, true);
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		if (stream == null)
 		if (stream == null)
 			return null;
 			return null;
@@ -50,8 +39,9 @@ package haxe;
 		return stream.readAll().toString();
 		return stream.readAll().toString();
 	}
 	}
 
 
+	@:access(haxe.io.Path.escape)
 	public static function getBytes( name : String ) : haxe.io.Bytes {
 	public static function getBytes( name : String ) : haxe.io.Bytes {
-		name = escapeName(name);
+		name = haxe.io.Path.escape(name, true);
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		var stream = cast(Resource, java.lang.Class<Dynamic>).getResourceAsStream("/" + name);
 		if (stream == null)
 		if (stream == null)
 			return null;
 			return null;

+ 4 - 13
std/php/_std/haxe/Resource.hx

@@ -35,26 +35,17 @@ class Resource {
 		return untyped __call__('dirname', __php__('__FILE__'))+"/../../res";
 		return untyped __call__('dirname', __php__('__FILE__'))+"/../../res";
 	}
 	}
 
 
-	private static function unescapeName( name : String ) : String
-	{
-		var regex = ~/-x([0-9]{2})/g;
-		return regex.map(name, function(regex) return String.fromCharCode(Std.parseInt(regex.matched(1))));
-	}
-
-	private static function escapeName( name : String ) : String {
-		var regex = ~/[^A-Za-z0-9_]/g;
-		return regex.map(name, function(v) return '-x' + v.matched(0).charCodeAt(0));
-	}
-
+	@:access(haxe.io.Path.escape)
 	static function getPath(name : String) : String {
 	static function getPath(name : String) : String {
-		return getDir()+'/'+escapeName(name);
+		return getDir()+'/'+haxe.io.Path.escape(name);
 	}
 	}
 
 
+	@:access(haxe.io.Path.unescape)
 	public static function listNames() : Array<String> {
 	public static function listNames() : Array<String> {
 		var a = sys.FileSystem.readDirectory(getDir());
 		var a = sys.FileSystem.readDirectory(getDir());
 		if(a[0] == '.') a.shift();
 		if(a[0] == '.') a.shift();
 		if(a[0] == '..') a.shift();
 		if(a[0] == '..') a.shift();
-		return a.map(function(s) return unescapeName(s));
+		return a.map(function(s) return haxe.io.Path.unescape(s));
 	}
 	}
 
 
 	public static function getString( name : String ) : String {
 	public static function getString( name : String ) : String {