-
Notifications
You must be signed in to change notification settings - Fork 49
/
Utils.sol
128 lines (116 loc) · 3.33 KB
/
Utils.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.12;
// Core utils used extensively to format CSS and numbers.
library utils {
// used to simulate empty strings
string internal constant NULL = '';
// formats a CSS variable line. includes a semicolon for formatting.
function setCssVar(string memory _key, string memory _val)
internal
pure
returns (string memory)
{
return string.concat('--', _key, ':', _val, ';');
}
// formats getting a css variable
function getCssVar(string memory _key)
internal
pure
returns (string memory)
{
return string.concat('var(--', _key, ')');
}
// formats getting a def URL
function getDefURL(string memory _id)
internal
pure
returns (string memory)
{
return string.concat('url(#', _id, ')');
}
// formats rgba white with a specified opacity / alpha
function white_a(uint256 _a) internal pure returns (string memory) {
return rgba(255, 255, 255, _a);
}
// formats rgba black with a specified opacity / alpha
function black_a(uint256 _a) internal pure returns (string memory) {
return rgba(0, 0, 0, _a);
}
// formats generic rgba color in css
function rgba(
uint256 _r,
uint256 _g,
uint256 _b,
uint256 _a
) internal pure returns (string memory) {
string memory formattedA = _a < 100
? string.concat('0.', utils.uint2str(_a))
: '1';
return
string.concat(
'rgba(',
utils.uint2str(_r),
',',
utils.uint2str(_g),
',',
utils.uint2str(_b),
',',
formattedA,
')'
);
}
// checks if two strings are equal
function stringsEqual(string memory _a, string memory _b)
internal
pure
returns (bool)
{
return
keccak256(abi.encodePacked(_a)) == keccak256(abi.encodePacked(_b));
}
// returns the length of a string in characters
function utfStringLength(string memory _str)
internal
pure
returns (uint256 length)
{
uint256 i = 0;
bytes memory string_rep = bytes(_str);
while (i < string_rep.length) {
if (string_rep[i] >> 7 == 0) i += 1;
else if (string_rep[i] >> 5 == bytes1(uint8(0x6))) i += 2;
else if (string_rep[i] >> 4 == bytes1(uint8(0xE))) i += 3;
else if (string_rep[i] >> 3 == bytes1(uint8(0x1E)))
i += 4;
//For safety
else i += 1;
length++;
}
}
// converts an unsigned integer to a string
function uint2str(uint256 _i)
internal
pure
returns (string memory _uintAsString)
{
if (_i == 0) {
return '0';
}
uint256 j = _i;
uint256 len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint256 k = len;
while (_i != 0) {
k = k - 1;
uint8 temp = (48 + uint8(_i - (_i / 10) * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}
}