Skip to main content

khive_types/
sql_like.rs

1//! Literal text for SQL `LIKE` patterns using a backslash escape character.
2
3use alloc::string::String;
4
5/// Escape `%`, `_`, and `\` so `input` matches literally under `LIKE ... ESCAPE '\'`.
6/// Callers can add their own wildcard prefix or suffix after escaping.
7pub fn escape_like_literal(input: &str) -> String {
8    let mut out = String::with_capacity(input.len());
9    for c in input.chars() {
10        if matches!(c, '\\' | '%' | '_') {
11            out.push('\\');
12        }
13        out.push(c);
14    }
15    out
16}
17
18#[cfg(test)]
19#[path = "sql_like_tests.rs"]
20mod tests;