Skip to content

Commit

Permalink
Add and fix another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
tie authored and asdine committed Oct 25, 2020
1 parent 6b1c0a7 commit 71f5462
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
30 changes: 16 additions & 14 deletions sql/query/glob/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,20 @@ func MatchLike(pattern, s string) bool {
var w, t string // backtracking state

for len(s) != 0 {
if len(pattern) == 0 {
return prevEscape
}

// Read (and consume) the next character from the input pattern.
var p rune
if len(pattern) == 0 {
goto backtrack
}
p, pattern = readRune(pattern)

backtrack:
// There are now 4 possibilities:
//
// 1. p is an unescaped match-all character "%",
// 2. p is an unescaped match-one character "_",
// 3. p is an unescaped escape character, or
// 4. p is to be handled as an ordinary character
//
loop:
if p == matchAll && !prevEscape {
// Case 1.
var c byte
Expand Down Expand Up @@ -135,17 +133,21 @@ func MatchLike(pattern, s string) bool {
var r rune
r, s = readRune(s)
if !equalFold(p, r) {
if len(w) == 0 {
// Nothing to backtrack.
return false
}
// Keep the pattern and skip rune in input.
// Note that we only backtrack to matchAll.
p, pattern = matchAll, w
s = skipRune(t)
goto backtrack
}
}
continue

backtrack:
if len(w) == 0 {
// Nothing to backtrack.
return prevEscape
}
// Keep the pattern and skip rune in input.
// Note that we only backtrack to matchAll.
p, pattern = matchAll, w
s = skipRune(t)
goto loop
}

// Check that the rest of the pattern is matchAll.
Expand Down
1 change: 1 addition & 0 deletions sql/query/glob/like_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func TestMatchLike(t *testing.T) {
{"ab", "_%_", true},
{"ab", "%_%_%", true},
{"aaaa", "_aa%", true},
{"aaaa", "%aa_", true},
{"abc", "_%%_%_", true},
{"abc", "_%%_%&_", false},
{"abcd", "_b%__", true},
Expand Down

0 comments on commit 71f5462

Please sign in to comment.