Skip to content
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
53 changes: 38 additions & 15 deletions src/Polyfill/Polyfill_Memory_SplitRanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,22 +246,10 @@ static bool FindNextSeparator(
break;

case SplitRangesMode.AnyStrings:
// Per-separator BCL IndexOf, then keep the leftmost match. Ties resolve by array order
// (first separator wins) — matching the BCL's MakeSeparatorListAny behaviour.
for (var s = 0; s < stringSeparators.Length; s++)
idx = IndexOfAnyStringSeparator(slice, stringSeparators, out var stringSeparatorLength);
if (idx >= 0)
{
var sep = stringSeparators[s];
if (string.IsNullOrEmpty(sep))
{
continue;
}

var found = slice.IndexOf(sep.AsSpan());
if (found >= 0 && (idx < 0 || found < idx))
{
idx = found;
len = sep.Length;
}
len = stringSeparatorLength;
}

break;
Expand All @@ -283,6 +271,41 @@ static bool FindNextSeparator(
return true;
}

/// <summary>
/// Finds the leftmost position in <paramref name="span"/> at which one of <paramref name="separators"/> occurs.
/// Walks the positions once, testing the separators in array order at each one, so ties resolve to the first
/// separator in the array, matching the BCL's MakeSeparatorListAny behaviour. Searching the whole span once per
/// separator instead would rescan it for every separator that is rare or absent, on every call.
/// </summary>
static int IndexOfAnyStringSeparator(ReadOnlySpan<char> span, ReadOnlySpan<string> separators, out int separatorLength)
{
for (var index = 0; index < span.Length; index++)
{
var current = span[index];
var remaining = span.Length - index;
for (var separatorIndex = 0; separatorIndex < separators.Length; separatorIndex++)
{
var separator = separators[separatorIndex];
if (string.IsNullOrEmpty(separator) ||
separator[0] != current ||
separator.Length > remaining)
{
continue;
}

if (separator.Length == 1 ||
span.Slice(index + 1, separator.Length - 1).SequenceEqual(separator.AsSpan(1)))
{
separatorLength = separator.Length;
return index;
}
}
}

separatorLength = 0;
return -1;
}

static void TrimSplitEntry(ReadOnlySpan<char> source, ref int startInclusive, ref int endExclusive)
{
while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive]))
Expand Down
47 changes: 35 additions & 12 deletions src/Split/net461/Polyfill_Memory_SplitRanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,10 @@ static bool FindNextSeparator(
}
break;
case SplitRangesMode.AnyStrings:
for (var s = 0; s < stringSeparators.Length; s++)
idx = IndexOfAnyStringSeparator(slice, stringSeparators, out var stringSeparatorLength);
if (idx >= 0)
{
var sep = stringSeparators[s];
if (string.IsNullOrEmpty(sep))
{
continue;
}
var found = slice.IndexOf(sep.AsSpan());
if (found >= 0 && (idx < 0 || found < idx))
{
idx = found;
len = sep.Length;
}
len = stringSeparatorLength;
}
break;
}
Expand All @@ -227,6 +218,38 @@ static bool FindNextSeparator(
separatorLength = len;
return true;
}
/// <summary>
/// Finds the leftmost position in <paramref name="span"/> at which one of <paramref name="separators"/> occurs.
/// Walks the positions once, testing the separators in array order at each one, so ties resolve to the first
/// separator in the array, matching the BCL's MakeSeparatorListAny behaviour. Searching the whole span once per
/// separator instead would rescan it for every separator that is rare or absent, on every call.
/// </summary>
static int IndexOfAnyStringSeparator(ReadOnlySpan<char> span, ReadOnlySpan<string> separators, out int separatorLength)
{
for (var index = 0; index < span.Length; index++)
{
var current = span[index];
var remaining = span.Length - index;
for (var separatorIndex = 0; separatorIndex < separators.Length; separatorIndex++)
{
var separator = separators[separatorIndex];
if (string.IsNullOrEmpty(separator) ||
separator[0] != current ||
separator.Length > remaining)
{
continue;
}
if (separator.Length == 1 ||
span.Slice(index + 1, separator.Length - 1).SequenceEqual(separator.AsSpan(1)))
{
separatorLength = separator.Length;
return index;
}
}
}
separatorLength = 0;
return -1;
}
static void TrimSplitEntry(ReadOnlySpan<char> source, ref int startInclusive, ref int endExclusive)
{
while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive]))
Expand Down
47 changes: 35 additions & 12 deletions src/Split/net462/Polyfill_Memory_SplitRanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,10 @@ static bool FindNextSeparator(
}
break;
case SplitRangesMode.AnyStrings:
for (var s = 0; s < stringSeparators.Length; s++)
idx = IndexOfAnyStringSeparator(slice, stringSeparators, out var stringSeparatorLength);
if (idx >= 0)
{
var sep = stringSeparators[s];
if (string.IsNullOrEmpty(sep))
{
continue;
}
var found = slice.IndexOf(sep.AsSpan());
if (found >= 0 && (idx < 0 || found < idx))
{
idx = found;
len = sep.Length;
}
len = stringSeparatorLength;
}
break;
}
Expand All @@ -227,6 +218,38 @@ static bool FindNextSeparator(
separatorLength = len;
return true;
}
/// <summary>
/// Finds the leftmost position in <paramref name="span"/> at which one of <paramref name="separators"/> occurs.
/// Walks the positions once, testing the separators in array order at each one, so ties resolve to the first
/// separator in the array, matching the BCL's MakeSeparatorListAny behaviour. Searching the whole span once per
/// separator instead would rescan it for every separator that is rare or absent, on every call.
/// </summary>
static int IndexOfAnyStringSeparator(ReadOnlySpan<char> span, ReadOnlySpan<string> separators, out int separatorLength)
{
for (var index = 0; index < span.Length; index++)
{
var current = span[index];
var remaining = span.Length - index;
for (var separatorIndex = 0; separatorIndex < separators.Length; separatorIndex++)
{
var separator = separators[separatorIndex];
if (string.IsNullOrEmpty(separator) ||
separator[0] != current ||
separator.Length > remaining)
{
continue;
}
if (separator.Length == 1 ||
span.Slice(index + 1, separator.Length - 1).SequenceEqual(separator.AsSpan(1)))
{
separatorLength = separator.Length;
return index;
}
}
}
separatorLength = 0;
return -1;
}
static void TrimSplitEntry(ReadOnlySpan<char> source, ref int startInclusive, ref int endExclusive)
{
while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive]))
Expand Down
47 changes: 35 additions & 12 deletions src/Split/net47/Polyfill_Memory_SplitRanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,10 @@ static bool FindNextSeparator(
}
break;
case SplitRangesMode.AnyStrings:
for (var s = 0; s < stringSeparators.Length; s++)
idx = IndexOfAnyStringSeparator(slice, stringSeparators, out var stringSeparatorLength);
if (idx >= 0)
{
var sep = stringSeparators[s];
if (string.IsNullOrEmpty(sep))
{
continue;
}
var found = slice.IndexOf(sep.AsSpan());
if (found >= 0 && (idx < 0 || found < idx))
{
idx = found;
len = sep.Length;
}
len = stringSeparatorLength;
}
break;
}
Expand All @@ -227,6 +218,38 @@ static bool FindNextSeparator(
separatorLength = len;
return true;
}
/// <summary>
/// Finds the leftmost position in <paramref name="span"/> at which one of <paramref name="separators"/> occurs.
/// Walks the positions once, testing the separators in array order at each one, so ties resolve to the first
/// separator in the array, matching the BCL's MakeSeparatorListAny behaviour. Searching the whole span once per
/// separator instead would rescan it for every separator that is rare or absent, on every call.
/// </summary>
static int IndexOfAnyStringSeparator(ReadOnlySpan<char> span, ReadOnlySpan<string> separators, out int separatorLength)
{
for (var index = 0; index < span.Length; index++)
{
var current = span[index];
var remaining = span.Length - index;
for (var separatorIndex = 0; separatorIndex < separators.Length; separatorIndex++)
{
var separator = separators[separatorIndex];
if (string.IsNullOrEmpty(separator) ||
separator[0] != current ||
separator.Length > remaining)
{
continue;
}
if (separator.Length == 1 ||
span.Slice(index + 1, separator.Length - 1).SequenceEqual(separator.AsSpan(1)))
{
separatorLength = separator.Length;
return index;
}
}
}
separatorLength = 0;
return -1;
}
static void TrimSplitEntry(ReadOnlySpan<char> source, ref int startInclusive, ref int endExclusive)
{
while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive]))
Expand Down
47 changes: 35 additions & 12 deletions src/Split/net471/Polyfill_Memory_SplitRanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,10 @@ static bool FindNextSeparator(
}
break;
case SplitRangesMode.AnyStrings:
for (var s = 0; s < stringSeparators.Length; s++)
idx = IndexOfAnyStringSeparator(slice, stringSeparators, out var stringSeparatorLength);
if (idx >= 0)
{
var sep = stringSeparators[s];
if (string.IsNullOrEmpty(sep))
{
continue;
}
var found = slice.IndexOf(sep.AsSpan());
if (found >= 0 && (idx < 0 || found < idx))
{
idx = found;
len = sep.Length;
}
len = stringSeparatorLength;
}
break;
}
Expand All @@ -227,6 +218,38 @@ static bool FindNextSeparator(
separatorLength = len;
return true;
}
/// <summary>
/// Finds the leftmost position in <paramref name="span"/> at which one of <paramref name="separators"/> occurs.
/// Walks the positions once, testing the separators in array order at each one, so ties resolve to the first
/// separator in the array, matching the BCL's MakeSeparatorListAny behaviour. Searching the whole span once per
/// separator instead would rescan it for every separator that is rare or absent, on every call.
/// </summary>
static int IndexOfAnyStringSeparator(ReadOnlySpan<char> span, ReadOnlySpan<string> separators, out int separatorLength)
{
for (var index = 0; index < span.Length; index++)
{
var current = span[index];
var remaining = span.Length - index;
for (var separatorIndex = 0; separatorIndex < separators.Length; separatorIndex++)
{
var separator = separators[separatorIndex];
if (string.IsNullOrEmpty(separator) ||
separator[0] != current ||
separator.Length > remaining)
{
continue;
}
if (separator.Length == 1 ||
span.Slice(index + 1, separator.Length - 1).SequenceEqual(separator.AsSpan(1)))
{
separatorLength = separator.Length;
return index;
}
}
}
separatorLength = 0;
return -1;
}
static void TrimSplitEntry(ReadOnlySpan<char> source, ref int startInclusive, ref int endExclusive)
{
while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive]))
Expand Down
47 changes: 35 additions & 12 deletions src/Split/net472/Polyfill_Memory_SplitRanges.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,19 +200,10 @@ static bool FindNextSeparator(
}
break;
case SplitRangesMode.AnyStrings:
for (var s = 0; s < stringSeparators.Length; s++)
idx = IndexOfAnyStringSeparator(slice, stringSeparators, out var stringSeparatorLength);
if (idx >= 0)
{
var sep = stringSeparators[s];
if (string.IsNullOrEmpty(sep))
{
continue;
}
var found = slice.IndexOf(sep.AsSpan());
if (found >= 0 && (idx < 0 || found < idx))
{
idx = found;
len = sep.Length;
}
len = stringSeparatorLength;
}
break;
}
Expand All @@ -227,6 +218,38 @@ static bool FindNextSeparator(
separatorLength = len;
return true;
}
/// <summary>
/// Finds the leftmost position in <paramref name="span"/> at which one of <paramref name="separators"/> occurs.
/// Walks the positions once, testing the separators in array order at each one, so ties resolve to the first
/// separator in the array, matching the BCL's MakeSeparatorListAny behaviour. Searching the whole span once per
/// separator instead would rescan it for every separator that is rare or absent, on every call.
/// </summary>
static int IndexOfAnyStringSeparator(ReadOnlySpan<char> span, ReadOnlySpan<string> separators, out int separatorLength)
{
for (var index = 0; index < span.Length; index++)
{
var current = span[index];
var remaining = span.Length - index;
for (var separatorIndex = 0; separatorIndex < separators.Length; separatorIndex++)
{
var separator = separators[separatorIndex];
if (string.IsNullOrEmpty(separator) ||
separator[0] != current ||
separator.Length > remaining)
{
continue;
}
if (separator.Length == 1 ||
span.Slice(index + 1, separator.Length - 1).SequenceEqual(separator.AsSpan(1)))
{
separatorLength = separator.Length;
return index;
}
}
}
separatorLength = 0;
return -1;
}
static void TrimSplitEntry(ReadOnlySpan<char> source, ref int startInclusive, ref int endExclusive)
{
while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive]))
Expand Down
Loading
Loading