Explorar o código

respect windows \ paths in Path.normalize

Simon Krajewski %!s(int64=11) %!d(string=hai) anos
pai
achega
34cb96810e
Modificáronse 2 ficheiros con 12 adicións e 13 borrados
  1. 11 13
      std/haxe/io/Path.hx
  2. 1 0
      tests/unit/unitstd/haxe/io/Path.unit.hx

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

@@ -177,31 +177,33 @@ class Path {
 
 
 		e.g. 'assets/maps' and '../textures/img.png' = 'assets/textures/img.png'
 		e.g. 'assets/maps' and '../textures/img.png' = 'assets/textures/img.png'
 	**/
 	**/
-	public static function join( path1 : String, path2 : String ) : String {
+	public static function join(path1 : String, path2 : String ) : String {
 		path1 = Path.addTrailingSlash(path1);
 		path1 = Path.addTrailingSlash(path1);
-
 		return Path.normalize(path1 + path2);
 		return Path.normalize(path1 + path2);
 	}
 	}
 
 
 	/**
 	/**
-		Normalize a given `path` (e.g. make '/usr/local/../lib' to '/usr/lib')
+		Normalize a given `path` (e.g. make '/usr/local/../lib' to '/usr/lib').
+		
+		Also replaces backslashes \ with slashes / and afterwards turns
+		multiple slashes into a single one.
+		
+		If `path` is null, the result is unspecified.
 	**/
 	**/
-	public static function normalize( path : String) : String {
+	public static function normalize(path : String) : String {
 		var slash = '/';
 		var slash = '/';
-
+		path = path.split("\\").join("/");
 		if( path == null || path == slash ) {
 		if( path == null || path == slash ) {
 			return slash;
 			return slash;
 		}
 		}
 
 
-		var prependSlash = (path.charAt(0) == slash ||
-			path.charAt(0) == '.');
+		var prependSlash = (path.charAt(0) == slash || path.charAt(0) == '.');
 		var target = [];
 		var target = [];
 		var src;
 		var src;
 		var parts;
 		var parts;
 		var token;
 		var token;
 
 
 		src = path.split(slash);
 		src = path.split(slash);
-
 		for( i in 0...src.length ) {
 		for( i in 0...src.length ) {
 			token = src[i];
 			token = src[i];
 
 
@@ -211,13 +213,9 @@ class Path {
 				target.push(token);
 				target.push(token);
 			}
 			}
 		}
 		}
-
-		var regex = ~/[\/]{2,}/g;
-
+		var regex = ~/\/\//g;
 		var tmp = target.join(slash);
 		var tmp = target.join(slash);
-
 		var result = regex.replace(tmp, slash);
 		var result = regex.replace(tmp, slash);
-
 		return (prependSlash ? slash : '') + result;
 		return (prependSlash ? slash : '') + result;
 	}
 	}
 
 

+ 1 - 0
tests/unit/unitstd/haxe/io/Path.unit.hx

@@ -64,6 +64,7 @@ haxe.io.Path.withExtension(path4, "foo") == "/dir/.foo";
 haxe.io.Path.normalize("dir1/dir2/../dir3") == "dir1/dir3";
 haxe.io.Path.normalize("dir1/dir2/../dir3") == "dir1/dir3";
 haxe.io.Path.normalize("/dir1/dir2/../../test.foo") == "/test.foo";
 haxe.io.Path.normalize("/dir1/dir2/../../test.foo") == "/test.foo";
 haxe.io.Path.normalize("dir1/dir2/dir3/dir4/../../../dir5") == "dir1/dir5";
 haxe.io.Path.normalize("dir1/dir2/dir3/dir4/../../../dir5") == "dir1/dir5";
+haxe.io.Path.normalize("C:\\Windows\\..\\Users/Waneck on Windows///.haxelib") == "C:/Users/Waneck on Windows/.haxelib";
 
 
 // join
 // join
 haxe.io.Path.join("dir1/dir2", "dir3/dir4") == "dir1/dir2/dir3/dir4";
 haxe.io.Path.join("dir1/dir2", "dir3/dir4") == "dir1/dir2/dir3/dir4";