Преглед на файлове

added haxe.io.Path.addTrailingSlash and (fixed issue #1464)

Simon Krajewski преди 12 години
родител
ревизия
b0286a4863
променени са 5 файла, в които са добавени 41 реда и са изтрити 3 реда
  1. 1 0
      std/cpp/_std/sys/FileSystem.hx
  2. 27 2
      std/haxe/io/Path.hx
  3. 1 0
      std/neko/_std/sys/FileSystem.hx
  4. 1 0
      std/php/_std/sys/FileSystem.hx
  5. 11 1
      tests/unit/unitstd/haxe/io/Path.unit.hx

+ 1 - 0
std/cpp/_std/sys/FileSystem.hx

@@ -67,6 +67,7 @@ class FileSystem {
 	}
 
 	public static function createDirectory( path : String ) : Void {
+		var path = haxe.io.Path.addTrailingSlash(path);
 		var parts = [while ((path = haxe.io.Path.directory(path)) != "") path];
 		parts.reverse();
 		for (part in parts) {

+ 27 - 2
std/haxe/io/Path.hx

@@ -49,7 +49,7 @@ class Path {
 		If there is no file name, e.g. for ".htaccess" or "/dir/", the value
 		is the empty String "".
 	**/
-	public var file(default, null) : String;	
+	public var file(default, null) : String;
 	
 	/**
 		The file extension.
@@ -123,7 +123,7 @@ class Path {
 		Returns the String representation of [path] without the directory.
 		
 		If [path] is null, the result is unspecified.
-	**/	
+	**/
 	public static function withoutDirectory( path ) {
 		var s = new Path(path);
 		s.dir = null;
@@ -171,4 +171,29 @@ class Path {
 		return s.toString();
 	}
 
+	/**
+		Adds a trailing slash to [path], if it does not have one already.
+		
+		If the last slash in [path] is a backslash, a backslash is appended to
+		[path].
+		
+		If the last slash in [path] is a slash, or if no slash is found, a slash
+		is appended to [path]. In particular, this applies to the empty String
+		"".
+		
+		If [path] is null, the result is unspecified.
+	**/
+	public static function addTrailingSlash( path : String ) : String {
+		if (path.length == 0)
+			return "/";
+		var c1 = path.lastIndexOf("/");
+		var c2 = path.lastIndexOf("\\");
+		return if ( c1 < c2 ) {
+			if (c2 != path.length - 1) path + "\\";
+			else path;
+		} else {
+			if (c1 != path.length - 1) path + "/";
+			else path;
+		}
+	}
 }

+ 1 - 0
std/neko/_std/sys/FileSystem.hx

@@ -64,6 +64,7 @@ class FileSystem {
 	}
 
 	public static function createDirectory( path : String ) : Void {
+		var path = haxe.io.Path.addTrailingSlash(path);
 		var parts = [while ((path = haxe.io.Path.directory(path)) != "") path];
 		parts.reverse();
 		for (part in parts) {

+ 1 - 0
std/php/_std/sys/FileSystem.hx

@@ -77,6 +77,7 @@ class FileSystem {
 	}
 
 	public static inline function createDirectory( path : String ) : Void {
+		var path = haxe.io.Path.addTrailingSlash(path);
 		var parts = [while ((path = haxe.io.Path.directory(path)) != "") path];
 		parts.reverse();
 		for (part in parts) {

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

@@ -58,4 +58,14 @@ haxe.io.Path.extension(path4) == "";
 haxe.io.Path.withExtension(path, "foo") == "/dir1/dir2/file.foo";
 haxe.io.Path.withExtension(path2, "foo") == "/dir1/dir.with.dots\\file.foo";
 haxe.io.Path.withExtension(path3, "foo") == ".foo";
-haxe.io.Path.withExtension(path4, "foo") == "/dir/.foo";
+haxe.io.Path.withExtension(path4, "foo") == "/dir/.foo";
+
+// addTrailingSlash
+haxe.io.Path.addTrailingSlash("") == "/";
+haxe.io.Path.addTrailingSlash("a") == "a/";
+haxe.io.Path.addTrailingSlash("a/") == "a/";
+haxe.io.Path.addTrailingSlash("a/b") == "a/b/";
+haxe.io.Path.addTrailingSlash("a/b/") == "a/b/";
+haxe.io.Path.addTrailingSlash("a\\") == "a\\";
+haxe.io.Path.addTrailingSlash("a\\b") == "a\\b\\";
+haxe.io.Path.addTrailingSlash("a\\b\\") == "a\\b\\";