From 02394ece0c070d61ccbc55b2104486d7b8f7600c Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Thu, 7 Sep 2017 21:42:42 -0400 Subject: [PATCH] Add String.Create span-based method --- src/mscorlib/shared/System/Action.cs | 6 ++++++ src/mscorlib/src/System/String.cs | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/mscorlib/shared/System/Action.cs b/src/mscorlib/shared/System/Action.cs index b82c14d9dcdd..da6813829e3a 100644 --- a/src/mscorlib/shared/System/Action.cs +++ b/src/mscorlib/shared/System/Action.cs @@ -33,3 +33,9 @@ namespace System public delegate bool Predicate(T obj); } + +namespace System.Buffers +{ + public delegate void SpanAction(Span span, TArg arg); + public delegate void ReadOnlySpanAction(ReadOnlySpan span, TArg arg); +} diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs index 9520cce6641e..c74abe990361 100644 --- a/src/mscorlib/src/System/String.cs +++ b/src/mscorlib/src/System/String.cs @@ -16,6 +16,7 @@ namespace System { using System.Text; using System; + using System.Buffers; using System.Runtime; using System.Runtime.ConstrainedExecution; using System.Globalization; @@ -684,6 +685,28 @@ private unsafe string CtorReadOnlySpanOfChar(ReadOnlySpan value) return result; } + public static string Create(int length, TState state, SpanAction action) + { + if (action == null) + { + throw new ArgumentNullException(nameof(action)); + } + + if (length > 0) + { + string result = FastAllocateString(length); + action(new Span(ref result.GetRawStringData(), length), state); + return result; + } + + if (length == 0) + { + return Empty; + } + + throw new ArgumentOutOfRangeException(nameof(length)); + } + // Returns this string. public override String ToString() {