-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpath.go
More file actions
81 lines (68 loc) · 1.74 KB
/
path.go
File metadata and controls
81 lines (68 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 WoozyMasta
// Source: github.com/woozymasta/pathrules
package pathrules
import (
"path"
"strings"
)
// normalizePath normalizes matching path to slash-separated relative clean form.
func normalizePath(raw string) string {
raw = strings.TrimSpace(raw)
if strings.Contains(raw, `\`) {
raw = strings.ReplaceAll(raw, `\`, `/`)
}
raw = strings.TrimPrefix(raw, "./")
raw = strings.TrimPrefix(raw, "/")
if raw == "" {
return ""
}
// Fast path for already-normalized relative paths.
if isSimpleNormalizedPath(raw) {
return raw
}
raw = path.Clean("/" + raw)
raw = strings.TrimPrefix(raw, "/")
if raw == "." {
return ""
}
return strings.TrimSuffix(raw, "/")
}
// normalizePattern normalizes source pattern for compilation.
func normalizePattern(raw string) string {
raw = strings.TrimSpace(raw)
raw = strings.ReplaceAll(raw, `\`, `/`)
return raw
}
// asciiLower converts only ASCII A-Z to a-z and leaves all other bytes unchanged.
func asciiLower(s string) string {
for i := 0; i < len(s); i++ {
if s[i] >= 'A' && s[i] <= 'Z' {
b := []byte(s)
for j := i; j < len(b); j++ {
if b[j] >= 'A' && b[j] <= 'Z' {
b[j] += 'a' - 'A'
}
}
return string(b)
}
}
return s
}
// isSimpleNormalizedPath reports whether path is already normalized enough to skip path.Clean.
func isSimpleNormalizedPath(path string) bool {
if path == "" ||
path == "." ||
path == ".." ||
strings.HasPrefix(path, "/") ||
strings.HasSuffix(path, "/") ||
strings.HasPrefix(path, "./") ||
strings.HasPrefix(path, "../") ||
strings.Contains(path, "//") ||
strings.Contains(path, "/./") ||
strings.Contains(path, "/../") ||
strings.HasSuffix(path, "/..") {
return false
}
return true
}