Skip to content

Commit da56054

Browse files
authored
Add String.Create. (#424)
* Delete the redundant characters. * Add String.Create.
1 parent 7ac44a0 commit da56054

File tree

6 files changed

+103
-1
lines changed

6 files changed

+103
-1
lines changed

apiCount.include.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
**API count: 642**
1+
**API count: 643**

api_list.include.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,7 @@
704704
* `string[] Split(string, StringSplitOptions)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=net-10.0#system-string-split(system-string-system-stringsplitoptions))
705705
* `bool StartsWith(char)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.startswith?view=net-10.0#system-string-startswith(system-char))
706706
* `bool TryCopyTo(Span<char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.trycopyto?view=net-10.0)
707+
* `string Create<TState>(int, TState, System.Buffers.SpanAction<char, TState>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.create?view=net-10.0#system-string-create-1(system-int32-0-system-buffers-spanaction((system-char-0))))
707708
* `int GetHashCode(ReadOnlySpan<char>, StringComparison)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.gethashcode?view=net-10.0#system-string-gethashcode(system-readonlyspan((system-char))-system-stringcomparison))
708709
* `int GetHashCode(ReadOnlySpan<char>)` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.gethashcode?view=net-10.0#system-string-gethashcode(system-readonlyspan((system-char))))
709710
* `string Join(char, object?[])` [reference](https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=net-10.0#system-string-join(system-char-system-object()))

src/Polyfill/ReadOnlySpanAction.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
#if FeatureMemory
5+
#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2X
6+
#nullable enable
7+
8+
namespace System.Buffers;
9+
10+
using System.Diagnostics;
11+
using System.Diagnostics.CodeAnalysis;
12+
13+
#if PolyUseEmbeddedAttribute
14+
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
15+
#endif
16+
// Encapsulates a method that receives a read-only span of objects of type T and a state object of type TArg.
17+
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.readonlyspanaction-2?view=net-10.0
18+
#if PolyPublic
19+
public
20+
#endif
21+
delegate void ReadOnlySpanAction<T, in TArg>(ReadOnlySpan<T> span, TArg arg);
22+
#else
23+
using System.Buffers;
24+
using System.Runtime.CompilerServices;
25+
[assembly: TypeForwardedTo(typeof(ReadOnlySpanAction<,>))]
26+
#endif
27+
#endif

src/Polyfill/SpanAction.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// <auto-generated />
2+
#pragma warning disable
3+
4+
#if FeatureMemory
5+
#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2X
6+
#nullable enable
7+
8+
namespace System.Buffers;
9+
10+
using System.Diagnostics;
11+
using System.Diagnostics.CodeAnalysis;
12+
13+
#if PolyUseEmbeddedAttribute
14+
[global::Microsoft.CodeAnalysis.EmbeddedAttribute]
15+
#endif
16+
// Encapsulates a method that receives a span of objects of type T and a state object of type TArg.
17+
// https://learn.microsoft.com/en-us/dotnet/api/system.buffers.spanaction-2?view=net-10.0
18+
#if PolyPublic
19+
public
20+
#endif
21+
delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
22+
#else
23+
using System.Buffers;
24+
using System.Runtime.CompilerServices;
25+
[assembly: TypeForwardedTo(typeof(SpanAction<,>))]
26+
#endif
27+
#endif

src/Polyfill/StringPolyfill.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// <auto-generated />
2+
23
#pragma warning disable
34

45
#if !NET9_0_OR_GREATER
@@ -217,6 +218,43 @@ public static string Join(char separator, string?[] value, int startIndex, int c
217218
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.string.join?view=net-10.0#system-string-join-1(system-char-system-collections-generic-ienumerable((-0)))
218219
public static string Join<T>(char separator, IEnumerable<T> values) =>
219220
string.Join(new(separator, 1), values);
221+
#endif
222+
#if NETFRAMEWORK || NETSTANDARD2_0 || NETCOREAPP2X
223+
#if FeatureMemory
224+
/// <summary>
225+
/// Creates a new string with a specific length and initializes it after creation by using the specified callback.
226+
/// </summary>
227+
//Link: https://learn.microsoft.com/en-us/dotnet/api/system.string.create?view=net-10.0#system-string-create-1(system-int32-0-system-buffers-spanaction((system-char-0)))
228+
public static string Create<TState>(int length, TState state, System.Buffers.SpanAction<char, TState> action)
229+
{
230+
#if AllowUnsafeBlocks
231+
var str = new string('\0', length);
232+
233+
unsafe
234+
{
235+
fixed (char* strPtr = str)
236+
{
237+
action(new Span<char>(strPtr, length), state);
238+
}
239+
}
240+
241+
return str;
242+
#else
243+
var chars = System.Buffers.ArrayPool<char>.Shared.Rent(length);
244+
245+
try
246+
{
247+
action(chars.AsSpan(0, length), state);
248+
249+
return new string(chars, 0, length);
250+
}
251+
finally
252+
{
253+
System.Buffers.ArrayPool<char>.Shared.Return(chars);
254+
}
255+
#endif
256+
}
257+
#endif
220258
#endif
221259
}
222260
}

src/Tests/StringPolyfillTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,13 @@ public void Join()
1313
// ReSharper disable once RedundantCast
1414
Assert.AreEqual("bac", string.Join('a', (IEnumerable<string>) new List<string>{"b", "c"}));
1515
}
16+
17+
[Test]
18+
public void Create() => Assert.AreEqual("abcde", string.Create(5, 'a', (span, state) =>
19+
{
20+
for (var i = 0; i < span.Length; i++)
21+
{
22+
span[i] = state++;
23+
}
24+
}));
1625
}

0 commit comments

Comments
 (0)