Repository navigation
Add an ignore-case option to NaturalStringComparer [minor] - #76
Open
matt-edmondson wants to merge 2 commits into
Open
matt-edmondson wants to merge 2 commits into
matt-edmondson wants to merge 2 commits into
Conversation
Text chunks were always compared ordinally, so a capitalised file name dropped into a block ahead of every lowercase one. A new NaturalStringComparer(bool ignoreCase) constructor, with static Ordinal and OrdinalIgnoreCase instances, folds text the way OrdinalIgnoreCase does. Every text comparison goes through the same fold, including text against a number's first digit, so the order stays transitive. Strings that differ only in case still tie-break ordinally and never compare equal. The default is unchanged and both modes allocate nothing. Fixes #66 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3WL4nzXCqdxvXj85f
The README documents the ignore-case option. Editing the same CLAUDE.md line as the non-generic IComparer PR only made the two PRs conflict. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017vfJW3WL4nzXCqdxvXj85f
|
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.



Fixes #66
What changed
NaturalStringComparercan now compare text ignoring case. Ordinal stays the default.NaturalStringComparer(bool ignoreCase)constructor, plus staticOrdinalandOrdinalIgnoreCaseinstances.new NaturalStringComparer()behaves exactly as before.char.ToUpperInvariant, which is howStringComparison.OrdinalIgnoreCasefolds. The comparison is still in place and allocates nothing.Fold, so the order stays a total order. That includes text against a number's first digit, in the main loop and in the "text runs on into the other side's number" case. In practice no character folds across the ASCII digit range, so the fold on the number-vs-text path never changes a result today. It is there so the two paths can't disagree.string.CompareOrdinal, as [patch] Break natural-order ties ordinally so distinct strings never compare equal #65 does for leading zeros. They sit next to each other and never compare equal. ASortedSetbuilt withOrdinalIgnoreCasekeeps bothfile1andFile1.Tests
The new
NaturalStringComparerIgnoreCaseTestscovers:build3.log, Build20.log, file1.txt, File2.txt, file10.txt, README.mdOrdinal, andnew NaturalStringComparer()keeping today's orderSortedSetkeeping all threeThe new tests fail without the fix:
With the fix applied, all 35 tests pass. That count includes the existing
Compare_AllocatesNothingand transitivity tests.Notes
IComparer, NaturalStringComparer can't be passed where a non-generic IComparer is required (ArrayList.Sort, WPF CollectionView.CustomSort, WinForms ListView sorting) #74) will conflict on that one line. Whichever merges second needs a one-line rebase topublic partial class NaturalStringComparer(bool ignoreCase) : IComparer<string?>, IComparer.dotnet packpackage validation fails locally on main because of the Polyfill 11.4.5 drift tracked by No release since 1.0.49: pack fails package validation (CP0016/CP0001/CP0014…) after the Polyfill bump, so the #70 base-name ordering fix isn't on NuGet #72 and fixed by Regenerate CompatibilitySuppressions.xml after the Polyfill 11.4.5 bump [patch] #73. This change adds no new CP errors of its own.Generated by Claude Code