Skip to content
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
8 changes: 7 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func minimizeTreeAnyOf(tree *ast.Node) *ast.Node {
case len(anyOf) == 1 && anyOf[0].Kind != ast.KindNothing:
result = append(result, anyOf[0])
case len(anyOf) > 1:
result = append(result, ast.NewNode(ast.KindAnyOf, nil, anyOf...))
result = append(result, ast.NewNode(ast.KindAnyOf, tree.Value, anyOf...))
}

if commonRightCount > 0 {
Expand Down Expand Up @@ -469,6 +469,12 @@ func compile(tree *ast.Node, sep []rune) (m match.Matcher, err error) {
if err != nil {
return nil, err
}

v := tree.Value.(ast.AnyOf)
if v.Not {
return match.NewNotAnyOf(matchers...), nil
}

return match.NewAnyOf(matchers...), nil

case ast.KindPattern:
Expand Down
79 changes: 72 additions & 7 deletions compiler/compiler_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package compiler

import (
"reflect"
"testing"

"github.com/gobwas/glob/match"
"github.com/gobwas/glob/match/debug"
"github.com/gobwas/glob/syntax/ast"
"reflect"
"testing"
)

var separators = []rune{'.'}
Expand Down Expand Up @@ -414,7 +415,7 @@ func TestCompiler(t *testing.T) {
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"/"}),
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindAnyOf, ast.AnyOf{},
ast.NewNode(ast.KindText, ast.Text{"z"}),
ast.NewNode(ast.KindText, ast.Text{"ab"}),
),
Expand Down Expand Up @@ -523,9 +524,9 @@ func TestCompiler(t *testing.T) {
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindAnyOf, ast.AnyOf{},
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindAnyOf, ast.AnyOf{},
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
),
Expand All @@ -537,7 +538,7 @@ func TestCompiler(t *testing.T) {
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindAnyOf, ast.AnyOf{},
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindSingle, nil),
Expand Down Expand Up @@ -584,7 +585,7 @@ func TestCompiler(t *testing.T) {
},
{
ast: ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindAnyOf, nil,
ast.NewNode(ast.KindAnyOf, ast.AnyOf{},
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"abc"}),
ast.NewNode(ast.KindList, ast.List{Chars: "abc"}),
Expand All @@ -609,6 +610,70 @@ func TestCompiler(t *testing.T) {
}...,
),
},
// {!foo,bar}
{
ast: ast.NewNode(ast.KindPattern, nil,
&ast.Node{
Kind: ast.KindAnyOf,
Value: ast.AnyOf{
Not: true,
},
Children: []*ast.Node{
ast.NewNode(ast.KindPattern, nil, ast.NewNode(ast.KindText, ast.Text{"foo"})),
ast.NewNode(ast.KindPattern, nil, ast.NewNode(ast.KindText, ast.Text{"bar"})),
},
},
),
result: match.NewNotAnyOf(
match.NewText("foo"),
match.NewText("bar"),
),
},
// {!foo,bar}baz
{
ast: ast.NewNode(ast.KindPattern, nil,
&ast.Node{
Kind: ast.KindAnyOf,
Value: ast.AnyOf{
Not: true,
},
Children: []*ast.Node{
ast.NewNode(ast.KindPattern, nil, ast.NewNode(ast.KindText, ast.Text{"foo"})),
ast.NewNode(ast.KindPattern, nil, ast.NewNode(ast.KindText, ast.Text{"bar"})),
},
},
ast.NewNode(ast.KindText, ast.Text{"baz"}),
),
result: match.NewBTree(
match.NewText("baz"),
match.NewNotAnyOf(
match.NewText("foo"),
match.NewText("bar"),
),
nil,
),
},
// {!nginx*}
{
ast: ast.NewNode(ast.KindPattern, nil,
&ast.Node{
Kind: ast.KindAnyOf,
Value: ast.AnyOf{
Not: true,
},
Children: []*ast.Node{
ast.NewNode(ast.KindPattern, nil,
ast.NewNode(ast.KindText, ast.Text{"nginx"}),
ast.NewNode(ast.KindAny, nil),
),
},
},
),
sep: nil,
result: match.NewNotAnyOf(
match.NewPrefix("nginx"),
),
},
} {
m, err := Compile(test.ast, test.sep)
if err != nil {
Expand Down
14 changes: 14 additions & 0 deletions glob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,20 @@ func TestGlob(t *testing.T) {
glob(true, "{*.google.*,*.yandex.*}", "www.yandex.com", '.'),
glob(false, "{*.google.*,*.yandex.*}", "yandex.com", '.'),
glob(false, "{*.google.*,*.yandex.*}", "google.com", '.'),
glob(true, "{!*.google.*,*.yandex.*}", "google.com", '.'),
glob(true, "{!*.google.*,*.yandex.*}", "www.rambler.com", '.'),

glob(false, "{!abc,cde}", "cde"),
glob(true, "{!abc,cde}", "qwe"),
glob(true, "{!nginx,haproxy}_*", "caddy_abc"),
glob(false, "{!nginx,haproxy}_*", "nginx_abc"),
glob(true, "{!nginx,ha?roxy,caddy_[abc]}_server", "_server"),
glob(false, "{!ngin?,}*", "nginx"),
glob(true, "{!ngin*}-?", "caddy-1"),
glob(true, "{!*ngin?}-?", "caddy-2"),
glob(true, "{!{nginx,caddy}_*,nginx_123}", "haproxy_123"),
glob(true, "{!dev-?,stage-*}.example.{!org,net}", "www.example.com", '.'),
glob(false, "{!dev-?,stage-[0-9]}.example.{!org,net}", "stage-3.example.com", '.'),

glob(true, "{*.google.*,yandex.*}", "www.google.com", '.'),
glob(true, "{*.google.*,yandex.*}", "yandex.com", '.'),
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gobwas/glob

go 1.25.4
4 changes: 2 additions & 2 deletions match/any_of_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func TestAnyOfIndex(t *testing.T) {
[]int{1},
},
} {
everyOf := NewAnyOf(test.matchers...)
index, segments := everyOf.Index(test.fixture)
anyOf := NewAnyOf(test.matchers...)
index, segments := anyOf.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index)
}
Expand Down
49 changes: 49 additions & 0 deletions match/not_any_of.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package match

import "fmt"

type NotAnyOf struct {
Matchers Matchers
}

func NewNotAnyOf(m ...Matcher) NotAnyOf {
return NotAnyOf{Matchers(m)}
}

func (self NotAnyOf) Match(s string) bool {
for _, m := range self.Matchers {
if m.Match(s) {
return false
}
}
return true
}

func (self NotAnyOf) Index(s string) (int, []int) {
match := false

for _, m := range self.Matchers {
if idx, _ := m.Index(s); idx != -1 {
match = true
break // found at least one match for any of the internal matchers
}
}

if match {
return -1, nil
}

return 0, []int{len(s)}
}

func (self NotAnyOf) Len() (l int) {
// The length of a NotAnyOf is typically considered unknown (-1)
// because it negates a set of potentially variable-length patterns.
// Or, if all internal matchers have the same fixed length, you might reuse that.
// For simplicity, returning -1 is safer for negation.
return -1
}

func (self NotAnyOf) String() string {
return fmt.Sprintf("<!any_of:[%s]>", self.Matchers)
}
86 changes: 86 additions & 0 deletions match/not_any_of_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package match

import (
"reflect"
"testing"
)

func TestNotAnyOfIndex(t *testing.T) {
for id, test := range []struct {
matchers Matchers
fixture string
index int
segments []int
}{
{
Matchers{
NewText("foo"),
NewText("bar"),
},
"abc",
0,
[]int{3},
},
{
Matchers{
NewText("a"),
NewText("b"),
NewText("c"),
},
"b",
-1,
nil,
},
{
Matchers{
NewPrefix("caddy"),
},
"caddy-1",
-1,
nil,
},
{
Matchers{
NewText("long_string"),
NewText("short"),
},
"medium",
0,
[]int{6},
},
{
Matchers{
NewSuper(),
},
"any_string",
-1,
nil,
},
{
Matchers{
NewText("a"),
},
"",
0,
[]int{0},
},
{
Matchers{
NewText(""),
},
"",
-1,
nil,
},
} {
notAnyOf := NewNotAnyOf(test.matchers...)
index, segments := notAnyOf.Index(test.fixture)
if index != test.index {
t.Errorf("#%d unexpected index: exp: %d, act: %d for fixture '%s'", id, test.index, index, test.fixture)
}

if !reflect.DeepEqual(segments, test.segments) {
t.Errorf("#%d unexpected segments: exp: %v, act: %v for fixture '%s'", id, test.segments, segments, test.fixture)
}
}
}
4 changes: 4 additions & 0 deletions syntax/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ type Text struct {
Text string
}

type AnyOf struct {
Not bool
}

type Kind int

const (
Expand Down
15 changes: 12 additions & 3 deletions syntax/ast/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package ast
import (
"errors"
"fmt"
"github.com/gobwas/glob/syntax/lexer"
"unicode/utf8"

"github.com/gobwas/glob/syntax/lexer"
)

type Lexer interface {
Expand Down Expand Up @@ -62,7 +63,7 @@ func parserMain(tree *Node, lex Lexer) (parseFn, *Node, error) {
return parserRange, tree, nil

case lexer.TermsOpen:
a := NewNode(KindAnyOf, nil)
a := NewNode(KindAnyOf, AnyOf{})
Insert(tree, a)

p := NewNode(KindPattern, nil)
Expand All @@ -79,11 +80,19 @@ func parserMain(tree *Node, lex Lexer) (parseFn, *Node, error) {
case lexer.TermsClose:
return parserMain, tree.Parent.Parent, nil

case lexer.Not:
if tree.Parent != nil && tree.Parent.Kind == KindAnyOf {
v := tree.Parent.Value.(AnyOf)
v.Not = true
tree.Parent.Value = v
}

return parserMain, tree, nil

default:
return nil, tree, fmt.Errorf("unexpected token: %s", token)
}
}
return nil, tree, fmt.Errorf("unknown error")
}

func parserRange(tree *Node, lex Lexer) (parseFn, *Node, error) {
Expand Down
Loading