Skip to content

Commit 679464a

Browse files
authored
Merge pull request #2 from prozolic/NET9and10
Upgrade v1.1.0
2 parents aedf2b7 + e3568ff commit 679464a

File tree

5 files changed

+73
-32
lines changed

5 files changed

+73
-32
lines changed

src/Utf8StringSplitter/Shims.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
22
using System.Runtime.CompilerServices;
33
using System.Text;
44

@@ -50,4 +50,13 @@ public static string GetString(ReadOnlySpan<byte> bytes)
5050
#endif
5151
}
5252
}
53+
54+
internal static class ArgumentExceptionEx
55+
{
56+
public static void Throw(string message)
57+
{
58+
throw new ArgumentException(message);
59+
}
60+
}
61+
5362
}

src/Utf8StringSplitter/Utf8Splitter.cs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-

1+
22
using System.Buffers;
33
using System.Runtime.CompilerServices;
44
using System.Runtime.InteropServices;
@@ -12,7 +12,7 @@ public static SplitEnumerator Split(ReadOnlySpan<byte> source, byte separator, U
1212
const Utf8StringSplitOptions AllOptions = Utf8StringSplitOptions.TrimEntries | Utf8StringSplitOptions.RemoveEmptyEntries;
1313
if ((splitOptions & ~AllOptions) != 0)
1414
{
15-
throw new ArgumentException("Utf8StringSplitOptions Value is Invalid.");
15+
ArgumentExceptionEx.Throw("The specified 'Utf8StringSplitOptions' value is not valid.");
1616
}
1717

1818
return new SplitEnumerator(source, separator, splitOptions);
@@ -23,7 +23,7 @@ public static SplitEnumerator Split(ReadOnlySpan<byte> source, ReadOnlySpan<byte
2323
const Utf8StringSplitOptions AllOptions = Utf8StringSplitOptions.TrimEntries | Utf8StringSplitOptions.RemoveEmptyEntries;
2424
if ((splitOptions & ~AllOptions) != 0)
2525
{
26-
throw new ArgumentException("Utf8StringSplitOptions Value is Invalid.");
26+
ArgumentExceptionEx.Throw("The specified 'Utf8StringSplitOptions' value is not valid.");
2727
}
2828

2929
return new SplitEnumerator(source, separator, splitOptions);
@@ -34,7 +34,7 @@ public static SplitAnyEnumerator SplitAny(ReadOnlySpan<byte> source, ReadOnlySpa
3434
const Utf8StringSplitOptions AllOptions = Utf8StringSplitOptions.TrimEntries | Utf8StringSplitOptions.RemoveEmptyEntries;
3535
if ((splitOptions & ~AllOptions) != 0)
3636
{
37-
throw new ArgumentException("Utf8StringSplitOptions Value is Invalid.");
37+
ArgumentExceptionEx.Throw("The specified 'Utf8StringSplitOptions' value is not valid.");
3838
}
3939

4040
switch(separatorOptions)
@@ -43,7 +43,8 @@ public static SplitAnyEnumerator SplitAny(ReadOnlySpan<byte> source, ReadOnlySpa
4343
case Utf8StringSeparatorOptions.Bytes:
4444
return new SplitAnyEnumerator(source, separators, splitOptions, separatorOptions);
4545
default:
46-
throw new ArgumentException("Utf8StringSeparatorOptions Value is Invalid.");
46+
ArgumentExceptionEx.Throw("The specified 'Utf8StringSeparatorOptions' value is not valid.");
47+
return default; // It will not reach here.
4748
}
4849
}
4950
}
@@ -105,7 +106,7 @@ internal SplitEnumerator(ReadOnlySpan<byte> source, ReadOnlySpan<byte> separator
105106

106107
public readonly SplitEnumerator GetEnumerator() => this;
107108

108-
internal readonly int sourceLength => source.Length;
109+
internal readonly int SourceLength => source.Length;
109110

110111
public bool MoveNext()
111112
{
@@ -122,7 +123,7 @@ public bool MoveNext()
122123

123124
public readonly byte[][] ToArray()
124125
{
125-
var writer = new ExtendableArray<byte[]>(sourceLength);
126+
var writer = new ExtendableArray<byte[]>(SourceLength);
126127
foreach (var i in this)
127128
{
128129
writer.Add(i.ToArray());
@@ -133,7 +134,7 @@ public readonly byte[][] ToArray()
133134

134135
public readonly string[] ToUtf16Array()
135136
{
136-
var writer = new ExtendableArray<string>(sourceLength);
137+
var writer = new ExtendableArray<string>(SourceLength);
137138
foreach (var i in this)
138139
{
139140
writer.Add(UTF8Ex.GetString(i));
@@ -207,8 +208,6 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan<byte> separator)
207208
var index = source.IndexOf(separator);
208209
if (index < 0 || separator.Length == 0) // end
209210
{
210-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, startIndex, endIndex);
211-
212211
if (trimEntries)
213212
{
214213
if (source.Length == 1 && source[0] == 0x20)
@@ -217,6 +216,7 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan<byte> separator)
217216
}
218217
else
219218
{
219+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, startIndex, endIndex);
220220
source = source[startIndex..endIndex];
221221
}
222222
}
@@ -235,13 +235,13 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan<byte> separator)
235235
source = source[index..];
236236
if (trimEntries)
237237
{
238-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
239238
if (current.Length == 1 && current[0] == 0x20)
240239
{
241240
current = ReadOnlySpan<byte>.Empty;
242241
}
243242
else
244243
{
244+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
245245
current = current[startIndex..endIndex];
246246
}
247247
}
@@ -255,8 +255,6 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan<byte> separator)
255255
index = source.IndexOf(separator);
256256
if (index < 0) // end
257257
{
258-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, 0, source.Length);
259-
260258
if (trimEntries)
261259
{
262260
if (source.Length == 1 && source[0] == 0x20)
@@ -265,6 +263,7 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan<byte> separator)
265263
}
266264
else
267265
{
266+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(source, 0, source.Length);
268267
source = source[startIndex..endIndex];
269268
}
270269
}
@@ -283,13 +282,13 @@ private bool MoveNextInternalWithOptions(scoped ReadOnlySpan<byte> separator)
283282
source = source[index..];
284283
if (trimEntries)
285284
{
286-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
287285
if (current.Length == 1 && current[0] == 0x20)
288286
{
289287
current = ReadOnlySpan<byte>.Empty;
290288
}
291289
else
292290
{
291+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
293292
current = current[startIndex..endIndex];
294293
}
295294
}
@@ -437,8 +436,6 @@ private bool MoveNextWithOptions()
437436
}
438437
if (index < 0) // end
439438
{
440-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, startIndex, endIndex);
441-
442439
if (trimEntries)
443440
{
444441
if (target.Length == 1 && target[0] == 0x20)
@@ -447,6 +444,7 @@ private bool MoveNextWithOptions()
447444
}
448445
else
449446
{
447+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, startIndex, endIndex);
450448
target = target[startIndex..endIndex];
451449
}
452450
}
@@ -465,13 +463,13 @@ private bool MoveNextWithOptions()
465463
target = target[index..];
466464
if (trimEntries)
467465
{
468-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
469466
if (current.Length == 1 && current[0] == 0x20)
470467
{
471468
current = ReadOnlySpan<byte>.Empty;
472469
}
473470
else
474471
{
472+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
475473
current = current[startIndex..endIndex];
476474
}
477475
}
@@ -495,8 +493,6 @@ private bool MoveNextWithOptions()
495493
}
496494
if (index < 0) // end
497495
{
498-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, 0, target.Length);
499-
500496
if (trimEntries)
501497
{
502498
if (target.Length == 1 && target[0] == 0x20)
@@ -505,6 +501,7 @@ private bool MoveNextWithOptions()
505501
}
506502
else
507503
{
504+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(target, 0, target.Length);
508505
target = target[startIndex..endIndex];
509506
}
510507
}
@@ -523,13 +520,13 @@ private bool MoveNextWithOptions()
523520
target = target[index..];
524521
if (trimEntries)
525522
{
526-
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
527523
if (current.Length == 1 && current[0] == 0x20)
528524
{
529525
current = ReadOnlySpan<byte>.Empty;
530526
}
531527
else
532528
{
529+
(startIndex, endIndex) = Utf8StringUtility.TrimSplitEntries(current, 0, current.Length);
533530
current = current[startIndex..endIndex];
534531
}
535532
}

src/Utf8StringSplitter/Utf8StringSplitter.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0</TargetFrameworks>
3+
<TargetFrameworks>netstandard2.0;netstandard2.1;net6.0;net8.0;net9.0;net10.0;</TargetFrameworks>
44
<ImplicitUsings>enable</ImplicitUsings>
5-
<LangVersion>12</LangVersion>
5+
<LangVersion>13</LangVersion>
66
<Nullable>enable</Nullable>
77
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
8-
<VersionPrefix>1.0.1</VersionPrefix>
8+
<VersionPrefix>1.1.0</VersionPrefix>
99
<Authors>prozolic</Authors>
1010
<Copyright>prozolic</Copyright>
1111
<PackageProjectUrl>https://github.com/prozolic/Utf8StringSplitter</PackageProjectUrl>

tests/Utf8StringSplitter.Tests/Utf8SplitterTest.cs

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using Shouldly;
22
using System;
33
using System.Diagnostics;
4+
using System.Reflection;
45
using System.Text;
6+
using System.Xml;
57

68
namespace Utf8StringSplitter.Tests
79
{
@@ -1117,14 +1119,14 @@ public void SplitAnyWithTrimEntriesAndRemoveEmptyEntriesTest2()
11171119
[Fact]
11181120
public void SplitAnyWithUtf8SeparatorOptionsTest()
11191121
{
1120-
var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; //あいうえうお
1121-
var separator = new byte[] { 227, 129, 134 }; //う
1122+
var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; //あいうえうお
1123+
var separator = new byte[] { 227, 129, 134 }; //う
11221124
{
11231125
var expected = new List<byte[]>()
11241126
{
1125-
new byte[] { 227, 129, 130, 227, 129, 132 }, // あい
1126-
new byte[] { 227, 129, 136}, // え
1127-
new byte[] { 227, 129, 138}, // お
1127+
new byte[] { 227, 129, 130, 227, 129, 132 }, // あい
1128+
new byte[] { 227, 129, 136}, // え
1129+
new byte[] { 227, 129, 138}, // お
11281130
};
11291131
var index = 0;
11301132
foreach (var s in Utf8Splitter.SplitAny(source, separator))
@@ -1154,8 +1156,8 @@ public void SplitAnyWithUtf8SeparatorOptionsTest()
11541156
[Fact]
11551157
public void SplitAnyWithBytesSeparatorOptionsTest()
11561158
{
1157-
var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; //あいうえうお
1158-
var separator = new byte[] { 227, 129, 134 }; //う
1159+
var source = new byte[] { 227, 129, 130, 227, 129, 132, 227, 129, 134, 227, 129, 136, 227, 129, 134, 227, 129, 138 }; //あいうえうお
1160+
var separator = new byte[] { 227, 129, 134 }; //う
11591161
{
11601162
var actual = new List<byte[]>();
11611163
var expected = new List<byte[]>();
@@ -1238,5 +1240,38 @@ public void ToUtf16ArrayTest2()
12381240
result.Length.ShouldBe(expected.Length);
12391241
}
12401242

1243+
[Fact]
1244+
public void ThrowArgumentExceptionTest()
1245+
{
1246+
{
1247+
var ex = Should.Throw<ArgumentException>(() =>
1248+
{
1249+
_ = Utf8Splitter.Split("1,2,3,4,5"u8, (byte)',', splitOptions:(Utf8StringSplitOptions)(-1));
1250+
});
1251+
ex.Message.ShouldBe("The specified 'Utf8StringSplitOptions' value is not valid.");
1252+
}
1253+
{
1254+
var ex = Should.Throw<ArgumentException>(() =>
1255+
{
1256+
_ = Utf8Splitter.Split("1,2,3,4,5"u8, ","u8, splitOptions:(Utf8StringSplitOptions)(-1));
1257+
});
1258+
ex.Message.ShouldBe("The specified 'Utf8StringSplitOptions' value is not valid.");
1259+
}
1260+
{
1261+
var ex = Should.Throw<ArgumentException>(() =>
1262+
{
1263+
_ = Utf8Splitter.SplitAny("1,2-3;4-5"u8, "-,;"u8, splitOptions:(Utf8StringSplitOptions)(-1));
1264+
});
1265+
ex.Message.ShouldBe("The specified 'Utf8StringSplitOptions' value is not valid.");
1266+
}
1267+
{
1268+
var ex = Should.Throw<ArgumentException>(() =>
1269+
{
1270+
_ = Utf8Splitter.SplitAny("1,2-3;4-5"u8, "-,;"u8, separatorOptions:(Utf8StringSeparatorOptions)(-1));
1271+
});
1272+
ex.Message.ShouldBe("The specified 'Utf8StringSeparatorOptions' value is not valid.");
1273+
}
1274+
}
1275+
12411276
}
12421277
}

tests/Utf8StringSplitter.Tests/Utf8StringSplitter.Tests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFrameworks Condition="'$(OS)' == 'Windows_NT'">net48</TargetFrameworks>
5-
<TargetFrameworks>$(TargetFrameworks);net6.0;net8.0</TargetFrameworks>
5+
<TargetFrameworks>$(TargetFrameworks);net6.0;net8.0;net9.0;net10.0</TargetFrameworks>
66
<ImplicitUsings>enable</ImplicitUsings>
77
<Nullable>enable</Nullable>
88
<LangVersion>12.0</LangVersion>

0 commit comments

Comments
 (0)