This repository has been archived by the owner on May 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Add option to interpret null byte as an empty cell #24
Open
tomplex
wants to merge
2
commits into
master
Choose a base branch
from
tc/null-byte-as-empty
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,13 @@ struct Opt { | |
/// quoting. | ||
#[structopt(value_name = "CHAR", long = "quote", default_value = "\"")] | ||
quote: CharSpecifier, | ||
|
||
/// Should a null-byte (\x0) be interpreted as an empty cell | ||
/// this option exists because it is not always possible to pass | ||
/// a null byte into --null on the command line. | ||
/// https://github.com/faradayio/scrubcsv/issues/17 | ||
#[structopt(long = "null-byte-as-empty")] | ||
null_byte_as_empty: bool, | ||
} | ||
|
||
lazy_static! { | ||
|
@@ -122,16 +129,25 @@ fn run() -> Result<()> { | |
// Remember the time we started. | ||
let start_time = now(); | ||
|
||
// Build a regex containing our `--null` value. | ||
let null_re = if let Some(null_re_str) = opt.null.as_ref() { | ||
// Always match the full CSV value. | ||
let s = format!("^{}$", null_re_str); | ||
let re = Regex::new(&s).context("can't compile regular expression")?; | ||
Some(re) | ||
let mut pattern = if opt.null_byte_as_empty == true { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sure there's a better way to deal with this, but I'm not familiar enough to tell for sure. |
||
Some(String::from(r"\x00")) | ||
} else { | ||
None | ||
}; | ||
|
||
if let Some(null_re_str) = opt.null.as_ref() { | ||
pattern = match pattern { | ||
Some(p) => Some(format!(r"{}|^{}$", p, null_re_str)), | ||
None => Some(format!("^{}$", null_re_str)), | ||
} | ||
} | ||
|
||
// Build a regex containing our `--null` value(s). | ||
let null_re = match pattern { | ||
Some(p) => Some(Regex::new(&p).context("can't build regular expression")?), | ||
None => None, | ||
}; | ||
|
||
// Fetch our input from either standard input or a file. The only tricky | ||
// detail here is that we use a `Box<dyn Read>` to represent "some object | ||
// implementing `Read`, stored on the heap." This allows us to do runtime | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -200,3 +200,47 @@ a,b,c | |
"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_null_byte_as_empty_cell() { | ||
let testdir = TestDir::new("scrubcsv", "null_byte_as_empty"); | ||
let output = testdir | ||
.cmd() | ||
.arg("--null-byte-as-empty") | ||
.output_with_stdin( | ||
br#"c1,c2,c3 | ||
1,2,3 | ||
\x00,\x00,1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is failing because the |
||
"#, | ||
) | ||
.expect("error running scrubcsv test"); | ||
assert_eq!( | ||
output.stdout_str(), | ||
r#"c1,c2,c3 | ||
1,2,3 | ||
,,1 | ||
"# | ||
); | ||
} | ||
#[test] | ||
fn test_null_byte_as_empty_cell_with_null_re() { | ||
let testdir = TestDir::new("scrubcsv", "null_byte_as_empty_with_null_re"); | ||
let output = testdir | ||
.cmd() | ||
.arg("--null-byte-as-empty") | ||
.args(&["--null", "NULL"]) | ||
.output_with_stdin( | ||
br#"c1,c2,c3 | ||
1,2,NULL | ||
\x00,\x00,1 | ||
"#, | ||
) | ||
.expect("error running scrubcsv test"); | ||
assert_eq!( | ||
output.stdout_str(), | ||
r#"c1,c2,c3 | ||
1,2, | ||
,,1 | ||
"# | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before adding an option, did we try
--null '^\x00$'? Rust's
regex` library allows bytes to be escaped.If we need to add another null option besides
--null
, we should aim for a consistent naming convention, both with-null
and the other existing options. Most of our existing options are either "--verb" or "--noun", and--null-byte-as-empty
. It's definitely good to start with--null-
like you do here, because that will ensure the right sort order on the display.