From 0bc4ba6008dad4494b7c51510da9e55596508271 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Mon, 12 Oct 2026 20:30:42 +1100 Subject: [PATCH] Find string separators in a single pass in SplitAny --- src/Polyfill/Polyfill_Memory_SplitRanges.cs | 53 +++++--- .../net461/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net462/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net47/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net471/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net472/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net48/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net481/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net5.0/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net6.0/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../net7.0/Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../Polyfill_Memory_SplitRanges.cs | 47 +++++-- .../uap10.0/Polyfill_Memory_SplitRanges.cs | 47 +++++-- src/Tests/PolyfillTests_Memory_SplitRanges.cs | 120 ++++++++++++++++++ 20 files changed, 788 insertions(+), 231 deletions(-) diff --git a/src/Polyfill/Polyfill_Memory_SplitRanges.cs b/src/Polyfill/Polyfill_Memory_SplitRanges.cs index 2262f1653..6f794dfb5 100644 --- a/src/Polyfill/Polyfill_Memory_SplitRanges.cs +++ b/src/Polyfill/Polyfill_Memory_SplitRanges.cs @@ -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; @@ -283,6 +271,41 @@ static bool FindNextSeparator( return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net461/Polyfill_Memory_SplitRanges.cs b/src/Split/net461/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net461/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net461/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net462/Polyfill_Memory_SplitRanges.cs b/src/Split/net462/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net462/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net462/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net47/Polyfill_Memory_SplitRanges.cs b/src/Split/net47/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net47/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net47/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net471/Polyfill_Memory_SplitRanges.cs b/src/Split/net471/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net471/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net471/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net472/Polyfill_Memory_SplitRanges.cs b/src/Split/net472/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net472/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net472/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net48/Polyfill_Memory_SplitRanges.cs b/src/Split/net48/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net48/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net48/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net481/Polyfill_Memory_SplitRanges.cs b/src/Split/net481/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net481/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net481/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net5.0/Polyfill_Memory_SplitRanges.cs b/src/Split/net5.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net5.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net5.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net6.0/Polyfill_Memory_SplitRanges.cs b/src/Split/net6.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net6.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net6.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/net7.0/Polyfill_Memory_SplitRanges.cs b/src/Split/net7.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/net7.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/net7.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netcoreapp2.0/Polyfill_Memory_SplitRanges.cs b/src/Split/netcoreapp2.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netcoreapp2.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netcoreapp2.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netcoreapp2.1/Polyfill_Memory_SplitRanges.cs b/src/Split/netcoreapp2.1/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netcoreapp2.1/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netcoreapp2.1/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netcoreapp2.2/Polyfill_Memory_SplitRanges.cs b/src/Split/netcoreapp2.2/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netcoreapp2.2/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netcoreapp2.2/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netcoreapp3.0/Polyfill_Memory_SplitRanges.cs b/src/Split/netcoreapp3.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netcoreapp3.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netcoreapp3.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netcoreapp3.1/Polyfill_Memory_SplitRanges.cs b/src/Split/netcoreapp3.1/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netcoreapp3.1/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netcoreapp3.1/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netstandard2.0/Polyfill_Memory_SplitRanges.cs b/src/Split/netstandard2.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netstandard2.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netstandard2.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/netstandard2.1/Polyfill_Memory_SplitRanges.cs b/src/Split/netstandard2.1/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/netstandard2.1/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/netstandard2.1/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Split/uap10.0/Polyfill_Memory_SplitRanges.cs b/src/Split/uap10.0/Polyfill_Memory_SplitRanges.cs index f676884f7..3e67edd7b 100644 --- a/src/Split/uap10.0/Polyfill_Memory_SplitRanges.cs +++ b/src/Split/uap10.0/Polyfill_Memory_SplitRanges.cs @@ -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; } @@ -227,6 +218,38 @@ static bool FindNextSeparator( separatorLength = len; return true; } + /// + /// Finds the leftmost position in at which one of 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. + /// + static int IndexOfAnyStringSeparator(ReadOnlySpan span, ReadOnlySpan 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 source, ref int startInclusive, ref int endExclusive) { while (startInclusive < endExclusive && char.IsWhiteSpace(source[startInclusive])) diff --git a/src/Tests/PolyfillTests_Memory_SplitRanges.cs b/src/Tests/PolyfillTests_Memory_SplitRanges.cs index 87d94617c..832bc1ad3 100644 --- a/src/Tests/PolyfillTests_Memory_SplitRanges.cs +++ b/src/Tests/PolyfillTests_Memory_SplitRanges.cs @@ -180,6 +180,126 @@ public async Task Split_Char_TruncatedDestinationRemoveEmpty() await Assert.That(SliceOf(",,a,,b,,c", ranges, count)).IsEquivalentTo(new[] {"a", "b,,c"}); } + [Test] + public async Task SplitAny_Strings_MatchStringSplit() + { + string[] inputs = + [ + "a", + "::", + "a::b->c::d", + "a--b+c--d", + "::a::", + "a::::b", + "a:b:c", + "a:", + ":a", + "xaby", + "xabcy", + "aaaa", + "aaaaa", + "abababa", + "no separators here", + "ends with half a separator -", + "a->b-->c--->d", + "::->::->", + ]; + string[][] separatorSets = + [ + ["::"], + [":"], + ["::", "->"], + ["->", "::"], + ["--", "+"], + // one separator is a prefix of another: the first in the array wins at a given position + ["a", "ab"], + ["ab", "a"], + ["ab", "abc"], + ["abc", "ab"], + ["aa", "a"], + ["a", "aa"], + ["aa"], + ["aba", "b"], + ["-", "--", "->"], + ["->", "--", "-"], + // null and empty separators are ignored + [null!, "::", ""], + ["", "->", null!, "::"], + ["missing", "absent", "::"], + ["a very long separator that is longer than most inputs", ":"], + ]; + var failures = new List(); + + foreach (var separators in separatorSets) + { + foreach (var input in inputs) + { + foreach (var options in new[] {StringSplitOptions.None, StringSplitOptions.RemoveEmptyEntries}) + { + var expected = input.Split(separators, options); + var ranges = new Range[input.Length + 2]; + var count = MemoryExtensions.SplitAny(input.AsSpan(), ranges, separators, options); + var actual = SliceOf(input, ranges, count); + if (string.Join("\n", actual) != string.Join("\n", expected) || + count != expected.Length) + { + failures.Add($"'{input}' split on '{string.Join("','", separators)}' ({options}): '{string.Join("','", actual)}'"); + } + } + } + } + + await Assert.That(failures).IsEmpty(); + } + + [Test] + public async Task SplitAny_Strings_TieResolvesToFirstSeparator() + { + var ranges = new Range[8]; + + string[] shortFirst = ["a", "ab"]; + var count = MemoryExtensions.SplitAny("xaby".AsSpan(), ranges, shortFirst); + await Assert.That(count).IsEqualTo(2); + await Assert.That(SliceOf("xaby", ranges, count)).IsEquivalentTo(new[] {"x", "by"}); + + string[] longFirst = ["ab", "a"]; + count = MemoryExtensions.SplitAny("xaby".AsSpan(), ranges, longFirst); + await Assert.That(count).IsEqualTo(2); + await Assert.That(SliceOf("xaby", ranges, count)).IsEquivalentTo(new[] {"x", "y"}); + } + + [Test] + public async Task SplitAny_Strings_LastSlotGetsRemainder() + { + var ranges = new Range[2]; + string[] separators = ["::", "->"]; + var count = MemoryExtensions.SplitAny("a::b->c::d".AsSpan(), ranges, separators); + await Assert.That(count).IsEqualTo(2); + await Assert.That(SliceOf("a::b->c::d", ranges, count)).IsEquivalentTo(new[] {"a", "b->c::d"}); + } + + [Test] + public async Task SplitAny_Strings_ManyWords() + { + var builder = new StringBuilder(); + for (var index = 0; index < 4000; index++) + { + builder.Append("word").Append(index).Append("::"); + } + + var input = builder.ToString(); + var ranges = new Range[5000]; + string[] separators = ["\t\t", "||", "::"]; + + var count = MemoryExtensions.SplitAny(input.AsSpan(), ranges, separators); + var first = input[ranges[0]]; + var last = input[ranges[3999]]; + + await Assert.That(count).IsEqualTo(4001); + await Assert.That(first).IsEqualTo("word0"); + await Assert.That(last).IsEqualTo("word3999"); + } + static string[] SliceOf(string source, Range[] ranges, int count) { var result = new string[count];