From 5481d6f7de44f14d43213a5f4e96c0f082fc72e4 Mon Sep 17 00:00:00 2001 From: Kohei Hakoishi <kohe-hakoishi@i-mobile.co.jp> Date: Fri, 22 Nov 2024 17:05:43 +0900 Subject: [PATCH 1/2] fix https://github.com/dotnet/docs/issues/43701 --- docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs b/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs index 75769bf172996..838e11208dd0f 100644 --- a/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs +++ b/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs @@ -64,8 +64,9 @@ private static Dictionary<string, int> CountWords(ReadOnlySpan<char> input) Dictionary<string, int>.AlternateLookup<ReadOnlySpan<char>> spanLookup = wordCounts.GetAlternateLookup<ReadOnlySpan<char>>(); - foreach (Range wordRange in Regex.EnumerateSplits(input, @"\b\w+\b")) + foreach (ValueMatch match in Regex.EnumerateMatches(input, @"\b\w+\b")) { + Range wordRange = match.Index..(match.Index + match.Length); ReadOnlySpan<char> word = input[wordRange]; spanLookup[word] = spanLookup.TryGetValue(word, out int count) ? count + 1 : 1; } From faa4654b2714972e601cfa5c7d93a02322ac0feb Mon Sep 17 00:00:00 2001 From: Kohei Hakoishi <kohe-hakoishi@i-mobile.co.jp> Date: Thu, 3 Apr 2025 10:59:23 +0900 Subject: [PATCH 2/2] incorporate feedback --- .../whats-new/snippets/dotnet-9/csharp/Collections.cs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs b/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs index 838e11208dd0f..5876aad542123 100644 --- a/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs +++ b/docs/core/whats-new/snippets/dotnet-9/csharp/Collections.cs @@ -58,15 +58,18 @@ internal partial class ReadOnlyCollections // </ReadOnlySet> // <AlternateLookup> - private static Dictionary<string, int> CountWords(ReadOnlySpan<char> input) + static Dictionary<string, int> CountWords(ReadOnlySpan<char> input) { Dictionary<string, int> wordCounts = new(StringComparer.OrdinalIgnoreCase); Dictionary<string, int>.AlternateLookup<ReadOnlySpan<char>> spanLookup = wordCounts.GetAlternateLookup<ReadOnlySpan<char>>(); - foreach (ValueMatch match in Regex.EnumerateMatches(input, @"\b\w+\b")) + foreach (Range wordRange in Regex.EnumerateSplits(input, @"\b\W+")) { - Range wordRange = match.Index..(match.Index + match.Length); + if (wordRange.Start.Value == wordRange.End.Value) + { + continue; // Skip empty ranges. + } ReadOnlySpan<char> word = input[wordRange]; spanLookup[word] = spanLookup.TryGetValue(word, out int count) ? count + 1 : 1; }