use regex::Regex;
pub fn search<'a>(query: &str, contents: &'a str, number: bool, invert_match: bool) -> Vec<String> {
let re = Regex::new(query).unwrap();
let mut results = Vec::new();
for (i, line) in contents.lines().enumerate() {
if re.is_match(line) != invert_match {
if number {
results.push(format!("{}: {}", i + 1, line));
} else {
results.push(line.to_string());
}
}
}
results
}
pub fn search_case_insensitive<'a>(query: &str, contents: &'a str, number: bool, invert_match: bool) -> Vec<String> {
let re = Regex::new(&format!("(?i){}", regex::escape(query))).unwrap();
let mut results = Vec::new();
for (i, line) in contents.lines().enumerate() {
if re.is_match(line) != invert_match {
if number {
results.push(format!("{}: {}", i + 1, line));
} else {
results.push(line.to_string());
}
}
}
results
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn case_sensitive() {
let query = "duct";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Duct tape.";
assert_eq!(vec!["safe, fast, productive."], search(query, contents, false, false));
}
#[test]
fn case_insensitive() {
let query = "rUsT";
let contents = "\
Rust:
safe, fast, productive.
Pick three.
Trust me.";
assert_eq!(
vec!["Rust:", "Trust me."],
search_case_insensitive(query, contents, false, false)
);
}
}