Skip to content

Commit fbbff53

Browse files
abcoathupnventuro
authored andcommitted
Strings library (#1746)
* Feature Issue #1745 * Feature Issue #1745 remove whitespace in contract * Feature Issue #1745 fix Solidity linter issues * Feature Issue #1745 fix JS lint issues * Update contracts/drafts/Strings.sol Co-Authored-By: Nicolás Venturo <[email protected]> * Update contracts/drafts/Strings.sol Co-Authored-By: Nicolás Venturo <[email protected]> * Update contracts/drafts/Strings.sol Co-Authored-By: Nicolás Venturo <[email protected]> * Updates based on PR feedback * Remove trailing whitespace * Update tests based on @nventuro feedback * Removed return name * Rename length as suggested * Rename temp variables in uint256ToString * Renamed bytes variable to buffer * Change concatenate to use abi.encodePacked * Moved OraclizeAPI reference to unit256ToString * Add emoji concat test * Remove concatenate * Remove concatenate from StringsMock and test * Rename function to fromUint256 * Update StringsMock.sol
1 parent a83f680 commit fbbff53

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed

contracts/drafts/Strings.sol

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
pragma solidity ^0.5.0;
2+
3+
/**
4+
* @title Strings
5+
* @dev String operations.
6+
*/
7+
library Strings {
8+
/**
9+
* Concatenates two strings.
10+
* string(abi.encodePacked(a, b))
11+
* https://solidity.readthedocs.io/en/latest/types.html?highlight=concatenate
12+
*/
13+
14+
/**
15+
* @dev Converts a uint256 to a string.
16+
* via OraclizeAPI - MIT licence
17+
* https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
18+
*/
19+
function fromUint256(uint256 value) internal pure returns (string memory) {
20+
if (value == 0) {
21+
return "0";
22+
}
23+
uint256 temp = value;
24+
uint256 digits;
25+
while (temp != 0) {
26+
digits++;
27+
temp /= 10;
28+
}
29+
bytes memory buffer = new bytes(digits);
30+
uint256 index = digits - 1;
31+
temp = value;
32+
while (temp != 0) {
33+
buffer[index--] = byte(uint8(48 + temp % 10));
34+
temp /= 10;
35+
}
36+
return string(buffer);
37+
}
38+
}

contracts/mocks/StringsMock.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pragma solidity ^0.5.0;
2+
3+
import "../drafts/Strings.sol";
4+
5+
contract StringsMock {
6+
function fromUint256(uint256 value) public pure returns (string memory) {
7+
return Strings.fromUint256(value);
8+
}
9+
}

test/drafts/Strings.test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const { constants } = require('openzeppelin-test-helpers');
2+
3+
const StringsMock = artifacts.require('StringsMock');
4+
5+
contract('Strings', function () {
6+
beforeEach(async function () {
7+
this.strings = await StringsMock.new();
8+
});
9+
10+
describe('from uint256', function () {
11+
it('converts 0', async function () {
12+
(await this.strings.fromUint256(0)).should.equal('0');
13+
});
14+
15+
it('converts a positive number', async function () {
16+
(await this.strings.fromUint256(4132)).should.equal('4132');
17+
});
18+
19+
it('converts MAX_UINT256', async function () {
20+
(await this.strings.fromUint256(constants.MAX_UINT256)).should.equal(constants.MAX_UINT256.toString());
21+
});
22+
});
23+
});

0 commit comments

Comments
 (0)