Skip to content

Commit

Permalink
Tune, new feature test
Browse files Browse the repository at this point in the history
  • Loading branch information
s.kamardin committed Jan 14, 2016
1 parent c65eb46 commit d2a191e
Show file tree
Hide file tree
Showing 13 changed files with 142 additions and 117 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ glob.iml
.idea
*.cpu
*.mem
*.test
*.test
*.dot
*.png
*.svg
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sudo: false
language: go

go:
- 1.5.1
- 1.5.3

script:
- go test -v ./...
8 changes: 4 additions & 4 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func optimize(matcher match.Matcher) match.Matcher {
m.Left = optimize(m.Left)
m.Right = optimize(m.Right)

r, ok := m.Value.(match.Raw)
r, ok := m.Value.(match.Text)
if !ok {
return m
}
Expand All @@ -26,7 +26,7 @@ func optimize(matcher match.Matcher) match.Matcher {
rightNil := m.Right == nil

if leftNil && rightNil {
return match.NewRaw(r.Str)
return match.NewText(r.Str)
}

_, leftSuper := m.Left.(match.Super)
Expand Down Expand Up @@ -325,7 +325,7 @@ func do(node node, s string) (m match.Matcher, err error) {
m = match.Single{s}

case *nodeText:
m = match.NewRaw(n.text)
m = match.NewText(n.text)

default:
return nil, fmt.Errorf("could not compile tree: unknown node type")
Expand Down Expand Up @@ -424,7 +424,7 @@ func do2(node node, s string) ([]match.Matcher, error) {
result = append(result, match.Single{s})

case *nodeText:
result = append(result, match.NewRaw(n.text))
result = append(result, match.NewText(n.text))

default:
return nil, fmt.Errorf("could not compile tree: unknown node type")
Expand Down
46 changes: 23 additions & 23 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func TestCompileMatchers(t *testing.T) {
[]match.Matcher{
match.Super{},
match.Single{separators},
match.Raw{"c", 1},
match.NewText("c"),
},
match.NewBTree(
match.Raw{"c", 1},
match.NewText("c"),
match.NewBTree(
match.Single{separators},
match.Super{},
Expand All @@ -89,11 +89,11 @@ func TestCompileMatchers(t *testing.T) {
{
[]match.Matcher{
match.Any{},
match.Raw{"c", 1},
match.NewText("c"),
match.Any{},
},
match.NewBTree(
match.Raw{"c", 1},
match.NewText("c"),
match.Any{},
match.Any{},
),
Expand All @@ -102,17 +102,17 @@ func TestCompileMatchers(t *testing.T) {
[]match.Matcher{
match.Range{'a', 'c', true},
match.List{"zte", false},
match.Raw{"c", 1},
match.NewText("c"),
match.Single{},
},
match.Row{
Matchers: match.Matchers{
match.Range{'a', 'c', true},
match.List{"zte", false},
match.Raw{"c", 1},
match.NewText("c"),
match.Single{},
},
Length: 4,
RunesLength: 4,
},
},
} {
Expand All @@ -137,7 +137,7 @@ func TestConvertMatchers(t *testing.T) {
[]match.Matcher{
match.Range{'a', 'c', true},
match.List{"zte", false},
match.Raw{"c", 1},
match.NewText("c"),
match.Single{},
match.Any{},
},
Expand All @@ -146,10 +146,10 @@ func TestConvertMatchers(t *testing.T) {
Matchers: match.Matchers{
match.Range{'a', 'c', true},
match.List{"zte", false},
match.Raw{"c", 1},
match.NewText("c"),
match.Single{},
},
Length: 4,
RunesLength: 4,
},
match.Any{},
},
Expand All @@ -158,7 +158,7 @@ func TestConvertMatchers(t *testing.T) {
[]match.Matcher{
match.Range{'a', 'c', true},
match.List{"zte", false},
match.Raw{"c", 1},
match.NewText("c"),
match.Single{},
match.Any{},
match.Single{},
Expand All @@ -170,9 +170,9 @@ func TestConvertMatchers(t *testing.T) {
Matchers: match.Matchers{
match.Range{'a', 'c', true},
match.List{"zte", false},
match.Raw{"c", 1},
match.NewText("c"),
},
Length: 3,
RunesLength: 3,
},
match.Min{3},
},
Expand Down Expand Up @@ -208,7 +208,7 @@ func TestCompiler(t *testing.T) {
}{
{
ast: pattern(&nodeText{text: "abc"}),
result: match.Raw{"abc", 3},
result: match.NewText("abc"),
},
{
ast: pattern(&nodeAny{}),
Expand Down Expand Up @@ -261,10 +261,10 @@ func TestCompiler(t *testing.T) {
result: match.NewBTree(
match.Row{
Matchers: match.Matchers{
match.Raw{"abc", 3},
match.NewText("abc"),
match.Single{separators},
},
Length: 4,
RunesLength: 4,
},
match.Any{separators},
nil,
Expand All @@ -277,10 +277,10 @@ func TestCompiler(t *testing.T) {
match.Row{
Matchers: match.Matchers{
match.Single{separators},
match.Raw{"abc", 3},
match.NewText("abc"),
match.Single{separators},
},
Length: 5,
RunesLength: 5,
},
match.Super{},
nil,
Expand All @@ -306,30 +306,30 @@ func TestCompiler(t *testing.T) {
ast: pattern(&nodeAny{}, &nodeAny{}, &nodeAny{}, &nodeText{text: "abc"}, &nodeAny{}, &nodeAny{}),
sep: separators,
result: match.NewBTree(
match.Raw{"abc", 3},
match.NewText("abc"),
match.Any{separators},
match.Any{separators},
),
},
{
ast: pattern(&nodeSuper{}, &nodeSingle{}, &nodeText{text: "abc"}, &nodeSuper{}, &nodeSingle{}),
result: match.NewBTree(
match.Raw{"abc", 3},
match.NewText("abc"),
match.Min{1},
match.Min{1},
),
},
{
ast: pattern(anyOf(&nodeText{text: "abc"})),
result: match.AnyOf{match.Matchers{
match.Raw{"abc", 3},
match.NewText("abc"),
}},
},
{
ast: pattern(anyOf(pattern(anyOf(pattern(&nodeText{text: "abc"}))))),
result: match.AnyOf{match.Matchers{
match.AnyOf{match.Matchers{
match.Raw{"abc", 3},
match.NewText("abc"),
}},
}},
},
Expand All @@ -345,7 +345,7 @@ func TestCompiler(t *testing.T) {
match.Range{Lo: 'a', Hi: 'z'},
match.Range{Lo: 'a', Hi: 'x', Not: true},
},
Length: 2,
RunesLength: 2,
},
nil,
match.Super{},
Expand Down
26 changes: 17 additions & 9 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const (
pattern_multiple = "https://*.google.*"
fixture_multiple = "https://account.google.com"

pattern_alternatives = "{https://*.google.*,*yahoo.*}"
pattern_alternatives = "{https://*.google.*,*yandex.*,*yahoo.*,*mail.ru}"
fixture_alternatives = "http://yahoo.com"

pattern_prefix = "abc*"
Expand Down Expand Up @@ -81,14 +81,22 @@ func TestCompilePattern(t *testing.T) {
sep string
exp match.Matcher
}{
// {
// pattern: "left*??B*abcd*[!b]??*abc*right",
// exp: match.Raw{"t"},
// },
// {
// pattern: "abc*??def",
// exp: match.Raw{"t"},
// },
// {
// pattern: "left*??B*abcd*[!b]??*abc*right",
// exp: match.Raw{"t"},
// },
// {
// pattern: "abc*??def",
// exp: match.Raw{"t"},
// },
{
pattern: "{abc[abc]ghi,abc[def]ghi}",
exp: match.NewBTree(
match.AnyOf{match.Matchers{match.List{"abc", false}, match.List{"qwe", false}}},
match.NewText("abc"),
match.NewText("ghi"),
),
},
} {
glob, err := Compile(test.pattern, test.sep)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions match/any_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestAnyOfIndex(t *testing.T) {
{
Matchers{
Any{},
Raw{"b"},
Raw{"c"},
Text{"b"},
Text{"c"},
},
"abc",
0,
Expand Down
45 changes: 25 additions & 20 deletions match/btree.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import (
)

type BTree struct {
Value, Left, Right Matcher
VLen, LLen, RLen int
Length int
Value Matcher
Left Matcher
Right Matcher
ValueLengthRunes int
LeftLengthRunes int
RightLengthRunes int
LengthRunes int
}

func NewBTree(Value, Left, Right Matcher) (tree BTree) {
Expand All @@ -17,33 +21,33 @@ func NewBTree(Value, Left, Right Matcher) (tree BTree) {
tree.Right = Right

lenOk := true
if tree.VLen = Value.Len(); tree.VLen == -1 {
if tree.ValueLengthRunes = Value.Len(); tree.ValueLengthRunes == -1 {
lenOk = false
}

if Left != nil {
if tree.LLen = Left.Len(); tree.LLen == -1 {
if tree.LeftLengthRunes = Left.Len(); tree.LeftLengthRunes == -1 {
lenOk = false
}
}

if Right != nil {
if tree.RLen = Right.Len(); tree.RLen == -1 {
if tree.RightLengthRunes = Right.Len(); tree.RightLengthRunes == -1 {
lenOk = false
}
}

if lenOk {
tree.Length = tree.LLen + tree.VLen + tree.RLen
tree.LengthRunes = tree.LeftLengthRunes + tree.ValueLengthRunes + tree.RightLengthRunes
} else {
tree.Length = -1
tree.LengthRunes = -1
}

return tree
}

func (self BTree) Len() int {
return self.Length
return self.LengthRunes
}

// todo?
Expand All @@ -54,27 +58,33 @@ func (self BTree) Index(s string) (int, []int) {
func (self BTree) Match(s string) bool {
inputLen := len(s)

if self.Length != -1 && self.Length > inputLen {
// self.Length, self.RLen and self.LLen are values meaning the length of runes for each part
// here we manipulating byte length for better optimizations
// but these checks still works, cause minLen of 1-rune string is 1 byte.
if self.LengthRunes != -1 && self.LengthRunes > inputLen {
return false
}

// try to cut unnecessary parts
// by knowledge of length of right and left part
var offset, limit int
if self.LLen >= 0 {
offset = self.LLen
if self.LeftLengthRunes >= 0 {
offset = self.LeftLengthRunes
}
if self.RLen >= 0 {
limit = inputLen - self.RLen
if self.RightLengthRunes >= 0 {
limit = inputLen - self.RightLengthRunes
} else {
limit = inputLen
}

for offset < limit {
// search for matching part in substring
index, segments := self.Value.Index(s[offset:limit])
if index == -1 {
return false
}

l := string(s[:offset+index])
l := s[:offset+index]
var left bool
if self.Left != nil {
left = self.Left.Match(l)
Expand All @@ -86,12 +96,7 @@ func (self BTree) Match(s string) bool {
for i := len(segments) - 1; i >= 0; i-- {
length := segments[i]

if self.RLen >= 0 && inputLen-(offset+index+length) != self.RLen {
continue
}

var right bool

var r string
// if there is no string for the right branch
if inputLen <= offset+index+length {
Expand Down
Loading

0 comments on commit d2a191e

Please sign in to comment.