@@ -12,14 +12,15 @@ import (
12
12
var RegMatchChars = regexp .MustCompile (`(^|[^\\])([*[?])` )
13
13
14
14
// StripPrefix removes the root path from the given path. Root may be a glob.
15
+ // The returned string has all backslashes replaced with slashes.
15
16
func StripPrefix (root , path string ) (string , error ) {
16
17
var err error
17
18
root , err = filepath .Abs (cleanGlob (root ))
18
19
if err != nil {
19
20
return "" , err
20
21
}
21
22
22
- if ! strings .HasSuffix (root , string ( filepath . Separator ) ) {
23
+ if ! strings .HasSuffix (root , "/" ) {
23
24
root += string (filepath .Separator )
24
25
}
25
26
@@ -28,21 +29,23 @@ func StripPrefix(root, path string) (string, error) {
28
29
return "" , err
29
30
}
30
31
31
- return strings .TrimPrefix (path , root ), nil
32
+ return strings .TrimPrefix (filepath . ToSlash ( path ) , root ), nil
32
33
}
33
34
34
- // cleanGlob removes all the parts of a glob that are not fixed.
35
+ // cleanGlob removes all the parts of a glob that are not fixed. It also
36
+ // converts all slashes or backslashes to /.
35
37
func cleanGlob (pattern string ) string {
38
+ pattern = filepath .ToSlash (pattern )
36
39
var parts []string
37
- for _ , part := range strings .Split (pattern , string ( filepath . Separator ) ) {
40
+ for _ , part := range strings .Split (pattern , "/" ) {
38
41
if strings .ContainsAny (part , "*?[\\ " ) {
39
42
break
40
43
}
41
44
42
45
parts = append (parts , part )
43
46
}
44
47
45
- return strings .Join (parts , string ( filepath . Separator ) )
48
+ return strings .Join (parts , "/" )
46
49
}
47
50
48
51
// PatternMatches returns the paths matched and any error found.
0 commit comments