Skip to content

Commit

Permalink
count: added --flexible option for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jqnatividad committed Nov 16, 2023
1 parent b469f9c commit 1598d27
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/cmd/count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ count options:
Common options:
-h, --help Display this message
-f, --flexible Do not validate if the CSV has different number of
fields per record, increasing performance when counting
without an index. Automatically enabled when --width is set.
-n, --no-headers When set, the first row will be included in
the count.
"#;
Expand All @@ -31,6 +34,7 @@ struct Args {
arg_input: Option<String>,
flag_human_readable: bool,
flag_width: bool,
flag_flexible: bool,
flag_no_headers: bool,
}

Expand All @@ -41,7 +45,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
// we also want to count the quotes when computing width
.quoting(!args.flag_width)
// and ignore differing column counts as well
.flexible(args.flag_width);
.flexible(args.flag_width || args.flag_flexible);

// this comment left here for Logging.md example
// log::debug!(
Expand Down
20 changes: 20 additions & 0 deletions tests/test_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ fn count_width() {
assert_eq!(got, expected.to_string());
}

#[test]
fn count_flexible() {
let wrk = Workdir::new("count_flexible");
wrk.create_from_string(
"in.csv",
r#"letter,number,flag
alphabetic,13,true,extra column
beta,24,false
gamma,37.1
delta,42.5,false
"#,
);
let mut cmd = wrk.command("count");
cmd.arg("--flexible").arg("in.csv");

let got: String = wrk.stdout(&mut cmd);
let expected = "4";
assert_eq!(got, expected.to_string());
}

#[test]
fn count_comments() {
let wrk = Workdir::new("count_comments");
Expand Down

0 comments on commit 1598d27

Please sign in to comment.