Fix Alphabet enumerator producing non-letters for large lists#715
Open
winklemad wants to merge 1 commit into
Open
Fix Alphabet enumerator producing non-letters for large lists#715winklemad wants to merge 1 commit into
winklemad wants to merge 1 commit into
Conversation
list.Alphabet used a naive per-digit div/mod formula that ignored the
"no zero digit" borrow of bijective base-26 (spreadsheet-style A..Z,
AA..ZZ, AAA..). Past index 1352 the middle digit underflowed to '@'
('A'-1) and the leading digit overflowed past 'Z' to '[', and the 4+
letter range was not handled at all, so large lists rendered enumerators
like "B@A." instead of "AZA.".
Replace the hard-coded branches with a correct bijective base-26
conversion (decrement-before-divide borrow, digits built least-
significant first then reversed). Behavior-preserving for the
previously-correct range and correct for all indices thereafter.
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.
Description
list.Alphabetimplements spreadsheet-style bijective base-26 labels (A..Z, AA..ZZ, AAA..), but used a naive per-digitdiv/modformula that ignores the "no zero digit" borrow. Past index 1352 the middle digit underflows to'@'('A'-1) when(i/26)%26 == 0, and the leading digit overflows past'Z'to'['; the 4+ letter range isn't handled at all. So a long list renders non-letter enumerators:First corruption is at index 1352 (the 1353rd item); a broad check finds 156 wrong outputs in 0..4999. This is reachable through the public API (
list.New(...).Enumerator(list.Alphabet)).Fix
Replace the hard-coded 1/2/3-letter branches with a correct bijective base-26 conversion (decrement-before-divide borrow, digits built least-significant-first then reversed). It's behavior-preserving for the previously-correct range (0..1351, which the existing tests cover) and correct for all indices beyond.
Test
Added regression rows to the existing
TestBullettable (1352→AZA,1377→AZZ,1378→BAA,18277→ZZZ,18278→AAAA), which fail on the old code and pass with the fix.go test ./...is green;gofmt/go vetclean.