Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search & searchset: add --literal option #2060

Merged
merged 2 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/cmd/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ Usage:
search options:
-i, --ignore-case Case insensitive search. This is equivalent to
prefixing the regex with '(?i)'.
--literal Treat the regex as a literal string. This allows
you to search for exact matches that even contain special
regex characters without escaping them.
-s, --select <arg> Select the columns to search. See 'qsv select -h'
for the full syntax.
-v, --invert-match Select only rows that did not match
Expand Down Expand Up @@ -88,6 +91,7 @@ use crate::{
struct Args {
arg_input: Option<String>,
arg_regex: String,
flag_literal: bool,
flag_select: SelectColumns,
flag_output: Option<String>,
flag_no_headers: bool,
Expand Down Expand Up @@ -115,7 +119,13 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
args.flag_unicode
};

let pattern = RegexBuilder::new(&args.arg_regex)
let arg_regex = if args.flag_literal {
regex::escape(&args.arg_regex)
} else {
args.arg_regex.clone()
};

let pattern = RegexBuilder::new(&arg_regex)
.case_insensitive(args.flag_ignore_case)
.unicode(regex_unicode)
.size_limit(args.flag_size_limit * (1 << 20))
Expand Down
19 changes: 16 additions & 3 deletions src/cmd/searchset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ Usage:
search options:
-i, --ignore-case Case insensitive search. This is equivalent to
prefixing the regex with '(?i)'.
--literal Treat the regex as a literal string. This allows
you to search for exact matches that even contain special
regex characters without escaping them.
-s, --select <arg> Select the columns to search. See 'qsv select -h'
for the full syntax.
-v, --invert-match Select only rows that did not match
Expand Down Expand Up @@ -99,6 +102,7 @@ use crate::{
struct Args {
arg_input: Option<String>,
arg_regexset_file: String,
flag_literal: bool,
flag_select: SelectColumns,
flag_output: Option<String>,
flag_no_headers: bool,
Expand All @@ -119,9 +123,18 @@ struct Args {
flag_quiet: bool,
}

fn read_regexset(filename: &String) -> io::Result<Vec<String>> {
fn read_regexset(filename: &String, literal: bool) -> io::Result<Vec<String>> {
match File::open(filename) {
Ok(f) => BufReader::new(f).lines().collect(),
Ok(f) => {
if literal {
BufReader::new(f)
.lines()
.map(|l| l.map(|s| regex::escape(&s)))
.collect()
} else {
BufReader::new(f).lines().collect()
}
},
Err(e) => Err(io::Error::new(
io::ErrorKind::NotFound,
format!("Cannot open regexset file {filename}: {e}"),
Expand All @@ -142,7 +155,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
);
}

let regexset = read_regexset(&args.arg_regexset_file)?;
let regexset = read_regexset(&args.arg_regexset_file, args.flag_literal)?;

let mut regex_labels: Vec<String> = Vec::with_capacity(regexset.len());
let labels_re = Regex::new(r".?#(?P<label>.*)$").unwrap();
Expand Down
27 changes: 27 additions & 0 deletions tests/test_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ fn data(headers: bool) -> Vec<Vec<String>> {
rows
}

fn data_with_regex_chars(headers: bool) -> Vec<Vec<String>> {
let mut rows = vec![
svec!["foo^bar", "barfoo"],
svec!["a", "b"],
svec!["^barfoo", "foobar"],
svec!["Ḟooƀar", "ḃarḟoo"],
];
if headers {
rows.insert(0, svec!["h1", "h2"]);
}
rows
}

#[test]
fn search() {
let wrk = Workdir::new("search");
Expand Down Expand Up @@ -625,3 +638,17 @@ fn search_preview_json() {
assert_eq!(got, expected);
wrk.assert_success(&mut cmd);
}

#[test]
fn search_literal() {
let wrk = Workdir::new("search_literal");
wrk.create("data.csv", data_with_regex_chars(false));
let mut cmd = wrk.command("search");
cmd.arg("^bar").arg("data.csv").arg("--literal");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![svec!["foo^bar", "barfoo"], svec!["^barfoo", "foobar"]];
assert_eq!(got, expected);

wrk.assert_success(&mut cmd);
}
40 changes: 40 additions & 0 deletions tests/test_searchset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@ fn data(headers: bool) -> Vec<Vec<String>> {
rows
}

fn data_with_regex_chars(headers: bool) -> Vec<Vec<String>> {
let mut rows = vec![
svec!["foo$bar^", "barfoo"],
svec!["a", "b"],
svec!["$bar^foo", "foobar"],
svec!["is wal[do] here", "spot"],
svec!["Ḟooƀar", "$ḃar^ḟoo"],
svec!["bleh", "no, Wal[do] is there"],
];
if headers {
rows.insert(0, svec!["h1", "h2"]);
}
rows
}

fn regexset_file() -> Vec<Vec<String>> {
let rows = vec![svec!["^foo"], svec!["bar$"], svec!["waldo"]];
rows
}

fn regexset_literal_file() -> Vec<Vec<String>> {
let rows = vec![svec!["$bar^"], svec!["[do]"]];
rows
}

fn regexset_no_match_file() -> Vec<Vec<String>> {
let rows = vec![svec!["^blah"], svec!["bloop$"], svec!["joel"]];
rows
Expand Down Expand Up @@ -427,3 +447,23 @@ fn searchset_flag_complex_unmatched_output() {

wrk.assert_success(&mut cmd);
}

#[test]
fn searchset_literal() {
let wrk = Workdir::new("searchset_literal");
wrk.create("data.csv", data_with_regex_chars(true));
wrk.create("regexset.txt", regexset_literal_file());
let mut cmd = wrk.command("searchset");
cmd.arg("regexset.txt").arg("data.csv").arg("--literal");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
let expected = vec![
svec!["h1", "h2"],
svec!["foo$bar^", "barfoo"],
svec!["$bar^foo", "foobar"],
svec!["is wal[do] here", "spot"],
svec!["bleh", "no, Wal[do] is there"],
];
assert_eq!(got, expected);
wrk.assert_success(&mut cmd);
}
Loading