Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/mscorlib/shared/System/Action.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,9 @@ namespace System

public delegate bool Predicate<in T>(T obj);
}

namespace System.Buffers
{
public delegate void SpanAction<T, in TArg>(Span<T> span, TArg arg);
public delegate void ReadOnlySpanAction<T, in TArg>(ReadOnlySpan<T> span, TArg arg);
}
23 changes: 23 additions & 0 deletions src/mscorlib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace System
{
using System.Text;
using System;
using System.Buffers;
using System.Runtime;
using System.Runtime.ConstrainedExecution;
using System.Globalization;
Expand Down Expand Up @@ -684,6 +685,28 @@ private unsafe string CtorReadOnlySpanOfChar(ReadOnlySpan<char> value)
return result;
}

public static string Create<TState>(int length, TState state, SpanAction<char, TState> action)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throw ArgumentNullException if action is null?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup.

{
if (action == null)
{
throw new ArgumentNullException(nameof(action));
}

if (length > 0)
{
string result = FastAllocateString(length);
action(new Span<char>(ref result.GetRawStringData(), length), state);
return result;
}

if (length == 0)
{
return Empty;
}

throw new ArgumentOutOfRangeException(nameof(length));
}

// Returns this string.
public override String ToString()
{
Expand Down