const ANY: &str = "(.|\\n|\\r)";
pub fn from_like(like: &str, escape: Option<char>) -> Option<String> {
let mut out = String::with_capacity(like.len() * 2);
let mut chars = like.chars();
while let Some(c) = chars.next() {
match c {
_ if Some(c) == escape => push_literal(&mut out, chars.next()?),
'%' => {
out.push_str(ANY);
out.push('*');
}
'_' => out.push_str(ANY),
_ => push_literal(&mut out, c),
}
}
Some(out)
}
pub fn from_postgres_regex(re: &str) -> Option<String> {
let chars: Vec<char> = re.chars().collect();
let mut parser = Regex { s: &chars, at: 0, depth: 0 };
let mut branches = Vec::new();
loop {
let (start, body, end) = parser.branch(true)?;
let pad = |anchored: bool| if anchored { String::new() } else { format!("{ANY}*") };
branches.push(if body.is_empty() && !start && !end { pad(false) } else { format!("{}{body}{}", pad(start), pad(end)) });
match parser.next() {
None => break,
Some('|') => {}
Some(_) => return None,
}
}
Some(branches.join("|"))
}
const MAX_GROUPS: usize = 32;
const MAX_BOUND: u32 = 255;
struct Regex<'a> {
s: &'a [char],
at: usize,
depth: usize,
}
impl Regex<'_> {
fn peek(&self) -> Option<char> {
self.s.get(self.at).copied()
}
fn next(&mut self) -> Option<char> {
let c = self.peek()?;
self.at += 1;
Some(c)
}
fn branch(&mut self, top: bool) -> Option<(bool, String, bool)> {
let start = top && self.peek() == Some('^');
if start {
self.at += 1;
}
let mut out = String::new();
loop {
match self.peek() {
None | Some('|') | Some(')') => return Some((start, out, false)),
Some('$') if top && matches!(self.s.get(self.at + 1), None | Some('|')) => {
self.at += 1;
return Some((start, out, true));
}
_ => {
out.push_str(&self.atom()?);
out.push_str(&self.quantifier()?);
}
}
}
}
fn atom(&mut self) -> Option<String> {
match self.next()? {
'(' => {
if self.peek() == Some('?') {
if self.s.get(self.at + 1) != Some(&':') {
return None;
}
self.at += 2;
}
self.depth += 1;
if self.depth > MAX_GROUPS {
return None;
}
let mut branches = Vec::new();
loop {
let (_, body, _) = self.branch(false)?;
branches.push(body);
match self.next()? {
'|' => {}
')' => break,
_ => return None,
}
}
self.depth -= 1;
Some(format!("({})", branches.join("|")))
}
'[' => self.bracket(),
'.' => Some(ANY.to_string()),
'\\' => {
let mut out = String::new();
push_literal(&mut out, self.escaped()?);
Some(out)
}
'*' | '+' | '?' | '{' | '^' | '$' | '}' | ']' => None,
c => {
let mut out = String::new();
push_literal(&mut out, c);
Some(out)
}
}
}
fn escaped(&mut self) -> Option<char> {
Some(match self.next()? {
'n' => '\n',
'r' => '\r',
't' => '\t',
'f' => '\u{c}',
'v' => '\u{b}',
'a' => '\u{7}',
'e' => '\u{1b}',
c if c.is_alphanumeric() => return None,
c => c,
})
}
fn quantifier(&mut self) -> Option<String> {
let out = match self.peek() {
Some(c @ ('*' | '+' | '?')) => {
self.at += 1;
c.to_string()
}
Some('{') => {
self.at += 1;
let number = |p: &mut Self| -> Option<u32> {
let from = p.at;
while p.peek().is_some_and(|c| c.is_ascii_digit()) {
p.at += 1;
}
let n: u32 = p.s[from..p.at].iter().collect::<String>().parse().ok()?;
(n <= MAX_BOUND).then_some(n)
};
let min = number(self)?;
match self.next()? {
'}' => format!("{{{min}}}"),
',' if self.peek() == Some('}') => {
self.at += 1;
format!("{{{min},}}")
}
',' => {
let max = number(self)?;
(self.next()? == '}' && min <= max).then_some(())?;
format!("{{{min},{max}}}")
}
_ => return None,
}
}
_ => return Some(String::new()),
};
if self.peek() == Some('?') {
self.at += 1;
}
match self.peek() {
Some('*' | '+' | '?' | '{') => None,
_ => Some(out),
}
}
fn bracket(&mut self) -> Option<String> {
let mut out = String::from("[");
if self.peek() == Some('^') {
self.at += 1;
out.push('^');
}
let mut first = true;
loop {
let c = self.next()?;
let low = match c {
']' if !first => break,
'[' if matches!(self.peek(), Some(':' | '.' | '=')) => return None,
'\\' => self.escaped()?,
'-' if !first && self.peek() != Some(']') => return None,
c => c,
};
first = false;
push_class_char(&mut out, low);
if self.peek() == Some('-') && self.s.get(self.at + 1).is_some_and(|c| *c != ']') {
self.at += 1;
let high = match self.next()? {
'\\' => self.escaped()?,
'[' => return None,
c => c,
};
if high < low {
return None;
}
out.push('-');
push_class_char(&mut out, high);
}
}
out.push(']');
(!out.contains("&&") && !out.contains("~~")).then_some(out)
}
}
fn push_class_char(out: &mut String, c: char) {
match c {
'-' | '[' | '\\' | ']' | '^' => {
out.push('\\');
out.push(c);
}
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
_ => out.push(c),
}
}
fn push_literal(out: &mut String, c: char) {
match c {
'.' | '\\' | '?' | '*' | '+' | '{' | '}' | '(' | ')' | '[' | ']' | '|' | '-' | '^' => {
out.push('\\');
out.push(c);
}
'\n' => out.push_str("\\n"),
'\r' => out.push_str("\\r"),
'\t' => out.push_str("\\t"),
_ => out.push(c),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_like_pattern_is_the_i_regexp_of_the_same_values() {
assert_eq!(from_like("a%", Some('\\')).as_deref(), Some("a(.|\\n|\\r)*"));
assert_eq!(from_like("%@%", Some('\\')).as_deref(), Some("(.|\\n|\\r)*@(.|\\n|\\r)*"));
assert_eq!(from_like("a_c", Some('\\')).as_deref(), Some("a(.|\\n|\\r)c"));
assert_eq!(from_like("1.5 (x)", Some('\\')).as_deref(), Some("1\\.5 \\(x\\)"));
assert_eq!(from_like("100\\%", Some('\\')).as_deref(), Some("100%"));
assert_eq!(from_like("a\\_b", Some('\\')).as_deref(), Some("a_b"));
assert_eq!(from_like("a!%", Some('!')).as_deref(), Some("a%"));
assert_eq!(from_like("a\\%", None).as_deref(), Some("a\\\\(.|\\n|\\r)*"));
assert_eq!(from_like("a\\", Some('\\')), None);
}
#[test]
fn a_postgres_regex_is_the_i_regexp_of_the_values_it_finds_a_match_in() {
let any = "(.|\\n|\\r)";
assert_eq!(from_postgres_regex("^[a-z]+$").as_deref(), Some("[a-z]+"));
assert_eq!(from_postgres_regex("^ab").as_deref(), Some(format!("ab{any}*")).as_deref());
assert_eq!(from_postgres_regex("@").as_deref(), Some(format!("{any}*@{any}*")).as_deref());
assert_eq!(from_postgres_regex("").as_deref(), Some(format!("{any}*")).as_deref());
assert_eq!(from_postgres_regex("^a$|^b").as_deref(), Some(format!("a|b{any}*")).as_deref());
assert_eq!(from_postgres_regex("^a.c$").as_deref(), Some(format!("a{any}c")).as_deref());
assert_eq!(from_postgres_regex("^a+?b{2,3}?(?:c|d)*$").as_deref(), Some("a+b{2,3}(c|d)*"));
assert_eq!(from_postgres_regex("^[^-a\\]]-x{3,}$").as_deref(), Some("[^\\-a\\]]\\-x{3,}"));
assert_eq!(from_postgres_regex("^\\.\\$\\n[\\t ]$").as_deref(), Some("\\.$\\n[\\t ]"));
assert_eq!(from_postgres_regex("^[]a]$").as_deref(), Some("[\\]a]"));
for refused in [
"^(a)\\1$", "^(?=a)", "(?i)^a$", "***=a", "^\\d+$", "^\\w$", "\\ma", "\\Aa", "^[[:alpha:]]$", "^[[.a.]]$", "^[[=a=]]$", "(^a)", "a^b", "a$b", "*a", "a**", "a{2}{3}", "a{,3}", "a{3,2}", "a{256}", "^[a-c-e]$", "^[z-a]$", "a\\", "(a", "a)", "]", "}", "^[a&&b]$", "\\x41", "\\u0041",
] {
assert_eq!(from_postgres_regex(refused), None, "{refused}");
}
}
}