Accept Rust-style _ digit separators in numeric contract arguments - #2692
Open
Galmanus wants to merge 1 commit into
Open
Accept Rust-style _ digit separators in numeric contract arguments#2692Galmanus wants to merge 1 commit into
Galmanus wants to merge 1 commit into
Conversation
Large numeric arguments like 1000000000 are hard to read and easy to get wrong, which matters constantly on Stellar where amounts carry a 1e7 offset. Passing 100_0000000 previously failed with: Expected type i128 (signed 128-bit integer), but received: '100_0000000' Spec::from_string now strips underscores before parsing when the target type is numeric (u32/i32/u64/i64/u128/i128/u256/i256/timepoint/duration) and every underscore sits between two digits. Malformed forms (_1000, 1000_, 1__000) and non-numeric types (symbols, strings, bytes) are left untouched, so existing behavior is unchanged there. Closes stellar#2447
Contributor
There was a problem hiding this comment.
Pull request overview
Adds underscore separators to numeric contract arguments.
Changes:
- Validates and strips separators for numeric types.
- Adds acceptance, rejection, option, and passthrough tests.
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
Comment on lines
+835
to
+844
| let well_formed = bytes.iter().enumerate().all(|(i, &b)| match b { | ||
| b'0'..=b'9' => true, | ||
| b'+' | b'-' => i == 0, | ||
| b'_' => { | ||
| i > 0 | ||
| && bytes[i - 1].is_ascii_digit() | ||
| && bytes.get(i + 1).is_some_and(u8::is_ascii_digit) | ||
| } | ||
| _ => false, | ||
| }); |
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.
Closes #2447
What
stellar contract invoke -- fn --amount 100_0000000now parses, instead of failing withExpected type i128 (signed 128-bit integer), but received: '100_0000000'.How
Spec::from_stringstrips underscores before parsing when both hold:u32/i32/u64/i64/u128/i128/u256/i256/timepoint/durationEverything else is passed through untouched:
_1000,1000_,1__000) still fail with the existing errorhello_worldas aSymbol,"1_000"as aString, byte payloadsOption<numeric>works via the existing recursion into the inner type.Testing
Option<u32>, rejection of malformed forms, and non-numeric passthroughcargo test -p soroban-spec-tools: 104 passedcargo clippy -p soroban-spec-tools --all-targets: clean