Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle non-ASCII runes in patterns (fixes #54) #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,16 @@ func TestGlob(t *testing.T) {

glob(true, pattern_prefix_suffix, fixture_prefix_suffix_match),
glob(false, pattern_prefix_suffix, fixture_prefix_suffix_mismatch),

glob(true, "155ö", "155ö"),
glob(true, "1?5ö", "155ö"), // <-
glob(true, "1?ö5", "15ö5"),
glob(true, "155helloö", "155helloö"),
glob(true, "1?5helloö", "155helloö"), // <-
glob(true, "1?ö5hello", "15ö5hello"),
glob(true, "1?5heöllo", "155heöllo"),
glob(true, "1ö?5", "1ö55"), // <-
glob(true, "ö1?5", "ö155"),
} {
t.Run("", func(t *testing.T) {
g := MustCompile(test.pattern, test.delimiters...)
Expand Down
15 changes: 9 additions & 6 deletions match/row.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package match

import (
"fmt"
"unicode/utf8"
)

type Row struct {
Expand All @@ -23,19 +24,21 @@ func (self Row) matchAll(s string) bool {
for _, m := range self.Matchers {
length := m.Len()

var next, i int
for next = range s[idx:] {
i++
if i == length {
var runeCount, byteIdx int
var r rune
for _, r = range s[idx:] {
runeCount++
byteIdx += utf8.RuneLen(r)
if runeCount == length {
break
}
}

if i < length || !m.Match(s[idx:idx+next+1]) {
if runeCount < length || !m.Match(s[idx:idx+byteIdx]) {
return false
}

idx += next + 1
idx += byteIdx
}

return true
Expand Down