Skip to content

Commit

Permalink
fix: path separator on windows (#89)
Browse files Browse the repository at this point in the history
Signed-off-by: Yoan Blanc <yoan@dosimple.ch>
  • Loading branch information
greut committed Nov 28, 2020
1 parent 40e7603 commit ba1adbf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
7 changes: 6 additions & 1 deletion editorconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"io"
"os"
"runtime"
"strings"

"gopkg.in/ini.v1"
Expand Down Expand Up @@ -122,7 +123,11 @@ func (e *Editorconfig) GetDefinitionForFilename(name string) (*Definition, error
}

if !strings.HasPrefix(name, "/") {
name = "/" + name
if runtime.GOOS != "windows" {
name = "/" + name
} else {
name = "\\" + name
}
}

ok, err := e.FnmatchCase(selector, name)
Expand Down
12 changes: 9 additions & 3 deletions fnmatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package editorconfig
import (
"fmt"
"regexp"
"runtime"
"strconv"
"strings"
)
Expand Down Expand Up @@ -41,6 +42,11 @@ func translate(pattern string) string { // nolint: gocyclo

matchesBraces := len(findLeftBrackets.FindAllString(pattern, -1)) == len(findRightBrackets.FindAllString(pattern, -1))

pathSeparator := "/"
if runtime.GOOS == "windows" {
pathSeparator = regexp.QuoteMeta("\\")
}

for index < length {
r := pat[index]
index++
Expand All @@ -52,19 +58,19 @@ func translate(pattern string) string { // nolint: gocyclo
result.WriteString(".*")
index++
} else {
result.WriteString("[^/]*")
result.WriteString(fmt.Sprintf("[^%s]*", pathSeparator))
}
case '/':
p := index
if p+2 < length && pat[p] == '*' && pat[p+1] == '*' && pat[p+2] == '/' {
result.WriteString("(?:/|/.*/)")
result.WriteString(fmt.Sprintf("(?:%s|%s.*%s)", pathSeparator, pathSeparator, pathSeparator))

index += 3
} else {
result.WriteRune(r)
}
case '?':
result.WriteString("[^/]")
result.WriteString(fmt.Sprintf("[^%s]", pathSeparator))
case '[':
if inBrackets {
result.WriteString("\\[")
Expand Down

0 comments on commit ba1adbf

Please sign in to comment.