pub fn build_regex(pattern: &str, flags: &str) -> Result<Regex, EvalError> {
    // LIKE patterns always cover the whole string, so we anchor the regex on
    // both sides. An underscore (`_`) in a LIKE pattern matches any single
    // character and a percent sign (`%`) matches any sequence of zero or more
    // characters, so we translate those operators to their equivalent regex
    // operators, `.` and `.*`, respectively. Other characters match themselves
    // and are copied directly, unless they have special meaning in a regex, in
    // which case they are escaped in the regex with a backslash (`\`).
    //
    // Note that characters in LIKE patterns may also be escaped by preceding
    // them with a backslash. This has no effect on most characters, but it
    // removes the special meaning from the underscore and percent sign
    // operators, and means that matching a literal backslash requires doubling
    // the backslash.
    //
    // TODO(benesch): SQL permits selecting a different escape character than
    // the backslash via LIKE '...' ESCAPE '...'. We will need to support this
    // syntax eventually.
    let mut regex = String::from("^");
    let mut escape = false;
    for c in pattern.chars() {
        match c {
            '\\' if !escape => escape = true,
            '_' if !escape => regex.push('.'),
            '%' if !escape => regex.push_str(".*"),
            c => {
                if regex_syntax::is_meta_character(c) {
                    regex.push('\\');
                }
                regex.push(c);
                escape = false;
            }
        }
    }
    regex.push('$');
    if escape {
        return Err(EvalError::UnterminatedLikeEscapeSequence);
    }

    let mut regex = RegexBuilder::new(&regex);
    for f in flags.chars() {
        match f {
            'i' => {
                regex.case_insensitive(true);
            }
            'c' => {
                regex.case_insensitive(false);
            }
            _ => return Err(EvalError::InvalidRegexFlag(f)),
        }
    }
    match regex.build() {
        Ok(regex) => Ok(regex),
        Err(regex::Error::CompiledTooBig(_)) => Err(EvalError::LikePatternTooLong),
        Err(e) => Err(EvalError::Internal(format!(
            "build_regex produced invalid regex: {}",
            e
        ))),
    }
}
/// Builds a regular expression that matches the same strings as a SQL
/// LIKE pattern.
Builds a regular expression that matches the same strings as a SQL
LIKE pattern.
Builds a regular expression that matches the same strings as a SQL LIKE pattern .
pub fn build_regex(pattern: &str, flags: &str) -> Result<Regex, EvalError> {
    // LIKE patterns always cover the whole string, so we anchor the regex on
    // both sides. An underscore (`_`) in a LIKE pattern matches any single
    // character and a percent sign (`%`) matches any sequence of zero or more
    // characters, so we translate those operators to their equivalent regex
    // operators, `.` and `.*`, respectively. Other characters match themselves
    // and are copied directly, unless they have special meaning in a regex, in
    // which case they are escaped in the regex with a backslash (`\`).
    //
    // Note that characters in LIKE patterns may also be escaped by preceding
    // them with a backslash. This has no effect on most characters, but it
    // removes the special meaning from the underscore and percent sign
    // operators, and means that matching a literal backslash requires doubling
    // the backslash.
    //
    // TODO(benesch): SQL permits selecting a different escape character than
    // the backslash via LIKE '...' ESCAPE '...'. We will need to support this
    // syntax eventually.
    let mut regex = String::from("^");
    let mut escape = false;
    for c in pattern.chars() {
        match c {
            '\\' if !escape => escape = true,
            '_' if !escape => regex.push('.'),
            '%' if !escape => regex.push_str(".*"),
            c => {
                if regex_syntax::is_meta_character(c) {
                    regex.push('\\');
                }
                regex.push(c);
                escape = false;
            }
        }
    }
    regex.push('$');
    if escape {
        return Err(EvalError::UnterminatedLikeEscapeSequence);
    }
    let mut regex = RegexBuilder::new(&regex);
    for f in flags.chars() {
        match f {
            'i' => {
                regex.case_insensitive(true);
            }
            'c' => {
                regex.case_insensitive(false);
            }
            _ => return Err(EvalError::InvalidRegexFlag(f)),
        }
    }
    match regex.build() {
        Ok(regex) => Ok(regex),
        Err(regex::Error::CompiledTooBig(_)) => Err(EvalError::LikePatternTooLong),
        Err(e) => Err(EvalError::Internal(format!(
            "build_regex produced invalid regex: {}",
            e
        ))),
    }
}
pub fn build_regex ( pattern : & str , flags : & str ) -> Result < Regex , EvalError > { // LIKE patterns always cover the whole string, so we anchor the regex on // both sides. An underscore (`_`) in a LIKE pattern matches any single // character and a percent sign (`%`) matches any sequence of zero or more // characters, so we translate those operators to their equivalent regex // operators, `.` and `.*`, respectively. Other characters match themselves // and are copied directly, unless they have special meaning in a regex, in // which case they are escaped in the regex with a backslash (`\`). // // Note that characters in LIKE patterns may also be escaped by preceding // them with a backslash. This has no effect on most characters, but it // removes the special meaning from the underscore and percent sign // operators, and means that matching a literal backslash requires doubling // the backslash. // // TODO(benesch): SQL permits selecting a different escape character than // the backslash via LIKE '...' ESCAPE '...'. We will need to support this // syntax eventually. let mut regex = String :: from ( " " ) ; let mut escape = false ; for c in pattern . chars ( ) { match c { '\\' if ! escape => escape = true , '_' if ! escape => regex . push ( '.' ) , '%' if ! escape => regex . push_str ( " " ) , c => { if regex_syntax :: is_meta_character ( c ) { regex . push ( '\\' ) ; } regex . push ( c ) ; escape = false ; } } } regex . push ( '$' ) ; if escape { return Err ( EvalError :: UnterminatedLikeEscapeSequence ) ; } let mut regex = RegexBuilder :: new ( & regex ) ; for f in flags . chars ( ) { match f { 'i' => { regex . case_insensitive ( true ) ; } 'c' => { regex . case_insensitive ( false ) ; } _ => return Err ( EvalError :: InvalidRegexFlag ( f ) ) , } } match regex . build ( ) { Ok ( regex ) => Ok ( regex ) , Err ( regex :: Error :: CompiledTooBig ( _ ) ) => Err ( EvalError :: LikePatternTooLong ) , Err ( e ) => Err ( EvalError :: Internal ( format ! ( " " e ) ) ) , } }
Builds a regular expression that matches the same strings as a SQL
LIKE pattern.
Builds a regular expression that matches the same strings as a SQL LIKE pattern .
pattern
&str
flags
&str
pattern
&str
flags
&str