Skip to content

Rename toString to toStringSigned #4330

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

16 changes: 16 additions & 0 deletions contracts/mocks/StringsMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.19;

import "../utils/Strings.sol";

contract StringsMock {
string public integerLiteralConversion;

constructor() {
integerLiteralConversion = string.concat(
Strings.toStringSigned(100),
" Integer literal conversion successful!"
);
}
}
7 changes: 7 additions & 0 deletions contracts/utils/Strings.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ library Strings {
return string(abi.encodePacked(value < 0 ? "-" : "", toString(SignedMath.abs(value))));
}

/**
* @dev Converts a `uint256` integer literal to its ASCII `string` decimal representation.
*/
function toStringSigned(uint256 value) internal pure returns (string memory) {
return toString(value);
}

/**
* @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
*/
Expand Down
9 changes: 9 additions & 0 deletions test/utils/Strings.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,19 @@ const { BN, constants, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');

const Strings = artifacts.require('$Strings');
const StringsMock = artifacts.require('$StringsMock');

contract('Strings', function () {
before(async function () {
this.strings = await Strings.new();
this.stringsMock = await StringsMock.new();
});

describe('toStringSigned', function () {
const expected = '100 Integer literal conversion successful!';
it('should fetch `integerLiteralConversion` as `100 Integer literal conversion successful!`', async function () {
expect(await this.stringsMock.methods['integerLiteralConversion()']()).to.equal(expected);
});
});

describe('toString', function () {
Expand Down