mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-29 06:12:35 +09:00
util: Improve and rename EscapePath() to DetermineEscapePath()
If the new URL encoded path is found then it has precedence over the '%' escaped path. In case none of both is found the new URL approach is used.
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/user"
|
||||
"path/filepath"
|
||||
@@ -408,8 +409,13 @@ func GetModTime(path string) (time.Time, error) {
|
||||
return info.ModTime(), nil
|
||||
}
|
||||
|
||||
// EscapePath replaces every path separator in a given path with a %
|
||||
func EscapePath(path string) string {
|
||||
// EscapePathUrl encodes the path in URL query form
|
||||
func EscapePathUrl(path string) string {
|
||||
return url.QueryEscape(filepath.ToSlash(path))
|
||||
}
|
||||
|
||||
// EscapePathLegacy replaces every path separator in a given path with a %
|
||||
func EscapePathLegacy(path string) string {
|
||||
path = filepath.ToSlash(path)
|
||||
if runtime.GOOS == "windows" {
|
||||
// ':' is not valid in a path name on Windows but is ok on Unix
|
||||
@@ -418,6 +424,24 @@ func EscapePath(path string) string {
|
||||
return strings.ReplaceAll(path, "/", "%")
|
||||
}
|
||||
|
||||
// DetermineEscapePath escapes a path, determining whether it should be escaped
|
||||
// using URL encoding (preferred, since it encodes unambiguously) or
|
||||
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
|
||||
// path exists in the given directory).
|
||||
func DetermineEscapePath(dir string, path string) string {
|
||||
url := filepath.Join(dir, EscapePathUrl(path))
|
||||
if _, err := os.Stat(url); err == nil {
|
||||
return url
|
||||
}
|
||||
|
||||
legacy := filepath.Join(dir, EscapePathLegacy(path))
|
||||
if _, err := os.Stat(legacy); err == nil {
|
||||
return legacy
|
||||
}
|
||||
|
||||
return url
|
||||
}
|
||||
|
||||
// GetLeadingWhitespace returns the leading whitespace of the given byte array
|
||||
func GetLeadingWhitespace(b []byte) []byte {
|
||||
ws := []byte{}
|
||||
|
||||
Reference in New Issue
Block a user