Skip to content

fix(strconv): adjust the logic for validating base #2015

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

Merged
merged 2 commits into from
Apr 28, 2025

Conversation

peter-jerry-ye
Copy link
Collaborator

Fixes #2006

This PR simplifies the logic:

  • if base is not defined, detect the base with prefix
  • otherwise, remove prefix for 2/8/16-based if there were one

Thus this breaks some exisiting behaviour. For example:

0x01 with base=3 was considered bad base because 0x does not match 3. Now it is considered bad syntax because x exceeds the character set allowed for base 3.

@coveralls
Copy link
Collaborator

coveralls commented Apr 27, 2025

Pull Request Test Coverage Report for Build 6473

Details

  • 11 of 11 (100.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.003%) to 92.697%

Totals Coverage Status
Change from base Build 6470: -0.003%
Covered Lines: 6118
Relevant Lines: 6600

💛 - Coveralls

Copy link

peter-jerry-ye-code-review bot commented Apr 27, 2025

Potential edge case with zero handling not covered in tests

Category
Correctness
Code Snippet
match view { ['0', 'x' | 'X', .. rest] if base == 16 => (16, rest, true) ... }
Recommendation
Add test cases for inputs like '0' and '00' to verify correct handling of lone zeros
Reasoning
The code handles prefixes but should explicitly test standalone zero cases to ensure they work correctly with different bases

The base range check could be extracted to a helper function

Category
Maintainability
Code Snippet
if base is (2..=36) { (base, view, false) } else { base_err!() }
Recommendation
Create a helper function like fn is_valid_base(base: Int) -> Bool { base is (2..=36) }
Reasoning
The base validation logic appears in multiple places in the codebase and could be centralized for better maintainability and consistency

Test cases could be better organized with clear sections

Category
Maintainability
Code Snippet
test "more edge cases" { ... }
Recommendation
Split test cases into logical groups with descriptive names like 'test_base16_with_prefixes', 'test_invalid_prefixes', etc.
Reasoning
The current test organization makes it harder to understand what aspects are being tested and to maintain the tests when behavior changes

@peter-jerry-ye peter-jerry-ye enabled auto-merge (rebase) April 28, 2025 05:30
@peter-jerry-ye peter-jerry-ye merged commit 203db44 into main Apr 28, 2025
12 checks passed
@peter-jerry-ye peter-jerry-ye deleted the zihang/fix-parse-int branch April 28, 2025 05:35
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