Skip to content

fix(strconv): allow "0b" to be parsed as digits when base is 16 #2009

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

Yu-zh
Copy link
Collaborator

@Yu-zh Yu-zh commented Apr 27, 2025

fix #2006

Copy link

The base 16 condition should be checked before the base (0 | 2) condition

Category
Correctness
Code Snippet
if base is (0 | 2) { ... } else if base is 16 { ... }
Recommendation
Swap the order of conditions: if base is 16 { ... } else if base is (0 | 2) { ... }
Reasoning
The current order might cause confusion as '0b' could be interpreted as binary when base=16 is intended. Checking base 16 first makes the intent clearer and follows the principle of most specific to least specific conditions.

Missing documentation for the new hex parsing behavior

Category
Maintainability
Code Snippet
// 'b' is a legitimate hex digit
Recommendation
Add a more detailed comment explaining the special case handling: '// When parsing hex numbers (base 16), treat b as hex digit (11) rather than binary prefix'
Reasoning
The current comment is too brief and doesn't fully explain the rationale behind this special case handling. Better documentation would help future maintainers understand the intended behavior.

Test cases could be better organized

Category
Maintainability
Code Snippet
test "more edge cases" { ... }
Recommendation
Split the test cases into logical groups with descriptive names: test "hex parsing with various prefixes" { ... }, test "invalid base combinations" { ... }
Reasoning
The current test organization lumps different scenarios together under 'more edge cases'. Splitting them by functionality would make it easier to understand what's being tested and maintain the tests.

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 6458

Details

  • 5 of 5 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.02%) to 92.687%

Totals Coverage Status
Change from base Build 6455: 0.02%
Covered Lines: 6122
Relevant Lines: 6605

💛 - Coveralls

Comment on lines +47 to +52
} else if base is 16 {
// 'b' is a legitimate hex digit
(16, view, false)
} else {
base_err!()
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't it be allowed for base larger than 11?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0 | 2 | 12 ..= 36 for b, 0 | 8 | 25..=36 for o, 0 | 16 | 34..=36 for x

@peter-jerry-ye
Copy link
Collaborator

Replaced by #2015

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.

@strconv.parse_int!("0b", base=16) fails to parse
3 participants