forked from gobwas/glob
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid btree Index() call on pattern with separators.
To avoid hard Index()'ing of given text with btree matcher we implement an prefix_any and suffix_any matchers that can work well in many cases. BTree matcher Index() will be implemented in upcoming commits to prevent same bugs. Fixes gobwas#23
- Loading branch information
Showing
8 changed files
with
258 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
package match | ||
|
||
import ( | ||
"fmt" | ||
) | ||
import "fmt" | ||
|
||
type AnyOf struct { | ||
Matchers Matchers | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package match | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"unicode/utf8" | ||
|
||
sutil "github.com/gobwas/glob/util/strings" | ||
) | ||
|
||
type PrefixAny struct { | ||
Prefix string | ||
Separators []rune | ||
} | ||
|
||
func NewPrefixAny(s string, sep []rune) PrefixAny { | ||
return PrefixAny{s, sep} | ||
} | ||
|
||
func (self PrefixAny) Index(s string) (int, []int) { | ||
idx := strings.Index(s, self.Prefix) | ||
if idx == -1 { | ||
return -1, nil | ||
} | ||
|
||
n := len(self.Prefix) | ||
sub := s[idx+n:] | ||
i := sutil.IndexAnyRunes(sub, self.Separators) | ||
if i > -1 { | ||
sub = sub[:i] | ||
} | ||
|
||
seg := acquireSegments(len(sub) + 1) | ||
seg = append(seg, n) | ||
for i, r := range sub { | ||
seg = append(seg, n+i+utf8.RuneLen(r)) | ||
} | ||
|
||
return idx, seg | ||
} | ||
|
||
func (self PrefixAny) Len() int { | ||
return lenNo | ||
} | ||
|
||
func (self PrefixAny) Match(s string) bool { | ||
if !strings.HasPrefix(s, self.Prefix) { | ||
return false | ||
} | ||
return sutil.IndexAnyRunes(s[len(self.Prefix):], self.Separators) == -1 | ||
} | ||
|
||
func (self PrefixAny) String() string { | ||
return fmt.Sprintf("<prefix_any:%s![%s]>", self.Prefix, string(self.Separators)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package match | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestPrefixAnyIndex(t *testing.T) { | ||
for id, test := range []struct { | ||
prefix string | ||
separators []rune | ||
fixture string | ||
index int | ||
segments []int | ||
}{ | ||
{ | ||
"ab", | ||
[]rune{'.'}, | ||
"ab", | ||
0, | ||
[]int{2}, | ||
}, | ||
{ | ||
"ab", | ||
[]rune{'.'}, | ||
"abc", | ||
0, | ||
[]int{2, 3}, | ||
}, | ||
{ | ||
"ab", | ||
[]rune{'.'}, | ||
"qw.abcd.efg", | ||
3, | ||
[]int{2, 3, 4}, | ||
}, | ||
} { | ||
p := NewPrefixAny(test.prefix, test.separators) | ||
index, segments := p.Index(test.fixture) | ||
if index != test.index { | ||
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) | ||
} | ||
if !reflect.DeepEqual(segments, test.segments) { | ||
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package match | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
sutil "github.com/gobwas/glob/util/strings" | ||
) | ||
|
||
type SuffixAny struct { | ||
Suffix string | ||
Separators []rune | ||
} | ||
|
||
func NewSuffixAny(s string, sep []rune) SuffixAny { | ||
return SuffixAny{s, sep} | ||
} | ||
|
||
func (self SuffixAny) Index(s string) (int, []int) { | ||
idx := strings.Index(s, self.Suffix) | ||
if idx == -1 { | ||
return -1, nil | ||
} | ||
|
||
i := sutil.LastIndexAnyRunes(s[:idx], self.Separators) + 1 | ||
|
||
return i, []int{idx + len(self.Suffix) - i} | ||
} | ||
|
||
func (self SuffixAny) Len() int { | ||
return lenNo | ||
} | ||
|
||
func (self SuffixAny) Match(s string) bool { | ||
if !strings.HasSuffix(s, self.Suffix) { | ||
return false | ||
} | ||
return sutil.IndexAnyRunes(s[:len(s)-len(self.Suffix)], self.Separators) == -1 | ||
} | ||
|
||
func (self SuffixAny) String() string { | ||
return fmt.Sprintf("<suffix_any:![%s]%s>", string(self.Separators), self.Suffix) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package match | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestSuffixAnyIndex(t *testing.T) { | ||
for id, test := range []struct { | ||
suffix string | ||
separators []rune | ||
fixture string | ||
index int | ||
segments []int | ||
}{ | ||
{ | ||
"ab", | ||
[]rune{'.'}, | ||
"ab", | ||
0, | ||
[]int{2}, | ||
}, | ||
{ | ||
"ab", | ||
[]rune{'.'}, | ||
"cab", | ||
0, | ||
[]int{3}, | ||
}, | ||
{ | ||
"ab", | ||
[]rune{'.'}, | ||
"qw.cdab.efg", | ||
3, | ||
[]int{4}, | ||
}, | ||
} { | ||
p := NewSuffixAny(test.suffix, test.separators) | ||
index, segments := p.Index(test.fixture) | ||
if index != test.index { | ||
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) | ||
} | ||
if !reflect.DeepEqual(segments, test.segments) { | ||
t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters