Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect code sample in Collection lookups with spans documentation #43701

Open
wipiano opened this issue Nov 22, 2024 · 0 comments · May be fixed by #43702
Open

Incorrect code sample in Collection lookups with spans documentation #43701

wipiano opened this issue Nov 22, 2024 · 0 comments · May be fixed by #43702
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. Pri1 High priority, do before Pri2 and Pri3 ⌚ Not Triaged Not triaged

Comments

@wipiano
Copy link

wipiano commented Nov 22, 2024

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

@issues-automation issues-automation bot added dotnet-fundamentals/svc Pri1 High priority, do before Pri2 and Pri3 labels Nov 22, 2024
@dotnetrepoman dotnetrepoman bot added the ⌚ Not Triaged Not triaged label Nov 22, 2024
wipiano added a commit to wipiano/dotnet-docs that referenced this issue Nov 22, 2024
@dotnet-policy-service dotnet-policy-service bot added the in-pr This issue will be closed (fixed) by an active pull request. label Nov 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotnet-fundamentals/svc in-pr This issue will be closed (fixed) by an active pull request. Pri1 High priority, do before Pri2 and Pri3 ⌚ Not Triaged Not triaged
Projects
None yet
1 participant