Browse Source

add haxe.io.Path.removeTrailingSlash

Simon Krajewski 12 years ago
parent
commit
d83f5112d4
1 changed files with 19 additions and 1 deletions
  1. 19 1
      std/haxe/io/Path.hx

+ 19 - 1
std/haxe/io/Path.hx

@@ -36,7 +36,7 @@ class Path {
 		This is the leading part of the path that is not part of the file name
 		This is the leading part of the path that is not part of the file name
 		and the extension.
 		and the extension.
 		
 		
-		Does not end with a / or \ separator.
+		Does not end with a `/` or `\` separator.
 		
 		
 		If the path has no directory, the value is null.
 		If the path has no directory, the value is null.
 	**/
 	**/
@@ -197,4 +197,22 @@ class Path {
 			else path;
 			else path;
 		}
 		}
 	}
 	}
+	
+	/**
+		Removes a trailing slash from `path` if it exists.
+		
+		If `path` does not end with a `/` or `\`, `path` is returned unchanged.
+		
+		Otherwise the substring of `path` excluding the trailing slash or
+		backslash is returned.
+		
+		If `path` is null, the result is unspecified.
+	**/
+	@:require(haxe_ver >= 3.1)
+	public static function removeTrailingSlash ( path : String ) : String {
+		return switch(path.charCodeAt(path.length - 1)) {
+			case '/'.code | '\\'.code: path.substr(0, -1);
+			case _: path;
+		}
+	}
 }
 }