File tree 3 files changed +70
-0
lines changed 3 files changed +70
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments