Counting char occurrences: retarget net10.0, fix IndexOf undercount, Regex.Count - #2218
Open
vladimir-pecanac-main wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 twoLs 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:
net6.0tonet10.0.CommunityToolkit.HighPerformanceremoved.MemoryExtensions.Count()has supplied the sameReadOnlySpan<T>extension fromSystemsince .NET 8, andCountCharsUsingSpanCount's body is unchanged.CountCharsUsingRegex()uses the staticRegex.Count()(.NET 7 and up) instead of constructing aRegexper call.Regex.Escape()is kept.CountSubstringsAndFrequenciesclass, not a benchmark class, holding the substring pair (non-overlapping spanCount()and an overlappingIndexOfloop) and the frequency-map pair (GroupBy().ToDictionary()and aforeachwithGetValueOrDefault()), with four tests.Program.cslifted to top-level statements.Build and tests on SDK 10.0.302 / runtime 10.0.10: 0 warnings, 0 errors, 16 of 16 passing.