-
Notifications
You must be signed in to change notification settings - Fork 11.9k
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
Add toUint, toInt and hexToUint to Strings #5166
Conversation
🦋 Changeset detectedLatest commit: c5790f8 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
Co-authored-by: cairo <[email protected]>
Co-authored-by: cairo <[email protected]>
Co-authored-by: cairo <[email protected]>
contracts/utils/Strings.sol
Outdated
function hexToUint(string memory input) internal pure returns (uint256) { | ||
bytes memory buffer = bytes(input); | ||
|
||
// skip 0x prefix if present. Length check doesn't appear to be critical |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
second sentence feels out of place
// skip 0x prefix if present. Length check doesn't appear to be critical | |
// skip 0x prefix if present |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just for the record, this is what the comment was about:
The string length may be less than 2. String could be empty, of just "1"
. In some cases, doing a input[1]
or even a input[0]
would revert (out of bound acces). Doing bytes2(input)
does not revert if the string is to short.
So we check the length to verify that it ok to read the prefix ? It turn out no.
- If the string is empty, then regardless of the result of that lookup (that could read dirty bytes), the loop will not run (because length == 0), and the result will be 0 -> that is ok
- If the string has length 1 then we have two options
- the check identifies the prefix
- that means the string is
"0"
, and there is a dirtyx
after. - In that case we have an offset of 2, and the length is 1, so the for loop does not run and the function returns 0 -> that is ok
- that means the string is
- the check does not find the prefix
- the only "digit" is read by the loop, and the result should be just fine
- the check identifies the prefix
- If the string has length >= 2, then the prefix lookup is in the bounds
That is the long explanation (I'm happy its visible in the PR 😃) to something that is not really trivial, and can be missed, but missing it is not a risk.
We may get questions about it though ...
Co-authored-by: cairo <[email protected]>
TODO: add functions to specify start and end of string to parse
Co-authored-by: cairo <[email protected]>
Co-authored-by: cairo <[email protected]>
ef973fc
to
f433e6d
Compare
I made |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd like to reconsider the naming of parseHex
as raised by @arr00
The function name is ambiguous because it's not clear whether it parses the string as a literal hex (i.e. hex"1234"
) or as a decimal string (i.e. 1234). I'd suggest renaming to parseHexUint
Co-authored-by: Ernesto García <[email protected]>
Co-authored-by: Ernesto García <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lfg
oh nvm we haven't agreed the parseHex
naming u.u
Co-authored-by: cairo <[email protected]> Co-authored-by: Ernesto García <[email protected]>
As discussed. Usefull for parsing addresses from CAIP10 identifier (strings)
PR Checklist
npx changeset add
)