From 5fe1c0ae8eb12cf04caca09a8b4a798b87eb8408 Mon Sep 17 00:00:00 2001 From: Bruce Bowyer-Smyth Date: Fri, 22 Apr 2016 19:47:45 +1000 Subject: [PATCH] Fixes https://github.com/dotnet/coreclr/issues/4252 (#4384) Change Join and Concat to access iterator.Current value once. Delegate null checking to StringBuilder.Append --- src/mscorlib/src/System/String.cs | 49 ++++++++++++--------------------------- 1 file changed, 15 insertions(+), 34 deletions(-) diff --git a/src/mscorlib/src/System/String.cs b/src/mscorlib/src/System/String.cs index b17baa9..3d2ac0f 100644 --- a/src/mscorlib/src/System/String.cs +++ b/src/mscorlib/src/System/String.cs @@ -93,22 +93,14 @@ namespace System { if (values.Length == 0 || values[0] == null) return String.Empty; - if (separator == null) - separator = String.Empty; - StringBuilder result = StringBuilderCache.Acquire(); - String value = values[0].ToString(); - if (value != null) - result.Append(value); + result.Append(values[0].ToString()); for (int i = 1; i < values.Length; i++) { result.Append(separator); if (values[i] != null) { - // handle the case where their ToString() override is broken - value = values[i].ToString(); - if (value != null) - result.Append(value); + result.Append(values[i].ToString()); } } return StringBuilderCache.GetStringAndRelease(result); @@ -121,30 +113,23 @@ namespace System { Contract.Ensures(Contract.Result() != null); Contract.EndContractBlock(); - if (separator == null) - separator = String.Empty; - using(IEnumerator en = values.GetEnumerator()) { if (!en.MoveNext()) return String.Empty; StringBuilder result = StringBuilderCache.Acquire(); - if (en.Current != null) { - // handle the case that the enumeration has null entries - // and the case where their ToString() override is broken - string value = en.Current.ToString(); - if (value != null) - result.Append(value); + T currentValue = en.Current; + + if (currentValue != null) { + result.Append(currentValue.ToString()); } while (en.MoveNext()) { + currentValue = en.Current; + result.Append(separator); - if (en.Current != null) { - // handle the case that the enumeration has null entries - // and the case where their ToString() override is broken - string value = en.Current.ToString(); - if (value != null) - result.Append(value); + if (currentValue != null) { + result.Append(currentValue.ToString()); } } return StringBuilderCache.GetStringAndRelease(result); @@ -3202,12 +3187,10 @@ namespace System { StringBuilder result = StringBuilderCache.Acquire(); using(IEnumerator en = values.GetEnumerator()) { while (en.MoveNext()) { - if (en.Current != null) { - // handle the case that the enumeration has null entries - // and the case where their ToString() override is broken - string value = en.Current.ToString(); - if (value != null) - result.Append(value); + T currentValue = en.Current; + + if (currentValue != null) { + result.Append(currentValue.ToString()); } } } @@ -3225,9 +3208,7 @@ namespace System { StringBuilder result = StringBuilderCache.Acquire(); using(IEnumerator en = values.GetEnumerator()) { while (en.MoveNext()) { - if (en.Current != null) { - result.Append(en.Current); - } + result.Append(en.Current); } } return StringBuilderCache.GetStringAndRelease(result); -- 2.7.4