From 14f600666b5622c63f43e6ee52946fbee1e38324 Mon Sep 17 00:00:00 2001 From: Oliver Booth Date: Thu, 14 Jan 2021 17:09:39 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=8E=20Improve=20performance=20for=20st?= =?UTF-8?q?ring.Reverse?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Item for #12 --- X10D/src/StringExtensions.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/X10D/src/StringExtensions.cs b/X10D/src/StringExtensions.cs index 93920a1..aa05785 100644 --- a/X10D/src/StringExtensions.cs +++ b/X10D/src/StringExtensions.cs @@ -265,7 +265,27 @@ /// A whose characters are that of in reverse order. public static string Reverse(this string value) { - return string.Join(string.Empty, Enumerable.Reverse(value)); + if (value is null) + { + throw new ArgumentNullException(nameof(value)); + } + + if (value.Length == 1) + { + return value; + } + + unsafe + { + var array = stackalloc char[value.Length]; + + for (var index = 0; index < value.Length; index++) + { + array[index] = value[value.Length - index - 1]; + } + + return new string(array); + } } ///