diff --git a/src/Utf8StringSplitter/Shims.cs b/src/Utf8StringSplitter/Shims.cs index 7df38a6..b6a9add 100644 --- a/src/Utf8StringSplitter/Shims.cs +++ b/src/Utf8StringSplitter/Shims.cs @@ -1,4 +1,4 @@ - + using System.Runtime.CompilerServices; using System.Text; @@ -50,4 +50,13 @@ public static string GetString(ReadOnlySpan bytes) #endif } } + + internal static class ArgumentExceptionEx + { + public static void Throw(string message) + { + throw new ArgumentException(message); + } + } + } diff --git a/src/Utf8StringSplitter/Utf8Splitter.cs b/src/Utf8StringSplitter/Utf8Splitter.cs index 3ed636b..191578b 100644 --- a/src/Utf8StringSplitter/Utf8Splitter.cs +++ b/src/Utf8StringSplitter/Utf8Splitter.cs @@ -1,4 +1,4 @@ - + using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -12,7 +12,7 @@ public static SplitEnumerator Split(ReadOnlySpan source, byte separator, U const Utf8StringSplitOptions AllOptions = Utf8StringSplitOptions.TrimEntries | Utf8StringSplitOptions.RemoveEmptyEntries; if ((splitOptions & ~AllOptions) != 0) { - throw new ArgumentException("Utf8StringSplitOptions Value is Invalid."); + ArgumentExceptionEx.Throw("The specified 'Utf8StringSplitOptions' value is not valid."); } return new SplitEnumerator(source, separator, splitOptions); @@ -23,7 +23,7 @@ public static SplitEnumerator Split(ReadOnlySpan source, ReadOnlySpan source, ReadOnlySpa const Utf8StringSplitOptions AllOptions = Utf8StringSplitOptions.TrimEntries | Utf8StringSplitOptions.RemoveEmptyEntries; if ((splitOptions & ~AllOptions) != 0) { - throw new ArgumentException("Utf8StringSplitOptions Value is Invalid."); + ArgumentExceptionEx.Throw("The specified 'Utf8StringSplitOptions' value is not valid."); } switch(separatorOptions) @@ -43,7 +43,8 @@ public static SplitAnyEnumerator SplitAny(ReadOnlySpan source, ReadOnlySpa case Utf8StringSeparatorOptions.Bytes: return new SplitAnyEnumerator(source, separators, splitOptions, separatorOptions); default: - throw new ArgumentException("Utf8StringSeparatorOptions Value is Invalid."); + ArgumentExceptionEx.Throw("The specified 'Utf8StringSeparatorOptions' value is not valid."); + return default; // It will not reach here. } } } @@ -105,7 +106,7 @@ internal SplitEnumerator(ReadOnlySpan source, ReadOnlySpan separator public readonly SplitEnumerator GetEnumerator() => this; - internal readonly int sourceLength => source.Length; + internal readonly int SourceLength => source.Length; public bool MoveNext() { @@ -122,7 +123,7 @@ public bool MoveNext() public readonly byte[][] ToArray() { - var writer = new ExtendableArray(sourceLength); + var writer = new ExtendableArray(SourceLength); foreach (var i in this) { writer.Add(i.ToArray()); @@ -133,7 +134,7 @@ public readonly byte[][] ToArray() public readonly string[] ToUtf16Array() { - var writer = new ExtendableArray(sourceLength); + var writer = new ExtendableArray(SourceLength); foreach (var i in this) { writer.Add(UTF8Ex.GetString(i)); @@ -207,8 +208,6 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan separator) var index = source.IndexOf(separator); if (index < 0 || separator.Length == 0) // end { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, startIndex, endIndex); - if (trimEntries) { if (source.Length == 1 && source[0] == 0x20) @@ -217,6 +216,7 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan separator) } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, startIndex, endIndex); source = source[startIndex..endIndex]; } } @@ -235,13 +235,13 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan separator) source = source[index..]; if (trimEntries) { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); if (current.Length == 1 && current[0] == 0x20) { current = ReadOnlySpan.Empty; } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); current = current[startIndex..endIndex]; } } @@ -255,8 +255,6 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan separator) index = source.IndexOf(separator); if (index < 0) // end { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, 0, source.Length); - if (trimEntries) { if (source.Length == 1 && source[0] == 0x20) @@ -265,6 +263,7 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan separator) } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, 0, source.Length); source = source[startIndex..endIndex]; } } @@ -283,13 +282,13 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan separator) source = source[index..]; if (trimEntries) { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); if (current.Length == 1 && current[0] == 0x20) { current = ReadOnlySpan.Empty; } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); current = current[startIndex..endIndex]; } } @@ -437,8 +436,6 @@ private bool MoveNextWithOptions() } if (index < 0) // end { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, startIndex, endIndex); - if (trimEntries) { if (target.Length == 1 && target[0] == 0x20) @@ -447,6 +444,7 @@ private bool MoveNextWithOptions() } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, startIndex, endIndex); target = target[startIndex..endIndex]; } } @@ -465,13 +463,13 @@ private bool MoveNextWithOptions() target = target[index..]; if (trimEntries) { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); if (current.Length == 1 && current[0] == 0x20) { current = ReadOnlySpan.Empty; } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); current = current[startIndex..endIndex]; } } @@ -495,8 +493,6 @@ private bool MoveNextWithOptions() } if (index < 0) // end { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, 0, target.Length); - if (trimEntries) { if (target.Length == 1 && target[0] == 0x20) @@ -505,6 +501,7 @@ private bool MoveNextWithOptions() } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, 0, target.Length); target = target[startIndex..endIndex]; } } @@ -523,13 +520,13 @@ private bool MoveNextWithOptions() target = target[index..]; if (trimEntries) { - (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); if (current.Length == 1 && current[0] == 0x20) { current = ReadOnlySpan.Empty; } else { + (startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length); current = current[startIndex..endIndex]; } } diff --git a/src/Utf8StringSplitter/Utf8StringSplitter.csproj b/src/Utf8StringSplitter/Utf8StringSplitter.csproj index df457b2..ec8a1e6 100644 --- a/src/Utf8StringSplitter/Utf8StringSplitter.csproj +++ b/src/Utf8StringSplitter/Utf8StringSplitter.csproj @@ -1,11 +1,11 @@  - netstandard2.0;netstandard2.1;net6.0;net8.0 + netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0;net10.0; enable - 12 + 13 enable true - 1.0.1 + 1.1.0 prozolic prozolic https://github.com/prozolic/Utf8StringSplitter diff --git a/tests/Utf8StringSplitter.Tests/Utf8SplitterTest.cs b/tests/Utf8StringSplitter.Tests/Utf8SplitterTest.cs index 37fa5da..da30d38 100644 --- a/tests/Utf8StringSplitter.Tests/Utf8SplitterTest.cs +++ b/tests/Utf8StringSplitter.Tests/Utf8SplitterTest.cs @@ -1,7 +1,9 @@ using Shouldly; using System; using System.Diagnostics; +using System.Reflection; using System.Text; +using System.Xml; namespace Utf8StringSplitter.Tests { @@ -1117,14 +1119,14 @@ public void SplitAnyWithTrimEntriesAndRemoveEmptyEntriesTest2() [Fact] public void SplitAnyWithUtf8SeparatorOptionsTest() { - var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; // - var separator = new byte[] { 227, 129, 134 }; // + var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; //あいうえうお + var separator = new byte[] { 227, 129, 134 }; //う { var expected = new List() { - new byte[] { 227, 129, 130, 227, 129, 132 }, // - new byte[] { 227, 129, 136}, // - new byte[] { 227, 129, 138}, // + new byte[] { 227, 129, 130, 227, 129, 132 }, // あい + new byte[] { 227, 129, 136}, // え + new byte[] { 227, 129, 138}, // お }; var index = 0; foreach (var s in Utf8Splitter.SplitAny(source, separator)) @@ -1154,8 +1156,8 @@ public void SplitAnyWithUtf8SeparatorOptionsTest() [Fact] public void SplitAnyWithBytesSeparatorOptionsTest() { - var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; // - var separator = new byte[] { 227, 129, 134 }; // + var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; //あいうえうお + var separator = new byte[] { 227, 129, 134 }; //う { var actual = new List(); var expected = new List(); @@ -1238,5 +1240,38 @@ public void ToUtf16ArrayTest2() result.Length.ShouldBe(expected.Length); } + [Fact] + public void ThrowArgumentExceptionTest() + { + { + var ex = Should.Throw(() => + { + _ = Utf8Splitter.Split("1,2,3,4,5"u8, (byte)',', splitOptions:(Utf8StringSplitOptions)(-1)); + }); + ex.Message.ShouldBe("The specified 'Utf8StringSplitOptions' value is not valid."); + } + { + var ex = Should.Throw(() => + { + _ = Utf8Splitter.Split("1,2,3,4,5"u8, ","u8, splitOptions:(Utf8StringSplitOptions)(-1)); + }); + ex.Message.ShouldBe("The specified 'Utf8StringSplitOptions' value is not valid."); + } + { + var ex = Should.Throw(() => + { + _ = Utf8Splitter.SplitAny("1,2-3;4-5"u8, "-,;"u8, splitOptions:(Utf8StringSplitOptions)(-1)); + }); + ex.Message.ShouldBe("The specified 'Utf8StringSplitOptions' value is not valid."); + } + { + var ex = Should.Throw(() => + { + _ = Utf8Splitter.SplitAny("1,2-3;4-5"u8, "-,;"u8, separatorOptions:(Utf8StringSeparatorOptions)(-1)); + }); + ex.Message.ShouldBe("The specified 'Utf8StringSeparatorOptions' value is not valid."); + } + } + } } \ No newline at end of file diff --git a/tests/Utf8StringSplitter.Tests/Utf8StringSplitter.Tests.csproj b/tests/Utf8StringSplitter.Tests/Utf8StringSplitter.Tests.csproj index e8e4ab5..58d9fcf 100644 --- a/tests/Utf8StringSplitter.Tests/Utf8StringSplitter.Tests.csproj +++ b/tests/Utf8StringSplitter.Tests/Utf8StringSplitter.Tests.csproj @@ -2,7 +2,7 @@ net48 - $(TargetFrameworks);net6.0;net8.0 + $(TargetFrameworks);net6.0;net8.0;net9.0;net10.0 enable enable 12.0