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 <cairoeth@protonmail.com>
Co-authored-by: cairo <cairoeth@protonmail.com>
Co-authored-by: cairo <cairoeth@protonmail.com>
| 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.
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.
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 dirtyxafter. - 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 <cairoeth@protonmail.com>
TODO: add functions to specify start and end of string to parse
Co-authored-by: cairo <cairoeth@protonmail.com>
Co-authored-by: cairo <cairoeth@protonmail.com>
ef973fc to
f433e6d
Compare
|
I made |
ernestognw
left a comment
There was a problem hiding this comment.
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 <ernestognw@gmail.com>
Co-authored-by: Ernesto García <ernestognw@gmail.com>
oh nvm we haven't agreed the parseHex naming u.u
Co-authored-by: cairo <cairoeth@protonmail.com> Co-authored-by: Ernesto García <ernestognw@gmail.com>
As discussed. Usefull for parsing addresses from CAIP10 identifier (strings)
PR Checklist
npx changeset add)