Skip to content
This repository was archived by the owner on Nov 1, 2020. It is now read-only.
Merged
Changes from all 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
30 changes: 17 additions & 13 deletions src/System.Private.CoreLib/src/System/String.cs
Original file line number Diff line number Diff line change
Expand Up @@ -421,29 +421,28 @@ public static String Join(String separator, IEnumerable<String> values)
if (values == null)
throw new ArgumentNullException("values");

if (separator == null)
separator = String.Empty;


using (IEnumerator<String> en = values.GetEnumerator())
{
if (!en.MoveNext())
return String.Empty;

StringBuilder result = StringBuilderCache.Acquire();
if (en.Current != null)
String firstValue = en.Current;

if (!en.MoveNext())
{
result.Append(en.Current);
// Only one value available
return firstValue ?? String.Empty;
}

while (en.MoveNext())
// Null separator and values are handled by the StringBuilder
StringBuilder result = StringBuilderCache.Acquire();
result.Append(firstValue);

do
{
result.Append(separator);
if (en.Current != null)
{
result.Append(en.Current);
}
}
result.Append(en.Current);
} while (en.MoveNext());
return StringBuilderCache.GetStringAndRelease(result);
}
}
Expand Down Expand Up @@ -476,6 +475,11 @@ public unsafe static String Join(String separator, String[] value, int startInde
return String.Empty;
}

if (count == 1)
{
return value[startIndex] ?? String.Empty;
}

int endIndex = startIndex + count - 1;
StringBuilder result = StringBuilderCache.Acquire();
// Append the first string first and then append each following string prefixed by the separator.
Expand Down