Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f286b13

Browse files
authored
Add files via upload
1 parent d61f0b4 commit f286b13

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed

lib/StringUtils.sol

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
pragma solidity ^0.4.23;
2+
3+
4+
library StringUtils {
5+
6+
function concat(
7+
string _s1, string _s2,
8+
string _s3, string _s4,
9+
string _s5
10+
) internal pure returns (string) {
11+
bytes memory _b1 = bytes(_s1);
12+
bytes memory _b2 = bytes(_s2);
13+
bytes memory _b3 = bytes(_s3);
14+
bytes memory _b4 = bytes(_s4);
15+
bytes memory _b5 = bytes(_s5);
16+
string memory concatString = new string(_b1.length +
17+
_b2.length + _b3.length + _b4.length + _b5.length);
18+
bytes memory concatBytes = bytes(concatString);
19+
uint k = 0;
20+
uint i;
21+
for (i = 0; i < _b1.length; i++) concatBytes[k++] = _b1[i];
22+
for (i = 0; i < _b2.length; i++) concatBytes[k++] = _b2[i];
23+
for (i = 0; i < _b3.length; i++) concatBytes[k++] = _b3[i];
24+
for (i = 0; i < _b4.length; i++) concatBytes[k++] = _b4[i];
25+
for (i = 0; i < _b5.length; i++) concatBytes[k++] = _b5[i];
26+
return string(concatBytes);
27+
}
28+
29+
function concat(
30+
string _s1, string _s2,
31+
string _s3, string _s4
32+
) internal pure returns (string) {
33+
return concat(_s1, _s2, _s3, _s4, "");
34+
}
35+
36+
function concat(
37+
string _s1, string _s2,
38+
string _s3
39+
) internal pure returns (string) {
40+
return concat(_s1, _s2, _s3, "", "");
41+
}
42+
43+
function concat(
44+
string _s1, string _s2
45+
) internal pure returns (string) {
46+
return concat(_s1, _s2, "", "", "");
47+
}
48+
49+
function addressToString(address x) internal pure returns (string) {
50+
bytes memory s = new bytes(40);
51+
for (uint i = 0; i < 20; i++) {
52+
byte b = byte(uint8(uint(x) / (2**(8*(19 - i)))));
53+
byte hi = byte(uint8(b) / 16);
54+
byte lo = byte(uint8(b) - 16 * uint8(hi));
55+
s[2*i] = char(hi);
56+
s[2*i+1] = char(lo);
57+
}
58+
return string(s);
59+
}
60+
61+
function char(byte b) private pure returns (byte c) {
62+
if (b < 10) return byte(uint8(b) + 0x30);
63+
else return byte(uint8(b) + 0x57);
64+
}
65+
66+
function uintToString(uint v) internal pure returns (string) {
67+
uint maxlength = 78;
68+
bytes memory reversed = new bytes(maxlength);
69+
uint i = 0;
70+
while (v != 0) {
71+
uint remainder = v % 10;
72+
v = v / 10;
73+
reversed[i++] = byte(48 + remainder);
74+
}
75+
bytes memory s = new bytes(i);
76+
for (uint j = 0; j < i; j++) {
77+
s[j] = reversed[i - j - 1];
78+
}
79+
return string(s);
80+
}
81+
}

0 commit comments

Comments
 (0)