Skip to content

Incorrect code sample in Collection lookups with spans documentation #43701

Open
@wipiano

Description

@wipiano

Type of issue

Code doesn't work

Description

In the current documentation, there's a code sample that doesn't work as intended. The issue is in the following method:

private 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 (Range wordRange in Regex.EnumerateSplits(input, @"\b\w+\b"))
    {
        ReadOnlySpan<char> word = input[wordRange];
        spanLookup[word] = spanLookup.TryGetValue(word, out int count) ? count + 1 : 1;
    }

    return wordCounts;
}

The problem is that Regex.EnumerateSplits is used to iterate over the input, but this method enumerates the separators between words, not the words themselves.

I believe the correct implementation should use Regex.EnumerateMatches instead, like this:

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"))
    {
        Range wordRange = match.Index..(match.Index + match.Length);
        ReadOnlySpan<char> word = input[wordRange];
        spanLookup[word] = spanLookup.TryGetValue(word, out int count) ? count + 1 : 1;
    }

    return wordCounts;
}

This change ensures that the method correctly iterates over the words in the input, rather than the spaces between them.

Page URL

https://learn.microsoft.com/en-us/dotnet/core/whats-new/dotnet-9/libraries#collection-lookups-with-spans

Content source URL

https://github.com/dotnet/docs/blob/main/docs/core/whats-new/dotnet-9/libraries.md

Document Version Independent Id

0c8c7059-86a1-e5df-32e6-fae6fdc5cae0

Article author

@gewarren

Metadata

  • ID: 7c184200-3411-aa5b-ba3f-0b4a3ce0f589
  • Service: dotnet-fundamentals

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions