瀏覽代碼

add haxe.io.Path.removeTrailingSlash

Simon Krajewski 12 年之前
父節點
當前提交
d83f5112d4
共有 1 個文件被更改,包括 19 次插入1 次删除
  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
 		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.
 	**/
@@ -197,4 +197,22 @@ class 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;
+		}
+	}
 }