|
| 1 | +# Unicode |
| 2 | + |
| 3 | +*Devin Stein* |
| 4 | + |
| 5 | +> A library for validating, parsing, and manipulating UTF-8 encoded Unicode strings |
| 6 | +
|
| 7 | +For character introspection or more complex transformations, checkout the UnicodeData contract. |
| 8 | + |
| 9 | +*All external and public functions use self as their first parameter to allow "using Unicode for strings;". If you have ideas for new functions or improvements, please contribute!* |
| 10 | + |
| 11 | +## Methods |
| 12 | + |
| 13 | +### CHAR_NOT_FOUND |
| 14 | + |
| 15 | +```solidity |
| 16 | +function CHAR_NOT_FOUND() external view returns (uint256) |
| 17 | +``` |
| 18 | + |
| 19 | +The return value of indexOf and bytesIndicesOf if the character is not found |
| 20 | + |
| 21 | +*Use CHAR_NOT_FOUND to check if indexOf or bytesIndicesOf does not find the inputted character* |
| 22 | + |
| 23 | + |
| 24 | +#### Returns |
| 25 | + |
| 26 | +| Name | Type | Description | |
| 27 | +|---|---|---| |
| 28 | +| _0 | uint256 | undefined |
| 29 | + |
| 30 | +### bytesIndicesOf |
| 31 | + |
| 32 | +```solidity |
| 33 | +function bytesIndicesOf(string self, string _of) external pure returns (uint256, uint256) |
| 34 | +``` |
| 35 | + |
| 36 | +Get the starting (inclusive) and ending (exclusive) bytes indices of character `_of` in string `self` |
| 37 | + |
| 38 | +*bytesIndicesOf returns (CHAR_NOT_FOUND, CHAR_NOT_FOUND) if `_of` isn't found in `self`* |
| 39 | + |
| 40 | +#### Parameters |
| 41 | + |
| 42 | +| Name | Type | Description | |
| 43 | +|---|---|---| |
| 44 | +| self | string | The input string |
| 45 | +| _of | string | The character to find the bytes indices of |
| 46 | + |
| 47 | +#### Returns |
| 48 | + |
| 49 | +| Name | Type | Description | |
| 50 | +|---|---|---| |
| 51 | +| _0 | uint256 | The starting (inclusive) and ending (exclusive) indites the character in the bytes underlying the string |
| 52 | +| _1 | uint256 | undefined |
| 53 | + |
| 54 | +### charAt |
| 55 | + |
| 56 | +```solidity |
| 57 | +function charAt(string self, uint256 _idx) external pure returns (string) |
| 58 | +``` |
| 59 | + |
| 60 | +Get the UTF-8 character at `_idx` for `self` |
| 61 | + |
| 62 | +*charAt will error if the idx is out of bounds* |
| 63 | + |
| 64 | +#### Parameters |
| 65 | + |
| 66 | +| Name | Type | Description | |
| 67 | +|---|---|---| |
| 68 | +| self | string | The input string |
| 69 | +| _idx | uint256 | The index of the character to get |
| 70 | + |
| 71 | +#### Returns |
| 72 | + |
| 73 | +| Name | Type | Description | |
| 74 | +|---|---|---| |
| 75 | +| _0 | string | The character at the given index |
| 76 | + |
| 77 | +### codePointAt |
| 78 | + |
| 79 | +```solidity |
| 80 | +function codePointAt(string self, uint256 _idx) external pure returns (uint32) |
| 81 | +``` |
| 82 | + |
| 83 | +Get the Unicode code point at `_idx` for `self` |
| 84 | + |
| 85 | +*codePointAt requires a valid UTF-8 string* |
| 86 | + |
| 87 | +#### Parameters |
| 88 | + |
| 89 | +| Name | Type | Description | |
| 90 | +|---|---|---| |
| 91 | +| self | string | The input string |
| 92 | +| _idx | uint256 | The index of the code point to get |
| 93 | + |
| 94 | +#### Returns |
| 95 | + |
| 96 | +| Name | Type | Description | |
| 97 | +|---|---|---| |
| 98 | +| _0 | uint32 | The Unicode code point at the given index |
| 99 | + |
| 100 | +### decode |
| 101 | + |
| 102 | +```solidity |
| 103 | +function decode(string self) external pure returns (string[]) |
| 104 | +``` |
| 105 | + |
| 106 | +Decode every UTF-8 characters in `self` |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | +#### Parameters |
| 111 | + |
| 112 | +| Name | Type | Description | |
| 113 | +|---|---|---| |
| 114 | +| self | string | The input string |
| 115 | + |
| 116 | +#### Returns |
| 117 | + |
| 118 | +| Name | Type | Description | |
| 119 | +|---|---|---| |
| 120 | +| _0 | string[] | An ordered array of all UTF-8 characters in `self` |
| 121 | + |
| 122 | +### decodeChar |
| 123 | + |
| 124 | +```solidity |
| 125 | +function decodeChar(string self, uint256 _cursor) external pure returns (string, uint256) |
| 126 | +``` |
| 127 | + |
| 128 | +Decode the next UTF-8 character in `self` given a starting position of `_cursor` |
| 129 | + |
| 130 | +*decodeChar is useful for functions want to iterate over the string in one pass and check each category for a condition* |
| 131 | + |
| 132 | +#### Parameters |
| 133 | + |
| 134 | +| Name | Type | Description | |
| 135 | +|---|---|---| |
| 136 | +| self | string | The input string |
| 137 | +| _cursor | uint256 | The starting bytes position (inclusive) of the character |
| 138 | + |
| 139 | +#### Returns |
| 140 | + |
| 141 | +| Name | Type | Description | |
| 142 | +|---|---|---| |
| 143 | +| _0 | string | The next character as a string and the starting position of the next character. |
| 144 | +| _1 | uint256 | undefined |
| 145 | + |
| 146 | +### indexOf |
| 147 | + |
| 148 | +```solidity |
| 149 | +function indexOf(string self, string _of) external pure returns (uint256) |
| 150 | +``` |
| 151 | + |
| 152 | +Get the character index of `_of` in string `self` |
| 153 | + |
| 154 | +*indexOf returns CHAR_NOT_FOUND if `_of` isn't found in `self`* |
| 155 | + |
| 156 | +#### Parameters |
| 157 | + |
| 158 | +| Name | Type | Description | |
| 159 | +|---|---|---| |
| 160 | +| self | string | The input string |
| 161 | +| _of | string | The character to find the index of |
| 162 | + |
| 163 | +#### Returns |
| 164 | + |
| 165 | +| Name | Type | Description | |
| 166 | +|---|---|---| |
| 167 | +| _0 | uint256 | The index of the character in the given string |
| 168 | + |
| 169 | +### isASCII |
| 170 | + |
| 171 | +```solidity |
| 172 | +function isASCII(string self) external pure returns (bool) |
| 173 | +``` |
| 174 | + |
| 175 | +Check if `self` contains only single byte ASCII characters (0-127) |
| 176 | + |
| 177 | +*If a string is only ASCII, then it's safe to treat each byte as a character. This returns false for extended ASCII (128-255) because they are use two bytes in UTF-8.* |
| 178 | + |
| 179 | +#### Parameters |
| 180 | + |
| 181 | +| Name | Type | Description | |
| 182 | +|---|---|---| |
| 183 | +| self | string | The input string |
| 184 | + |
| 185 | +#### Returns |
| 186 | + |
| 187 | +| Name | Type | Description | |
| 188 | +|---|---|---| |
| 189 | +| _0 | bool | True if the `self` only contains ASCII |
| 190 | + |
| 191 | +### isUTF8 |
| 192 | + |
| 193 | +```solidity |
| 194 | +function isUTF8(string self) external pure returns (bool) |
| 195 | +``` |
| 196 | + |
| 197 | +Check if `self` is valid UTF-8 |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +#### Parameters |
| 202 | + |
| 203 | +| Name | Type | Description | |
| 204 | +|---|---|---| |
| 205 | +| self | string | The input string |
| 206 | + |
| 207 | +#### Returns |
| 208 | + |
| 209 | +| Name | Type | Description | |
| 210 | +|---|---|---| |
| 211 | +| _0 | bool | True if the string is UTF-8 encoded |
| 212 | + |
| 213 | +### length |
| 214 | + |
| 215 | +```solidity |
| 216 | +function length(string self) external pure returns (uint256) |
| 217 | +``` |
| 218 | + |
| 219 | +Get length of `self` |
| 220 | + |
| 221 | +*For efficiency, length assumes valid UTF-8 encoded input. It only does simple checks for bytes sequences* |
| 222 | + |
| 223 | +#### Parameters |
| 224 | + |
| 225 | +| Name | Type | Description | |
| 226 | +|---|---|---| |
| 227 | +| self | string | The input string |
| 228 | + |
| 229 | +#### Returns |
| 230 | + |
| 231 | +| Name | Type | Description | |
| 232 | +|---|---|---| |
| 233 | +| _0 | uint256 | The number of UTF-8 characters in `self` |
| 234 | + |
| 235 | +### toCodePoint |
| 236 | + |
| 237 | +```solidity |
| 238 | +function toCodePoint(string self) external pure returns (uint32) |
| 239 | +``` |
| 240 | + |
| 241 | +Get the code point of character: `self` |
| 242 | + |
| 243 | +*This function requires a valid UTF-8 character* |
| 244 | + |
| 245 | +#### Parameters |
| 246 | + |
| 247 | +| Name | Type | Description | |
| 248 | +|---|---|---| |
| 249 | +| self | string | The input character |
| 250 | + |
| 251 | +#### Returns |
| 252 | + |
| 253 | +| Name | Type | Description | |
| 254 | +|---|---|---| |
| 255 | +| _0 | uint32 | The code point of `self` |
| 256 | + |
| 257 | + |
| 258 | + |
| 259 | + |
0 commit comments