Skip to content

Commit 0f40f7b

Browse files
committed
Refactor Quoters/Delimiters and add validation logic
Refactored `Quoters` and `Delimiters` properties to use private backing fields and added validation to prevent overlapping characters between them. Introduced a constant error message `CommonCharactersErrorMessage` for validation failures. Changed `CsvDelimiters` to use `ImmutableHashSet` for immutability. Added a new test case `TestMethodSingleCommon` to ensure validation logic works as expected. Updated imports to include `System.Collections.Immutable`.
1 parent e2e3245 commit 0f40f7b

2 files changed

Lines changed: 38 additions & 4 deletions

File tree

SplitQuotedString/QuotedStringSplitter.cs

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Text;
1+
using System.Collections.Immutable;
2+
using System.Text;
23

34
namespace HunnyR.Tools;
45

@@ -25,6 +26,8 @@ public class QuotedStringSplitter
2526
/// </summary>
2627
public bool TreatTwoQuotesAsLiteral { get; set; } = false;
2728

29+
private const string CommonCharactersErrorMessage = "The delimiters and quoters have common characters. This might lead to undesired result. Quoters will always be evaluated first!";
30+
2831
/// <summary>
2932
/// quote characters
3033
/// default are double quote(") and single quote (')
@@ -33,16 +36,42 @@ public class QuotedStringSplitter
3336
/// '"a.b"' will return ["a.b"] not matter what delimiters
3437
/// ''a.b'' will return ["a","b"] if . is the delimiter and TreatTwoQuotesAsLiteral as literal is false else it will return ["'a.b'"]
3538
/// </summary>
36-
public HashSet<char> Quoters { get; set; } = ['"', '\''];
39+
private HashSet<char> _quoters= ['"', '\''];
40+
public HashSet<char> Quoters {
41+
get {return this._quoters; }
42+
set {
43+
if (value.Intersect(this.Delimiters).Any())
44+
{
45+
throw new ArgumentException(CommonCharactersErrorMessage, nameof(this.Quoters));
46+
}
47+
48+
this._quoters = [..value]; // create a copy!
49+
}
50+
}
3751

3852
/// <summary>
3953
/// delimiters
4054
/// default is space and tab
4155
/// should contain at least one character. if empty will return the full string as single token
4256
/// </summary>
43-
public HashSet<char> Delimiters { get; set; } = [' ', '\t'];
57+
private HashSet<char> _delimters= [' ', '\t'];
58+
public HashSet<char> Delimiters { get { return this._delimters; }
59+
set {
60+
if (value.Intersect(this.Quoters).Any())
61+
{
62+
throw new ArgumentException(CommonCharactersErrorMessage, nameof(this.Delimiters));
63+
}
64+
65+
this._delimters = [.. value]; // create a copy!
66+
67+
}
68+
}
4469

45-
public static readonly HashSet<char> CsvDelimiters = [',', ';', '\t'];
70+
71+
/// <summary>
72+
/// the delimiters typically used for CSV files
73+
/// </summary>
74+
public static readonly ImmutableHashSet<char> CsvDelimiters = [',', ';', '\t'];
4675

4776
public static IEnumerable<string> Split(
4877
string source, HashSet<char> delimiters, HashSet<char>? quoters = null, bool treatConsecutiveDelimitersAsOne = false, bool treatTwoQuotesAsLiteral = false

TestProject1/Test1.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public void TestMethodSingleDelimiter()
2222
Assert.IsTrue(result[1].Length == 0);
2323
}
2424

25+
[TestMethod]
26+
public void TestMethodSingleCommon()
27+
{
28+
Assert.Throws<ArgumentException>(()=>new QuotedStringSplitter() { Delimiters = ['A','B'], Quoters=['A'] });
29+
}
2530

2631

2732
[TestMethod]

0 commit comments

Comments
 (0)