Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String.Split() trim last substring if specified #81331

Merged
merged 4 commits into from
Feb 16, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -3353,8 +3353,8 @@ public static int SplitAny(this ReadOnlySpan<char> source, Span<Range> destinati
string.CheckStringSplitOptions(options);

// If the separators list is empty, whitespace is used as separators. In that case, we want to ignore TrimEntries if specified,
// since TrimEntries also impacts whitespace.
if (separators.IsEmpty)
// since TrimEntries also impacts whitespace. The TrimEntries flag must be left intact if we are constrained by count because we need to process last substring.
if (separators.IsEmpty && destination.Length > source.Length)
stephentoub marked this conversation as resolved.
Show resolved Hide resolved
{
options &= ~StringSplitOptions.TrimEntries;
}
Expand Down Expand Up @@ -3394,8 +3394,8 @@ public static int SplitAny(this ReadOnlySpan<char> source, Span<Range> destinati
string.CheckStringSplitOptions(options);

// If the separators list is empty, whitespace is used as separators. In that case, we want to ignore TrimEntries if specified,
// since TrimEntries also impacts whitespace.
if (separators.IsEmpty)
// since TrimEntries also impacts whitespace. The TrimEntries flag must be left intact if we are constrained by count because we need to process last substring.
if (separators.IsEmpty && destination.Length > source.Length)
{
options &= ~StringSplitOptions.TrimEntries;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1508,9 +1508,10 @@ private string[] SplitInternal(ReadOnlySpan<char> separators, int count, StringS
return CreateSplitArrayOfThisAsSoleValue(options, count);
}

if (separators.IsEmpty)
if (separators.IsEmpty && count > Length)
{
// Caller is already splitting on whitespace; no need for separate trim step
// Caller is already splitting on whitespace; no need for separate trim step if the count is sufficient
// to examine the whole input.
options &= ~StringSplitOptions.TrimEntries;
}

Expand Down
20 changes: 20 additions & 0 deletions src/libraries/System.Runtime/tests/System/String.SplitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,8 @@ public static void SplitNoMatchSingleResult()
[InlineData(" ", ',', M, StringSplitOptions.TrimEntries, new[] { "" })]
[InlineData(" ", ',', M, StringSplitOptions.RemoveEmptyEntries, new[] { " " })]
[InlineData(" ", ',', M, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new string[0])]
[InlineData("a b ", ' ', 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData(" a b ", ' ', 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
public static void SplitCharSeparator(string value, char separator, int count, StringSplitOptions options, string[] expected)
{
Assert.Equal(expected, value.Split(separator, count, options));
Expand Down Expand Up @@ -550,6 +552,12 @@ public static void SplitCharSeparator(string value, char separator, int count, S
[InlineData(" ", ",", M, StringSplitOptions.TrimEntries, new[] { "" })]
[InlineData(" ", ",", M, StringSplitOptions.RemoveEmptyEntries, new[] { " " })]
[InlineData(" ", ",", M, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new string[0])]
[InlineData("a b ", null, 2, StringSplitOptions.TrimEntries, new[] { "a b" })]
[InlineData("a b ", "", 2, StringSplitOptions.TrimEntries, new[] { "a b" })]
[InlineData("a b ", " ", 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData(" a b ", null, 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a b" })]
[InlineData(" a b ", "", 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a b" })]
[InlineData(" a b ", " ", 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
public static void SplitStringSeparator(string value, string separator, int count, StringSplitOptions options, string[] expected)
{
Assert.Equal(expected, value.Split(separator, count, options));
Expand Down Expand Up @@ -610,6 +618,12 @@ public static void SplitNullCharArraySeparator_BindsToCharArrayOverload()
[InlineData(" ", new[] { ',', ':' }, M, StringSplitOptions.TrimEntries, new[] { "" })]
[InlineData(" ", new[] { ',', ':' }, M, StringSplitOptions.RemoveEmptyEntries, new[] { " " })]
[InlineData(" ", new[] { ',', ':' }, M, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new string[0])]
[InlineData("a b ", null, 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData("a b ", new char[0], 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData("a b ", new char[] { ' ' }, 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData(" a b ", null, 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
[InlineData(" a b ", new char[0], 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
[InlineData(" a b ", new char[] { ' ' }, 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
public static void SplitCharArraySeparator(string value, char[] separators, int count, StringSplitOptions options, string[] expected)
{
Assert.Equal(expected, value.Split(separators, count, options));
Expand Down Expand Up @@ -654,6 +668,12 @@ public static void SplitCharArraySeparator(string value, char[] separators, int
[InlineData(" ", new[] { ",", ":" }, M, StringSplitOptions.TrimEntries, new[] { "" })]
[InlineData(" ", new[] { ",", ":" }, M, StringSplitOptions.RemoveEmptyEntries, new[] { " " })]
[InlineData(" ", new[] { ",", ":" }, M, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new string[0])]
[InlineData("a b ", null, 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData("a b ", new string[0], 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData("a b ", new string[] { " " }, 2, StringSplitOptions.TrimEntries, new[] { "a", "b" })]
[InlineData(" a b ", null, 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
[InlineData(" a b ", new string[0], 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
[InlineData(" a b ", new string[] { " " }, 2, StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries, new[] { "a", "b" })]
public static void SplitStringArraySeparator(string value, string[] separators, int count, StringSplitOptions options, string[] expected)
{
Assert.Equal(expected, value.Split(separators, count, options));
Expand Down