Skip to content

Counting char occurrences: retarget net10.0, fix IndexOf undercount, Regex.Count - #2218

Open
vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/75027-counting-char-within-string-csharp
Open

vladimir-pecanac-main wants to merge 1 commit into
CodeMazeBlog:mainfrom
vladimir-pecanac-main:seo/75027-counting-char-within-string-csharp

Conversation

@vladimir-pecanac-main

Copy link
Copy Markdown
Collaborator

Counting Occurrences of a Char Within a String: sample retargeted to .NET 10, with one real bug fix.

CountCharsUsingIndex() skipped the character immediately after each match, so two adjacent occurrences were counted as one: counting 'a' in "aab" returned 1 and in "aaaa" returned 2. Every existing test used "Mary Had A Little Lamb", whose two Ls are seven characters apart, so the suite could never fail on it. The extra increment is removed and a regression test for adjacent occurrences is added.

Also in this PR:

  • Both projects retargeted from net6.0 to net10.0.
  • CommunityToolkit.HighPerformance removed. MemoryExtensions.Count() has supplied the same ReadOnlySpan<T> extension from System since .NET 8, and CountCharsUsingSpanCount's body is unchanged.
  • CountCharsUsingRegex() uses the static Regex.Count() (.NET 7 and up) instead of constructing a Regex per call. Regex.Escape() is kept.
  • New CountSubstringsAndFrequencies class, not a benchmark class, holding the substring pair (non-overlapping span Count() and an overlapping IndexOf loop) and the frequency-map pair (GroupBy().ToDictionary() and a foreach with GetValueOrDefault()), with four tests.
  • Program.cs lifted to top-level statements.
  • Packages: BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3, xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1.

Build and tests on SDK 10.0.302 / runtime 10.0.10: 0 warnings, 0 errors, 16 of 16 passing.

…Regex.Count

CountCharsUsingIndex skipped the character after each match, so adjacent
occurrences were counted as one ("aab" returned 1, "aaaa" returned 2).
Removed the extra increment and added a regression test for adjacent
occurrences, which the existing suite could not fail on.

Also:
- both projects retargeted from net6.0 to net10.0
- CommunityToolkit.HighPerformance removed; MemoryExtensions.Count() supplies
  the same call from System since .NET 8
- CountCharsUsingRegex uses Regex.Count() instead of building a Regex per call
- new CountSubstringsAndFrequencies class with four tests, covering counting a
  substring (overlapping and non-overlapping) and counting every character
- Program.cs lifted to top-level statements
- BenchmarkDotNet 0.15.8, Microsoft.NET.Test.Sdk 18.10.1, xunit 2.9.3,
  xunit.runner.visualstudio 4.0.0, coverlet.collector 10.0.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant