Skip to content

Commit

Permalink
Merge pull request #1471 from jqnatividad/apply_replace_align_use_null
Browse files Browse the repository at this point in the history
align `apply`/`applydp` with `replace` to use <NULL> case-insensitive…
  • Loading branch information
jqnatividad authored Dec 12, 2023
2 parents caf2c03 + 6814c7f commit c8d6818
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 10 deletions.
8 changes: 5 additions & 3 deletions src/cmd/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ It has 36 supported operations:
* replace: Replace all matches of a pattern (using --comparand)
with a string (using --replacement) (Rust replace)
* regex_replace: Replace all regex matches in --comparand w/ --replacement.
Specify <EMPTY> as --replacement to remove matches.
Specify <NULL> as --replacement to remove matches.
* titlecase - capitalizes English text using Daring Fireball titlecase style
https://daringfireball.net/2008/05/title_case
* censor: profanity filter. Add additional comma-delimited profanities with --comparand.
Expand Down Expand Up @@ -470,6 +470,8 @@ const DEFAULT_THRESHOLD: f64 = 0.9;
// default number of decimal places to round to
const DEFAULT_ROUND_PLACES: u32 = 3;

const NULL_VALUE: &str = "<null>";

// for thousands operator
static INDIANCOMMA_POLICY: SeparatorPolicy = SeparatorPolicy {
separator: ",",
Expand Down Expand Up @@ -584,11 +586,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
wtr.write_record(&headers)?;
}

// if there is a regex_replace operation and replacement is <empty> case-insensitive,
// if there is a regex_replace operation and replacement is <NULL> case-insensitive,
// we set it to empty string
let flag_replacement = if apply_cmd == ApplySubCmd::Operations
&& ops_vec.contains(&Operations::Regex_Replace)
&& args.flag_replacement.to_lowercase() == "<empty>"
&& args.flag_replacement.to_lowercase() == NULL_VALUE
{
String::new()
} else {
Expand Down
8 changes: 5 additions & 3 deletions src/cmd/applydp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ It has 18 supported operations:
* replace: Replace all matches of a pattern (using --comparand)
with a string (using --replacement) (Rust replace)
* regex_replace: Replace all regex matches in --comparand w/ --replacement.
Specify <EMPTY> as --replacement to remove matches.
Specify <NULL> as --replacement to remove matches.
* round: Round numeric values to the specified number of decimal places using
Midpoint Nearest Even Rounding Strategy AKA "Bankers Rounding."
Specify the number of decimal places with --formatstr (default: 3).
Expand Down Expand Up @@ -319,6 +319,8 @@ static ROUND_PLACES: OnceLock<u32> = OnceLock::new();
// default number of decimal places to round to
const DEFAULT_ROUND_PLACES: u32 = 3;

const NULL_VALUE: &str = "<null>";

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
let rconfig = Config::new(&args.arg_input)
Expand Down Expand Up @@ -422,11 +424,11 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
wtr.write_record(&headers)?;
}

// if there is a regex_replace operation and replacement is <empty> case-insensitive,
// if there is a regex_replace operation and replacement is <NULL> case-insensitive,
// we set it to empty string
let flag_replacement = if applydp_cmd == ApplydpSubCmd::Operations
&& ops_vec.contains(&Operations::Regex_Replace)
&& args.flag_replacement.to_lowercase() == "<empty>"
&& args.flag_replacement.to_lowercase() == NULL_VALUE
{
String::new()
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/cmd/replace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ struct Args {
flag_quiet: bool,
}

const NULL_VALUE: &str = "<NULL>";
const NULL_VALUE: &str = "<null>";

pub fn run(argv: &[&str]) -> CliResult<()> {
let args: Args = util::get_args(USAGE, argv)?;
Expand All @@ -95,7 +95,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
.size_limit(args.flag_size_limit * (1 << 20))
.dfa_size_limit(args.flag_dfa_size_limit * (1 << 20))
.build()?;
let replacement = if args.arg_replacement == NULL_VALUE {
let replacement = if args.arg_replacement.to_lowercase() == NULL_VALUE {
b""
} else {
args.arg_replacement.as_bytes()
Expand Down
2 changes: 1 addition & 1 deletion tests/test_apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ fn apply_regex_replace_issue1469() {
.arg("regex_replace")
.arg("col1,col2,col3")
.args(["--comparand", r"\([^)]+\)"])
.args(["--replacement", "<EmpTY>"])
.args(["--replacement", "<Null>"])
.arg("data.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
Expand Down
2 changes: 1 addition & 1 deletion tests/test_applydp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ fn applydp_regex_replace_issue1469() {
.arg("regex_replace")
.arg("col1,col2,col3")
.args(["--comparand", r"\([^)]+\)"])
.args(["--replacement", "<EMPTY>"])
.args(["--replacement", "<NULL>"])
.arg("data.csv");

let got: Vec<Vec<String>> = wrk.read_stdout(&mut cmd);
Expand Down

0 comments on commit c8d6818

Please sign in to comment.