...
Source file src/cmd/go/internal/str/path.go
1
2
3
4
5 package str
6
7 import (
8 "path"
9 "path/filepath"
10 "strings"
11 )
12
13
14
15 func HasPathPrefix(s, prefix string) bool {
16 if len(s) == len(prefix) {
17 return s == prefix
18 }
19 if prefix == "" {
20 return true
21 }
22 if len(s) > len(prefix) {
23 if prefix[len(prefix)-1] == '/' || s[len(prefix)] == '/' {
24 return s[:len(prefix)] == prefix
25 }
26 }
27 return false
28 }
29
30
31
32 func HasFilePathPrefix(s, prefix string) bool {
33 sv := strings.ToUpper(filepath.VolumeName(s))
34 pv := strings.ToUpper(filepath.VolumeName(prefix))
35 s = s[len(sv):]
36 prefix = prefix[len(pv):]
37 switch {
38 default:
39 return false
40 case sv != pv:
41 return false
42 case len(s) == len(prefix):
43 return s == prefix
44 case prefix == "":
45 return true
46 case len(s) > len(prefix):
47 if prefix[len(prefix)-1] == filepath.Separator {
48 return strings.HasPrefix(s, prefix)
49 }
50 return s[len(prefix)] == filepath.Separator && s[:len(prefix)] == prefix
51 }
52 }
53
54
55
56
57
58 func GlobsMatchPath(globs, target string) bool {
59 for globs != "" {
60
61 var glob string
62 if i := strings.Index(globs, ","); i >= 0 {
63 glob, globs = globs[:i], globs[i+1:]
64 } else {
65 glob, globs = globs, ""
66 }
67 if glob == "" {
68 continue
69 }
70
71
72
73
74 n := strings.Count(glob, "/")
75 prefix := target
76
77 for i := 0; i < len(target); i++ {
78 if target[i] == '/' {
79 if n == 0 {
80 prefix = target[:i]
81 break
82 }
83 n--
84 }
85 }
86 if n > 0 {
87
88 continue
89 }
90 matched, _ := path.Match(glob, prefix)
91 if matched {
92 return true
93 }
94 }
95 return false
96 }
97
View as plain text