Skip to content

fix: parse leading-zero strings as decimal instead of invalid octal#340

Open
Yanhu007 wants to merge 1 commit intospf13:masterfrom
Yanhu007:fix/leading-zero-decimal-parse
Open

fix: parse leading-zero strings as decimal instead of invalid octal#340
Yanhu007 wants to merge 1 commit intospf13:masterfrom
Yanhu007:fix/leading-zero-decimal-parse

Conversation

@Yanhu007
Copy link
Copy Markdown

Fixes #290

Problem

cast.ToInt("08") returns 0 instead of 8.

strconv.ParseInt with base 0 treats leading zeros as octal notation, and 8 is not a valid octal digit, so the parse fails silently.

cast.ToInt("08") // 0 (expected 8)
cast.ToInt("09") // 0 (expected 9)

Fix

parseInt and parseUint now try base-10 first. If that fails (e.g., for "0xff" or "0b1010"), they fall back to base-0 auto-detection.

Input Before After
"08" 0 8
"09" 0 9
"010" 8 (octal) 10 (decimal) ✅
"0xff" 255 255
"0b1010" 10 10

All existing tests pass.

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
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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.

the code「cast.ToInt("08") 」 has unexpected value result

2 participants