Skip to content

Commit 7727946

Browse files
committed
Use string.Create for Repeat extension on netstandard2.1+.
1 parent f85bf9b commit 7727946

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

ClassLibraries/Macross.Extensions/Code/Extensions/System/StringExtensions.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ public static string Repeat(this char value, int repetitions)
3232
/// <returns>String built by repeating the specified string.</returns>
3333
public static string Repeat(this string value, int repetitions)
3434
{
35+
#if NETSTANDARD2_0
3536
if (value == null)
3637
throw new ArgumentNullException(nameof(value));
37-
if (repetitions < 0)
38+
if (repetitions < 0 || repetitions >= int.MaxValue)
3839
throw new ArgumentOutOfRangeException(nameof(repetitions));
3940

4041
int Length = value.Length;
@@ -45,6 +46,23 @@ public static string Repeat(this string value, int repetitions)
4546
value.CopyTo(0, Characters, p, Length);
4647

4748
return new string(Characters);
49+
#else
50+
return value == null
51+
? throw new ArgumentNullException(nameof(value))
52+
: repetitions < 0 || repetitions >= int.MaxValue
53+
? throw new ArgumentOutOfRangeException(nameof(repetitions))
54+
: string.Create(value.Length * (repetitions + 1), (value, repetitions), BuildString);
55+
56+
static void BuildString(Span<char> destination, (string Value, int Repetitions) state)
57+
{
58+
int repetitions = state.Repetitions;
59+
ReadOnlySpan<char> value = state.Value.AsSpan();
60+
int length = value.Length;
61+
62+
for (int i = 0, p = 0; i <= repetitions; i++, p += length)
63+
value.CopyTo(destination[p..]);
64+
}
65+
#endif
4866
}
4967

5068
/// <summary>

ClassLibraries/Macross.Extensions/Code/Properties/AssemblyInfo.cs

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

88
[assembly: Guid("6468b8f5-b893-4216-9a0f-64a1a7f9112a")]
99

10-
[assembly: AssemblyVersion("0.0.1.20121")]
10+
[assembly: AssemblyVersion("0.0.1.21206")]

0 commit comments

Comments
 (0)