Skip to content

Commit fdf67dc

Browse files
authored
fix code snippet in 'Collection lookups with spans (What's new in .NET libraries for .NET 9)' documentation (#43702)
1 parent be9b3fe commit fdf67dc

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,18 @@ internal partial class ReadOnlyCollections
5858
// </ReadOnlySet>
5959

6060
// <AlternateLookup>
61-
private static Dictionary<string, int> CountWords(ReadOnlySpan<char> input)
61+
static Dictionary<string, int> CountWords(ReadOnlySpan<char> input)
6262
{
6363
Dictionary<string, int> wordCounts = new(StringComparer.OrdinalIgnoreCase);
6464
Dictionary<string, int>.AlternateLookup<ReadOnlySpan<char>> spanLookup =
6565
wordCounts.GetAlternateLookup<ReadOnlySpan<char>>();
6666

67-
foreach (Range wordRange in Regex.EnumerateSplits(input, @"\b\w+\b"))
67+
foreach (Range wordRange in Regex.EnumerateSplits(input, @"\b\W+"))
6868
{
69+
if (wordRange.Start.Value == wordRange.End.Value)
70+
{
71+
continue; // Skip empty ranges.
72+
}
6973
ReadOnlySpan<char> word = input[wordRange];
7074
spanLookup[word] = spanLookup.TryGetValue(word, out int count) ? count + 1 : 1;
7175
}

0 commit comments

Comments
 (0)