You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: contracts/Unicode.sol
+52-52Lines changed: 52 additions & 52 deletions
Original file line number
Diff line number
Diff line change
@@ -4,14 +4,14 @@ pragma solidity ^0.8.9;
4
4
/// @title A library for validating, parsing, and manipulating UTF-8 encoded Unicode strings
5
5
/// @author Devin Stein
6
6
/// @notice For character introspection or more complex transformations, checkout the UnicodeData contract.
7
-
/// @dev All external and public functions use _str as their first parameter to allow "using Unicode for strings;". If you have ideas for new functions or improvements, please contribute!
7
+
/// @dev 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!
8
8
libraryUnicode {
9
-
/// @notice Check if `_str` contains only ASCII characters
9
+
/// @notice Check if `self` contains only ASCII characters
10
10
/// @dev If a string is only ASCII, then it's safe to treat each byte as a character
11
-
/// @param _str The input string
12
-
/// @return True if the `_str` only contains ASCII
13
-
function isASCII(stringcalldata_str) externalpurereturns (bool) {
14
-
bytescalldata _b =bytes(_str);
11
+
/// @param self The input string
12
+
/// @return True if the `self` only contains ASCII
13
+
function isASCII(stringcalldataself) externalpurereturns (bool) {
14
+
bytescalldata _b =bytes(self);
15
15
uint256 len = _b.length;
16
16
17
17
for (uint256 i =0; i < len; i++) {
@@ -67,12 +67,12 @@ library Unicode {
67
67
uint8(_b[3] &0x3f);
68
68
}
69
69
70
-
/// @notice Get length of `_str`
70
+
/// @notice Get length of `self`
71
71
/// @dev For efficiency, length assumes valid UTF-8 encoded input. It only does simple checks for bytes sequences
72
-
/// @param _str The input string
73
-
/// @return The number of UTF-8 characters in `_str`
74
-
function length(stringcalldata_str) publicpurereturns (uint256) {
75
-
bytesmemory _b =bytes(_str);
72
+
/// @param self The input string
73
+
/// @return The number of UTF-8 characters in `self`
74
+
function length(stringcalldataself) publicpurereturns (uint256) {
75
+
bytesmemory _b =bytes(self);
76
76
uint256 end = _b.length;
77
77
uint256 len;
78
78
uint256 i;
@@ -100,12 +100,12 @@ library Unicode {
100
100
return len;
101
101
}
102
102
103
-
/// @notice Get the code point of character: `_str`
103
+
/// @notice Get the code point of character: `self`
104
104
/// @dev This function requires a valid UTF-8 character
105
-
/// @param _str The input character
106
-
/// @return The code point of `_str`
107
-
function toCodePoint(stringmemory_str) publicpurereturns (uint32) {
108
-
bytesmemory _b =bytes(_str);
105
+
/// @param self The input character
106
+
/// @return The code point of `self`
107
+
function toCodePoint(stringmemoryself) publicpurereturns (uint32) {
108
+
bytesmemory _b =bytes(self);
109
109
uint256 len = _b.length;
110
110
111
111
require(
@@ -144,11 +144,11 @@ library Unicode {
144
144
return0;
145
145
}
146
146
147
-
/// @notice Check if `_str` is valid UTF-8
148
-
/// @param _str The input string
147
+
/// @notice Check if `self` is valid UTF-8
148
+
/// @param self The input string
149
149
/// @return True if the string is UTF-8 encoded
150
-
function isUTF8(stringcalldata_str) externalpurereturns (bool) {
151
-
bytesmemory _b =bytes(_str);
150
+
function isUTF8(stringcalldataself) externalpurereturns (bool) {
151
+
bytesmemory _b =bytes(self);
152
152
uint256 end = _b.length;
153
153
uint32 cp;
154
154
uint256 i;
@@ -214,17 +214,17 @@ library Unicode {
214
214
returntrue;
215
215
}
216
216
217
-
/// @notice Decode the next UTF-8 character in `_str` given a starting position of `_cursor`
217
+
/// @notice Decode the next UTF-8 character in `self` given a starting position of `_cursor`
218
218
/// @dev decodeChar is useful for functions want to iterate over the string in one pass and check each category for a condition
219
-
/// @param _str The input string
219
+
/// @param self The input string
220
220
/// @param _cursor The starting bytes position (inclusive) of the character
221
221
/// @return The next character as a string and the starting position of the next character.
222
-
function decodeChar(stringcalldata_str, uint256_cursor)
222
+
function decodeChar(stringcalldataself, uint256_cursor)
223
223
public
224
224
pure
225
225
returns (stringmemory, uint256)
226
226
{
227
-
bytesmemory _b =bytes(_str);
227
+
bytesmemory _b =bytes(self);
228
228
uint256 len = _b.length;
229
229
bytesmemory output;
230
230
uint32 cp;
@@ -313,116 +313,116 @@ library Unicode {
313
313
return ("", 0);
314
314
}
315
315
316
-
/// @notice Decode every UTF-8 characters in `_str`
317
-
/// @param _str The input string
318
-
/// @return An ordered array of all UTF-8 characters in `_str`
319
-
function decode(stringcalldata_str)
316
+
/// @notice Decode every UTF-8 characters in `self`
317
+
/// @param self The input string
318
+
/// @return An ordered array of all UTF-8 characters in `self`
319
+
function decode(stringcalldataself)
320
320
external
321
321
pure
322
322
returns (string[] memory)
323
323
{
324
324
// The charaters array must be initialized to a fixed size.
325
325
// Loop over the string to get the number of charcters before decoding.
326
-
uint256 size =length(_str);
326
+
uint256 size =length(self);
327
327
string[] memory characters =newstring[](size);
328
328
329
329
stringmemory char;
330
330
uint256 cursor =0;
331
-
uint256 len =bytes(_str).length;
331
+
uint256 len =bytes(self).length;
332
332
uint256 idx;
333
333
334
334
while (cursor < len) {
335
-
(char, cursor) =decodeChar(_str, cursor);
335
+
(char, cursor) =decodeChar(self, cursor);
336
336
characters[idx] = char;
337
337
idx++;
338
338
}
339
339
340
340
return characters;
341
341
}
342
342
343
-
/// @notice Get the UTF-8 character at `_idx` for `_str`
343
+
/// @notice Get the UTF-8 character at `_idx` for `self`
344
344
/// @dev charAt will error if the idx is out of bounds
345
-
/// @param _str The input string
345
+
/// @param self The input string
346
346
/// @param _idx The index of the character to get
347
347
/// @return The character at the given index
348
-
function charAt(stringcalldata_str, uint256_idx)
348
+
function charAt(stringcalldataself, uint256_idx)
349
349
public
350
350
pure
351
351
returns (stringmemory)
352
352
{
353
353
stringmemory char;
354
-
uint256 len =bytes(_str).length;
354
+
uint256 len =bytes(self).length;
355
355
uint256 cursor;
356
356
357
357
for (uint256 i =0; i <= _idx; i++) {
358
-
(char, cursor) =decodeChar(_str, cursor);
358
+
(char, cursor) =decodeChar(self, cursor);
359
359
// if we hit the end, it must be the _idx
360
360
require(cursor < len || i == _idx, "index out of bounds");
361
361
}
362
362
363
363
return char;
364
364
}
365
365
366
-
/// @notice Get the Unicode code point at `_idx` for `_str`
366
+
/// @notice Get the Unicode code point at `_idx` for `self`
367
367
/// @dev codePointAt requires a valid UTF-8 string
368
-
/// @param _str The input string
368
+
/// @param self The input string
369
369
/// @param _idx The index of the code point to get
370
370
/// @return The Unicode code point at the given index
371
-
function codePointAt(stringcalldata_str, uint256_idx)
371
+
function codePointAt(stringcalldataself, uint256_idx)
372
372
external
373
373
pure
374
374
returns (uint32)
375
375
{
376
-
returntoCodePoint(charAt(_str, _idx));
376
+
returntoCodePoint(charAt(self, _idx));
377
377
}
378
378
379
379
/// @notice The return value of indexOf and bytesIndicesOf if the character is not found
380
380
/// @dev Use CHAR_NOT_FOUND to check if indexOf or bytesIndicesOf does not find the inputted character
0 commit comments