Skip to content

Commit 30ef5f6

Browse files
chore: use filepath.Separator instead of hardcoded separator (#1685)
1 parent 8d88c13 commit 30ef5f6

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

internal/watcher/watch_pattern.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ func parseFilePatterns(filePatterns []string) ([]*watchPattern, error) {
3131
}
3232

3333
// this method prepares the watchPattern struct for a single file pattern (aka /path/*pattern)
34-
// TODO: using '/' is more efficient than filepath functions, but does not work on windows
3534
func parseFilePattern(filePattern string) (*watchPattern, error) {
3635
w := &watchPattern{}
3736

@@ -43,7 +42,7 @@ func parseFilePattern(filePattern string) (*watchPattern, error) {
4342
w.dir = absPattern
4443

4544
// then we split the pattern to determine where the directory ends and the pattern starts
46-
splitPattern := strings.Split(absPattern, "/")
45+
splitPattern := strings.Split(absPattern, string(filepath.Separator))
4746
patternWithoutDir := ""
4847
for i, part := range splitPattern {
4948
isFilename := i == len(splitPattern)-1 && strings.Contains(part, ".")
@@ -58,11 +57,11 @@ func parseFilePattern(filePattern string) (*watchPattern, error) {
5857
// now we split the pattern according to the recursive '**' syntax
5958
w.patterns = strings.Split(patternWithoutDir, "**")
6059
for i, pattern := range w.patterns {
61-
w.patterns[i] = strings.Trim(pattern, "/")
60+
w.patterns[i] = strings.Trim(pattern, string(filepath.Separator))
6261
}
6362

64-
// finally, we remove the trailing slash and add leading slash
65-
w.dir = "/" + strings.Trim(w.dir, "/")
63+
// finally, we remove the trailing separator and add leading separator
64+
w.dir = string(filepath.Separator) + strings.Trim(w.dir, string(filepath.Separator))
6665

6766
return w, nil
6867
}
@@ -94,8 +93,8 @@ func isValidPattern(fileName string, dir string, patterns []string) bool {
9493
return false
9594
}
9695

97-
// remove the dir and '/' from the filename
98-
fileNameWithoutDir := strings.TrimPrefix(strings.TrimPrefix(fileName, dir), "/")
96+
// remove the dir and separator from the filename
97+
fileNameWithoutDir := strings.TrimPrefix(strings.TrimPrefix(fileName, dir), string(filepath.Separator))
9998

10099
// if the pattern has size 1 we can match it directly against the filename
101100
if len(patterns) == 1 {
@@ -106,12 +105,12 @@ func isValidPattern(fileName string, dir string, patterns []string) bool {
106105
}
107106

108107
func matchPatterns(patterns []string, fileName string) bool {
109-
partsToMatch := strings.Split(fileName, "/")
108+
partsToMatch := strings.Split(fileName, string(filepath.Separator))
110109
cursor := 0
111110

112111
// if there are multiple patterns due to '**' we need to match them individually
113112
for i, pattern := range patterns {
114-
patternSize := strings.Count(pattern, "/") + 1
113+
patternSize := strings.Count(pattern, string(filepath.Separator)) + 1
115114

116115
// if we are at the last pattern we will start matching from the end of the filename
117116
if i == len(patterns)-1 {
@@ -121,7 +120,7 @@ func matchPatterns(patterns []string, fileName string) bool {
121120
// the cursor will move through the fileName until the pattern matches
122121
for j := cursor; j < len(partsToMatch); j++ {
123122
cursor = j
124-
subPattern := strings.Join(partsToMatch[j:j+patternSize], "/")
123+
subPattern := strings.Join(partsToMatch[j:j+patternSize], string(filepath.Separator))
125124
if matchBracketPattern(pattern, subPattern) {
126125
cursor = j + patternSize - 1
127126
break

0 commit comments

Comments
 (0)