瀏覽代碼

respect windows \ paths in Path.normalize

Simon Krajewski 11 年之前
父節點
當前提交
34cb96810e
共有 2 個文件被更改,包括 12 次插入13 次删除
  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'
 	**/
-	public static function join( path1 : String, path2 : String ) : String {
+	public static function join(path1 : String, path2 : String ) : String {
 		path1 = Path.addTrailingSlash(path1);
-
 		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 = '/';
-
+		path = path.split("\\").join("/");
 		if( path == null || path == slash ) {
 			return slash;
 		}
 
-		var prependSlash = (path.charAt(0) == slash ||
-			path.charAt(0) == '.');
+		var prependSlash = (path.charAt(0) == slash || path.charAt(0) == '.');
 		var target = [];
 		var src;
 		var parts;
 		var token;
 
 		src = path.split(slash);
-
 		for( i in 0...src.length ) {
 			token = src[i];
 
@@ -211,13 +213,9 @@ class Path {
 				target.push(token);
 			}
 		}
-
-		var regex = ~/[\/]{2,}/g;
-
+		var regex = ~/\/\//g;
 		var tmp = target.join(slash);
-
 		var result = regex.replace(tmp, slash);
-
 		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/../../test.foo") == "/test.foo";
 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
 haxe.io.Path.join("dir1/dir2", "dir3/dir4") == "dir1/dir2/dir3/dir4";