forked from mirror/glob
little bit fixes
This commit is contained in:
parent
ec7fba7e40
commit
dfb03553a2
48
match/row.go
48
match/row.go
|
@ -2,7 +2,6 @@ package match
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"unicode/utf8"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Row struct {
|
type Row struct {
|
||||||
|
@ -13,23 +12,40 @@ type Row struct {
|
||||||
func (self Row) matchAll(s string) bool {
|
func (self Row) matchAll(s string) bool {
|
||||||
var idx int
|
var idx int
|
||||||
for _, m := range self.Matchers {
|
for _, m := range self.Matchers {
|
||||||
l := m.Len()
|
length := m.Len()
|
||||||
if !m.Match(s[idx : idx+l]) {
|
|
||||||
|
var next, i int
|
||||||
|
for next = range s[idx:] {
|
||||||
|
i++
|
||||||
|
if i == length {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if i < length || !m.Match(s[idx:idx+next+1]) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
idx += l
|
idx += next + 1
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Row) Match(s string) bool {
|
func (self Row) lenOk(s string) bool {
|
||||||
if utf8.RuneCountInString(s) < self.RunesLength {
|
var i int
|
||||||
return false
|
for range s {
|
||||||
|
i++
|
||||||
|
if i >= self.RunesLength {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return self.matchAll(s)
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (self Row) Match(s string) bool {
|
||||||
|
return self.lenOk(s) && self.matchAll(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Row) Len() (l int) {
|
func (self Row) Len() (l int) {
|
||||||
|
@ -37,20 +53,20 @@ func (self Row) Len() (l int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self Row) Index(s string) (int, []int) {
|
func (self Row) Index(s string) (int, []int) {
|
||||||
l := utf8.RuneCountInString(s)
|
if !self.lenOk(s) {
|
||||||
if l < self.RunesLength {
|
|
||||||
return -1, nil
|
return -1, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range s {
|
for i := range s {
|
||||||
sub := s[i:]
|
// this is not strict check but useful
|
||||||
if self.matchAll(sub) {
|
// when glob will be refactored for usage with []rune
|
||||||
return i, []int{self.RunesLength}
|
// it will be better
|
||||||
|
if len(s[i:]) < self.RunesLength {
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
l -= 1
|
if self.matchAll(s[i:]) {
|
||||||
if l < self.RunesLength {
|
return i, []int{self.RunesLength}
|
||||||
return -1, nil
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,6 +5,20 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func BenchmarkRowIndex(b *testing.B) {
|
||||||
|
m := Row{
|
||||||
|
Matchers: Matchers{
|
||||||
|
NewText("abc"),
|
||||||
|
NewText("def"),
|
||||||
|
Single{},
|
||||||
|
},
|
||||||
|
RunesLength: 7,
|
||||||
|
}
|
||||||
|
for i := 0; i < b.N; i++ {
|
||||||
|
m.Index("abcdefghijk")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestRowIndex(t *testing.T) {
|
func TestRowIndex(t *testing.T) {
|
||||||
for id, test := range []struct {
|
for id, test := range []struct {
|
||||||
matchers Matchers
|
matchers Matchers
|
||||||
|
|
|
@ -100,7 +100,7 @@ Run `go test -bench=.` from source root to see the benchmarks:
|
||||||
|
|
||||||
Pattern | Fixture | Operations | Speed (ns/op)
|
Pattern | Fixture | Operations | Speed (ns/op)
|
||||||
--------|---------|------------|--------------
|
--------|---------|------------|--------------
|
||||||
`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | 2000000 | 549
|
`[a-z][!a-x]*cat*[h][!b]*eyes*` | `my cat has very bright eyes` | 2000000 | 527
|
||||||
`https://*.google.*` | `https://account.google.com` | 10000000 | 121
|
`https://*.google.*` | `https://account.google.com` | 10000000 | 121
|
||||||
`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | 10000000 | 167
|
`{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}` | `http://yahoo.com` | 10000000 | 167
|
||||||
`{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | 50000000 | 24.7
|
`{https://*gobwas.com,http://exclude.gobwas.com}` | `https://safe.gobwas.com` | 50000000 | 24.7
|
||||||
|
|
Loading…
Reference in New Issue