-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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 Strings.toString for signed integers #3773
Add Strings.toString for signed integers #3773
Conversation
galadd
commented
Oct 19, 2022
•
edited by ernestognw
Loading
edited by ernestognw
- Tests
- Documentation
- Changelog entry
Hello, @gbolahananon Considering that we already have
|
That makes sense. I just implemented that. You can check the latest update |
contracts/utils/Strings.sol
Outdated
* @dev Converts a `int256` to its ASCII `string` decimal representation. | ||
*/ | ||
function toString(int256 value) internal pure returns (string memory) { | ||
return string(abi.encodePacked(value < 0 ? "-" : "",toString(SignedMath.abs(value)))); |
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.
Any reason to do string(abi.encodePacked(...))
and not string.concat(...)
? I think the second one is clearer and cleaner
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 used abi.encodePacked
because it works with previous version of solidity and if a beginner wants to use the library, they aren’t faced with much difficulty. But on second thought, it's better a beginner gets used to the new implementation.
I have made a new commit
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.
That would require bumping the pragma from pragma solidity ^0.8.0;
to pragma solidity ^0.8.12;
...
It not just a matter of beginner devs, but also of good support with big projects that are fixing a solidity version that is before 0.8.12.
Considering we can do without the pragma bump, you might be right that we should probable use string(abi.encodePacked(...))
.
Alright then, I think it's all good to go 👍🏽 |
This PR has not been merged. Is there any change I need to effect? |
We are still missing a Changelog entry for this PR. |
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.
LGTM
…eration-for-int256-#3758
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.
For the record, let's group tests by type so they don't mix with each other
describe('toString', function () {
...
describe('int256', function () { ... });
describe('uint256', function () { ... });
...
})
Congrats, your important contribution to this open-source project has earned you a GitPOAP! GitPOAP: 2022 OpenZeppelin Contracts Contributor: Head to gitpoap.io & connect your GitHub account to mint! Learn more about GitPOAPs here. |