Skip to content
Merged
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
2 changes: 1 addition & 1 deletion checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func (v *Checker) binaryNode(node *ast.BinaryNode) Nature {
return v.error(node, err.Error())
}
}
if l.IsString() && r.IsString() {
if (l.IsString() || l.IsByteSlice()) && r.IsString() {
return v.config.NtCache.FromType(boolType)
}
if l.MaybeCompatible(&v.config.NtCache, r, StringCheck) {
Expand Down
15 changes: 10 additions & 5 deletions checker/nature/nature.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import (
)

var (
intType = reflect.TypeOf(0)
floatType = reflect.TypeOf(float64(0))
arrayType = reflect.TypeOf([]any{})
timeType = reflect.TypeOf(time.Time{})
durationType = reflect.TypeOf(time.Duration(0))
intType = reflect.TypeOf(0)
floatType = reflect.TypeOf(float64(0))
arrayType = reflect.TypeOf([]any{})
byteSliceType = reflect.TypeOf([]byte{})
timeType = reflect.TypeOf(time.Time{})
durationType = reflect.TypeOf(time.Duration(0))

builtinInt = map[reflect.Type]struct{}{
reflect.TypeOf(int(0)): {},
Expand Down Expand Up @@ -502,6 +503,10 @@ func (n *Nature) IsString() bool {
return n.Kind == reflect.String
}

func (n *Nature) IsByteSlice() bool {
return n.Type == byteSliceType
}

func (n *Nature) IsArray() bool {
return n.Kind == reflect.Slice || n.Kind == reflect.Array
}
Expand Down
14 changes: 12 additions & 2 deletions vm/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
vm.push(false)
break
}
match, err := regexp.MatchString(b.(string), a.(string))
var match bool
var err error
if s, ok := a.(string); ok {
match, err = regexp.MatchString(b.(string), s)
} else {
match, err = regexp.Match(b.(string), a.([]byte))
}
if err != nil {
panic(err)
}
Expand All @@ -312,7 +318,11 @@ func (vm *VM) Run(program *Program, env any) (_ any, err error) {
break
}
r := program.Constants[arg].(*regexp.Regexp)
vm.push(r.MatchString(a.(string)))
if s, ok := a.(string); ok {
vm.push(r.MatchString(s))
} else {
vm.push(r.Match(a.([]byte)))
}

case OpContains:
b := vm.pop()
Expand Down
12 changes: 12 additions & 0 deletions vm/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,18 @@ func TestVM_OpcodeOperations(t *testing.T) {
expr: `"hello123" matches "^hello\\d+$"`,
want: true,
},
{
name: "byte slice matches regex",
expr: `b matches "^hello\\d+$"`,
env: map[string]any{"b": []byte("hello123")},
want: true,
},
{
name: "byte slice matches dynamic regex",
expr: `b matches pattern`,
env: map[string]any{"b": []byte("hello123"), "pattern": "^hello\\d+$"},
want: true,
},

// Data Structure Operations
{
Expand Down
Loading