Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.

StringBuilder.AppendJoin (appending lists to StringBuilder) #8350

Merged
merged 3 commits into from
Dec 9, 2016
Merged
Changes from 2 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
83 changes: 83 additions & 0 deletions src/mscorlib/src/System/Text/StringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace System.Text {
using System.Threading;
using System.Globalization;
using System.Diagnostics.Contracts;
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
Expand Down Expand Up @@ -1011,6 +1012,88 @@ public StringBuilder Append(char[] value) {
return this;
}

// Append joined values with a separator between each value.
public unsafe StringBuilder AppendJoin<T>(char separator, params T[] values)
{
// Defer argument validation to the internal function
return AppendJoinCore(&separator, 1, values);
}

public unsafe StringBuilder AppendJoin<T>(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<T>(char separator, IEnumerable<T> values)
{
// Defer argument validation to the internal function
return AppendJoinCore(&separator, 1, values);
}

public unsafe StringBuilder AppendJoin<T>(string separator, IEnumerable<T> 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<T>(char* separator, int separatorLength, params T[] values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
Contract.Ensures(Contract.Result<StringBuilder>() != 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<T>(char* separator, int separatorLength, IEnumerable<T> values)
{
if (values == null)
throw new ArgumentNullException(nameof(values));
Contract.Ensures(Contract.Result<StringBuilder>() != 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====================================
**
==============================================================================*/
Expand Down