fix: parse leading-zero strings as decimal instead of invalid octal#340
Open
Yanhu007 wants to merge 1 commit intospf13:masterfrom
Open
fix: parse leading-zero strings as decimal instead of invalid octal#340Yanhu007 wants to merge 1 commit intospf13:masterfrom
Yanhu007 wants to merge 1 commit intospf13:masterfrom
Conversation
cast.ToInt("08") returned 0 because strconv.ParseInt with base 0
treats leading zeros as octal, and 8 is not a valid octal digit.
Now parseInt/parseUint try base 10 first, falling back to base 0
(auto-detect) for hex/octal/binary prefixed strings. This ensures:
- "08", "09" → 8, 9 (previously 0)
- "010" → 10 (previously 8, now decimal as users expect)
- "0xff" → 255 (still works via fallback)
- "0b1010" → 10 (still works via fallback)
Fixes spf13#290
|
|
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 #290
Problem
cast.ToInt("08")returns0instead of8.strconv.ParseIntwith base0treats leading zeros as octal notation, and8is not a valid octal digit, so the parse fails silently.Fix
parseIntandparseUintnow try base-10 first. If that fails (e.g., for"0xff"or"0b1010"), they fall back to base-0 auto-detection."08"08✅"09"09✅"010"8(octal)10(decimal) ✅"0xff"255255✅"0b1010"1010✅All existing tests pass.