Skip to content

@Csv{File}Source: align whitespace detection with FastCSV #4692

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

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
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@
* Controls whether leading and trailing whitespace characters of unquoted
* CSV columns should be ignored.
*
* <p>Whitespace refers to characters with Unicode code points less than
* or equal to {@code U+0020}, as defined by {@link String#trim()}.
*
* <p>Defaults to {@code true}.
*
* @since 5.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public String modify(long unusedStartingLineNumber, int unusedFieldIdx, boolean
if (!quoted && field.isBlank()) {
return NULL_MARKER;
}
String modifiedField = (!quoted && ignoreLeadingAndTrailingWhitespaces) ? field.strip() : field;
String modifiedField = (!quoted && ignoreLeadingAndTrailingWhitespaces) ? field.trim() : field;
if (nullValues.contains(modifiedField)) {
return NULL_MARKER;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,9 @@
* Controls whether leading and trailing whitespace characters of unquoted
* CSV columns should be ignored.
*
* <p>Whitespace refers to characters with Unicode code points less than
* or equal to {@code U+0020}, as defined by {@link String#trim()}.
*
* <p>Defaults to {@code true}.
*
* @since 5.8
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ void trimsTrailingSpaces() {
assertThat(arguments).containsExactly(new Object[][] { { "1", "" }, { "2", "" }, { "3", "" }, { "4", "" } });
}

@Test
void trimsSpacesUsingStringTrim() {
// \u0000 (null) removed by trim(), preserved by strip()
// \u00A0 (non-breaking space) preserved by trim(), removed by strip()
var annotation = csvSource().lines("\u0000foo,\u00A0bar", "\u0000' foo',\u00A0' bar'").build();

var arguments = provideArguments(annotation);

assertThat(arguments).containsExactly(array("foo", "\u00A0bar"), array(" foo", "\u00A0' bar'"));
}

@Test
void ignoresLeadingAndTrailingSpaces() {
var annotation = csvSource().lines("1,a", "2, b", "3,c ", "4, d ") //
Expand Down