use memchr::memchr3;
use memchr::memmem::Finder;
use regex::bytes::Regex;
use regex::bytes::RegexBuilder;
use vortex_error::VortexResult;
use vortex_error::vortex_err;
pub(crate) enum LikePattern {
Eq(Vec<u8>),
StartsWith(Vec<u8>),
EndsWith(Vec<u8>),
Contains(Box<Finder<'static>>, usize),
IEqAscii(Vec<u8>),
IStartsWithAscii(Vec<u8>),
IEndsWithAscii(Vec<u8>),
Regex(Regex),
}
impl LikePattern {
pub(crate) fn compile(
pattern: &str,
case_insensitive: bool,
ascii_haystack: bool,
) -> VortexResult<Self> {
let prefix = || pattern.strip_suffix('%').and_then(unescape_affix);
let suffix = || pattern.strip_prefix('%').and_then(unescape);
let infix = || {
pattern
.strip_prefix('%')
.and_then(|pattern| pattern.strip_suffix('%'))
.and_then(unescape_affix)
};
if case_insensitive {
if ascii_haystack && pattern.is_ascii() {
if let Some(literal) = unescape(pattern) {
return Ok(Self::IEqAscii(literal));
} else if let Some(prefix) = prefix() {
return Ok(Self::IStartsWithAscii(prefix));
} else if let Some(suffix) = suffix() {
return Ok(Self::IEndsWithAscii(suffix));
}
}
return Ok(Self::Regex(regex_like(pattern, true)?));
}
Ok(if let Some(literal) = unescape(pattern) {
Self::Eq(literal)
} else if let Some(prefix) = prefix() {
Self::StartsWith(prefix)
} else if let Some(suffix) = suffix() {
Self::EndsWith(suffix)
} else if let Some(infix) = infix() {
let len = infix.len();
Self::Contains(Box::new(Finder::new(&infix).into_owned()), len)
} else {
Self::Regex(regex_like(pattern, false)?)
})
}
#[inline]
pub(crate) fn matches(&self, haystack: &[u8]) -> bool {
match self {
Self::Eq(needle) => needle == haystack,
Self::StartsWith(needle) => {
needle.len() <= haystack.len() && &haystack[..needle.len()] == needle.as_slice()
}
Self::EndsWith(needle) => {
needle.len() <= haystack.len()
&& &haystack[haystack.len() - needle.len()..] == needle.as_slice()
}
Self::Contains(finder, needle_len) => {
haystack.len() >= *needle_len && finder.find(haystack).is_some()
}
Self::IEqAscii(needle) => haystack.eq_ignore_ascii_case(needle),
Self::IStartsWithAscii(needle) => {
needle.len() <= haystack.len()
&& haystack[..needle.len()].eq_ignore_ascii_case(needle)
}
Self::IEndsWithAscii(needle) => {
needle.len() <= haystack.len()
&& haystack[haystack.len() - needle.len()..].eq_ignore_ascii_case(needle)
}
Self::Regex(regex) => regex.is_match(haystack),
}
}
}
fn unescape(pattern: &str) -> Option<Vec<u8>> {
unescape_with_trailing(pattern).map(|(literal, _)| literal)
}
fn unescape_affix(pattern: &str) -> Option<Vec<u8>> {
unescape_with_trailing(pattern)
.and_then(|(literal, trailing_backslash)| (!trailing_backslash).then_some(literal))
}
fn unescape_with_trailing(pattern: &str) -> Option<(Vec<u8>, bool)> {
let mut remaining = pattern.as_bytes();
let mut literal: Option<Vec<u8>> = None;
while let Some(idx) = memchr3(b'%', b'_', b'\\', remaining) {
if remaining[idx] != b'\\' {
return None;
}
let literal = literal.get_or_insert_with(|| Vec::with_capacity(pattern.len()));
literal.extend_from_slice(&remaining[..idx]);
remaining = &remaining[idx + 1..];
let Some((&escaped, rest)) = remaining.split_first() else {
literal.push(b'\\');
return Some((std::mem::take(literal), true));
};
literal.push(escaped);
remaining = rest;
}
let mut literal = literal.unwrap_or_else(|| Vec::with_capacity(remaining.len()));
literal.extend_from_slice(remaining);
Some((literal, false))
}
fn regex_like(pattern: &str, case_insensitive: bool) -> VortexResult<Regex> {
let mut result = String::with_capacity(pattern.len() * 2);
let mut chars_iter = pattern.chars().peekable();
match chars_iter.peek() {
Some('%') => {
chars_iter.next();
}
_ => result.push('^'),
};
while let Some(c) = chars_iter.next() {
match c {
'\\' => {
match chars_iter.peek() {
Some(&next) => {
if regex_syntax::is_meta_character(next) {
result.push('\\');
}
result.push(next);
chars_iter.next();
}
None => {
result.push('\\');
result.push('\\');
}
}
}
'%' => result.push_str(".*"),
'_' => result.push('.'),
c => {
if regex_syntax::is_meta_character(c) {
result.push('\\');
}
result.push(c);
}
}
}
if result.ends_with(".*") {
result.pop();
result.pop();
} else {
result.push('$');
}
RegexBuilder::new(&result)
.case_insensitive(case_insensitive)
.dot_matches_new_line(true)
.build()
.map_err(|e| vortex_err!("Unable to build regex from LIKE pattern: {e}"))
}
#[cfg(test)]
mod tests {
use super::*;
fn like(pattern: &str) -> LikePattern {
LikePattern::compile(pattern, false, false).unwrap()
}
#[test]
fn compile_fast_paths() {
assert!(matches!(like("foo"), LikePattern::Eq(_)));
assert!(matches!(like("foo%"), LikePattern::StartsWith(_)));
assert!(matches!(like("%foo"), LikePattern::EndsWith(_)));
assert!(matches!(like("%foo%"), LikePattern::Contains(..)));
assert!(matches!(like("%"), LikePattern::StartsWith(_)));
assert!(matches!(like("%%"), LikePattern::Contains(..)));
assert!(matches!(like("f%o"), LikePattern::Regex(_)));
assert!(matches!(like("f_o"), LikePattern::Regex(_)));
for (pattern, matches, misses) in [
("foo", "foo", Some("foobar")),
("foo%", "foobar", Some("xfoo")),
("%foo", "xfoo", Some("foobar")),
("%foo%", "xfooy", Some("bar")),
("%", "", None),
("%%", "anything", None),
("f%o", "frodo", Some("frodo!")),
("f_o", "foo", Some("fooo")),
(r"foo\%", "foo%", Some("foobar")),
] {
let compiled = like(pattern);
assert!(compiled.matches(matches.as_bytes()), "{pattern}");
if let Some(misses) = misses {
assert!(!compiled.matches(misses.as_bytes()), "{pattern}");
}
}
}
#[test]
fn compile_escaped_fast_paths() {
assert!(matches!(like(r"foo\%"), LikePattern::Eq(_)));
assert!(matches!(like(r"a\_b%"), LikePattern::StartsWith(_)));
assert!(matches!(like(r"%a\%b"), LikePattern::EndsWith(_)));
assert!(matches!(like(r"%foo\%"), LikePattern::EndsWith(_)));
assert!(matches!(like(r"%a\_b%"), LikePattern::Contains(..)));
}
#[test]
fn fast_paths_agree_with_regex() {
let patterns = [
r"foo\%",
r"a\_b%",
r"%a\%b",
r"%foo\%",
r"%a\_b%",
r"\\%",
r"%\\",
r"\\",
r"\%%",
r"%\%",
r"\a%",
r"%x\",
r"\é%",
"trailing\\",
];
let haystacks = [
"",
"foo%",
"foox",
"a_b",
"a_bc",
"axbc",
"xa%b",
"xaxb",
"xfoo%",
"xa_by",
"\\",
"\\abc",
"abc\\",
"%",
"%x",
"ax",
"x\\",
"éa",
"trailing\\",
];
for pattern in patterns {
let compiled = like(pattern);
let regex = regex_like(pattern, false).unwrap();
for haystack in haystacks {
assert_eq!(
compiled.matches(haystack.as_bytes()),
regex.is_match(haystack.as_bytes()),
"{pattern} on {haystack}"
);
}
}
}
#[test]
fn regex_translation() {
let cases = [
(r"%foobar%", "xfoobary", "foo"),
(r"foo%bar", "foo\nbar", "xfoobar"),
(r"foo_bar", "foo£bar", "fooxxbar"),
(r"\%\_", "%_", "xx"),
(r"\a", "a", "aa"),
(r"\\%", r"\abc", "abc"),
(r"\\a", r"\a", "a"),
(r".", ".", "x"),
(r"$", "$", "x"),
(r"\\", r"\", ""),
];
for (pattern, matches, misses) in cases {
let regex = regex_like(pattern, false).unwrap();
assert!(regex.is_match(matches.as_bytes()), "{pattern}");
assert!(!regex.is_match(misses.as_bytes()), "{pattern}");
}
}
#[test]
fn matches_wildcards() {
assert!(like("h_llo w%d").matches(b"hello world"));
assert!(like("h_llo w%d").matches("h\u{00a3}llo wd".as_bytes()));
assert!(!like("h_llo w%d").matches(b"hxxllo world"));
assert!(like("hello%world").matches(b"hello\nworld"));
assert!(like("_").matches("\u{00a3}".as_bytes()));
assert!(!like("_").matches("\u{00a3}x".as_bytes()));
}
#[test]
fn matches_escapes() {
assert!(like(r"100\%").matches(b"100%"));
assert!(!like(r"100\%").matches(b"100x"));
assert!(like(r"a\_b").matches(b"a_b"));
assert!(!like(r"a\_b").matches(b"axb"));
assert!(like("trailing\\").matches(b"trailing\\"));
}
#[test]
fn matches_case_insensitive() {
let ilike = |pattern: &str| LikePattern::compile(pattern, true, false).unwrap();
assert!(ilike("hello%").matches(b"HELLO WORLD"));
assert!(ilike("%WORLD").matches(b"hello world"));
assert!(ilike("k").matches("\u{212a}".as_bytes()));
assert!(ilike("\u{03c3}").matches("\u{03a3}".as_bytes()));
assert!(ilike("\u{03c3}").matches("\u{03c2}".as_bytes()));
let ascii = |pattern: &str| LikePattern::compile(pattern, true, true).unwrap();
assert!(matches!(ascii("abc"), LikePattern::IEqAscii(_)));
assert!(matches!(ascii("abc%"), LikePattern::IStartsWithAscii(_)));
assert!(matches!(ascii("%abc"), LikePattern::IEndsWithAscii(_)));
assert!(ascii("abc").matches(b"AbC"));
assert!(ascii("abc%").matches(b"ABCDEF"));
assert!(ascii("%def").matches(b"ABCDEF"));
assert!(!ascii("abc").matches(b"AbCd"));
}
}