From 9c76462a44f9ce34912f7ce6a2694e203bad662c Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Tue, 29 Nov 2016 18:13:56 +0700 Subject: [PATCH 1/3] AppendJoin --- src/mscorlib/src/System/Text/StringBuilder.cs | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) diff --git a/src/mscorlib/src/System/Text/StringBuilder.cs b/src/mscorlib/src/System/Text/StringBuilder.cs index f525118fa4b5..cc17afd005fd 100644 --- a/src/mscorlib/src/System/Text/StringBuilder.cs +++ b/src/mscorlib/src/System/Text/StringBuilder.cs @@ -21,6 +21,7 @@ namespace System.Text { using System.Threading; using System.Globalization; using System.Diagnostics.Contracts; + using Collections.Generic; // This class represents a mutable string. It is convenient for situations in // which it is desirable to modify a string, perhaps by removing, replacing, or @@ -1011,6 +1012,88 @@ public StringBuilder Append(char[] value) { return this; } + // Append joined values with a separator between each value. + public unsafe StringBuilder AppendJoin(char separator, params T[] values) + { + // Defer argument validation to the internal function + return AppendJoinCore(&separator, 1, values); + } + + public unsafe StringBuilder AppendJoin(string separator, params T[] values) + { + separator = separator ?? string.Empty; + fixed (char* pSeparator = separator) + { + // Defer argument validation to the internal function + return AppendJoinCore(pSeparator, separator.Length, values); + } + } + + public unsafe StringBuilder AppendJoin(char separator, IEnumerable values) + { + // Defer argument validation to the internal function + return AppendJoinCore(&separator, 1, values); + } + + public unsafe StringBuilder AppendJoin(string separator, IEnumerable values) + { + separator = separator ?? string.Empty; + fixed (char* pSeparator = separator) + { + // Defer argument validation to the internal function + return AppendJoinCore(pSeparator, separator.Length, values); + } + } + + private unsafe StringBuilder AppendJoinCore(char* separator, int separatorLength, params T[] values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + Contract.Ensures(Contract.Result() != null); + + if (values.Length == 0) + return this; + + var value = values[0]; + if (value != null) + Append(value.ToString()); + + for (var i = 1; i < values.Length; i++) + { + Append(separator, separatorLength); + value = values[i]; + if (value != null) + Append(value.ToString()); + } + return this; + } + + private unsafe StringBuilder AppendJoinCore(char* separator, int separatorLength, IEnumerable values) + { + if (values == null) + throw new ArgumentNullException(nameof(values)); + Contract.Ensures(Contract.Result() != null); + + using (var en = values.GetEnumerator()) + { + if (!en.MoveNext()) + return this; + + var value = en.Current; + if (value != null) + Append(value.ToString()); + + while (en.MoveNext()) + { + Append(separator, separatorLength); + value = en.Current; + if (value != null) + Append(value.ToString()); + } + } + return this; + } + /*====================================Insert==================================== ** ==============================================================================*/ From 82d82c638c3bd2ded8b0f3fa482002ff3adfff65 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Tue, 29 Nov 2016 23:02:38 +0700 Subject: [PATCH 2/3] using System.Collections.Generic --- src/mscorlib/src/System/Text/StringBuilder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/mscorlib/src/System/Text/StringBuilder.cs b/src/mscorlib/src/System/Text/StringBuilder.cs index cc17afd005fd..e81d90c72281 100644 --- a/src/mscorlib/src/System/Text/StringBuilder.cs +++ b/src/mscorlib/src/System/Text/StringBuilder.cs @@ -21,7 +21,7 @@ namespace System.Text { using System.Threading; using System.Globalization; using System.Diagnostics.Contracts; - using Collections.Generic; + using System.Collections.Generic; // This class represents a mutable string. It is convenient for situations in // which it is desirable to modify a string, perhaps by removing, replacing, or From 9fc248ff00b585598f67b772240d8aafae996a75 Mon Sep 17 00:00:00 2001 From: Alexander Radchenko Date: Fri, 9 Dec 2016 06:52:02 +0700 Subject: [PATCH 3/3] model.xml --- src/mscorlib/model.xml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mscorlib/model.xml b/src/mscorlib/model.xml index 71c8cab22ec3..7abb94cd4cd5 100644 --- a/src/mscorlib/model.xml +++ b/src/mscorlib/model.xml @@ -7855,6 +7855,10 @@ + + + +