Skip to content

Commit 9884239

Browse files
committed
Use self convention for Unicode library
1 parent ab5a8cd commit 9884239

1 file changed

Lines changed: 52 additions & 52 deletions

File tree

contracts/Unicode.sol

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ pragma solidity ^0.8.9;
44
/// @title A library for validating, parsing, and manipulating UTF-8 encoded Unicode strings
55
/// @author Devin Stein
66
/// @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!
88
library Unicode {
9-
/// @notice Check if `_str` contains only ASCII characters
9+
/// @notice Check if `self` contains only ASCII characters
1010
/// @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(string calldata _str) external pure returns (bool) {
14-
bytes calldata _b = bytes(_str);
11+
/// @param self The input string
12+
/// @return True if the `self` only contains ASCII
13+
function isASCII(string calldata self) external pure returns (bool) {
14+
bytes calldata _b = bytes(self);
1515
uint256 len = _b.length;
1616

1717
for (uint256 i = 0; i < len; i++) {
@@ -67,12 +67,12 @@ library Unicode {
6767
uint8(_b[3] & 0x3f);
6868
}
6969

70-
/// @notice Get length of `_str`
70+
/// @notice Get length of `self`
7171
/// @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(string calldata _str) public pure returns (uint256) {
75-
bytes memory _b = bytes(_str);
72+
/// @param self The input string
73+
/// @return The number of UTF-8 characters in `self`
74+
function length(string calldata self) public pure returns (uint256) {
75+
bytes memory _b = bytes(self);
7676
uint256 end = _b.length;
7777
uint256 len;
7878
uint256 i;
@@ -100,12 +100,12 @@ library Unicode {
100100
return len;
101101
}
102102

103-
/// @notice Get the code point of character: `_str`
103+
/// @notice Get the code point of character: `self`
104104
/// @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(string memory _str) public pure returns (uint32) {
108-
bytes memory _b = bytes(_str);
105+
/// @param self The input character
106+
/// @return The code point of `self`
107+
function toCodePoint(string memory self) public pure returns (uint32) {
108+
bytes memory _b = bytes(self);
109109
uint256 len = _b.length;
110110

111111
require(
@@ -144,11 +144,11 @@ library Unicode {
144144
return 0;
145145
}
146146

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
149149
/// @return True if the string is UTF-8 encoded
150-
function isUTF8(string calldata _str) external pure returns (bool) {
151-
bytes memory _b = bytes(_str);
150+
function isUTF8(string calldata self) external pure returns (bool) {
151+
bytes memory _b = bytes(self);
152152
uint256 end = _b.length;
153153
uint32 cp;
154154
uint256 i;
@@ -214,17 +214,17 @@ library Unicode {
214214
return true;
215215
}
216216

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`
218218
/// @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
220220
/// @param _cursor The starting bytes position (inclusive) of the character
221221
/// @return The next character as a string and the starting position of the next character.
222-
function decodeChar(string calldata _str, uint256 _cursor)
222+
function decodeChar(string calldata self, uint256 _cursor)
223223
public
224224
pure
225225
returns (string memory, uint256)
226226
{
227-
bytes memory _b = bytes(_str);
227+
bytes memory _b = bytes(self);
228228
uint256 len = _b.length;
229229
bytes memory output;
230230
uint32 cp;
@@ -313,116 +313,116 @@ library Unicode {
313313
return ("", 0);
314314
}
315315

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(string calldata _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(string calldata self)
320320
external
321321
pure
322322
returns (string[] memory)
323323
{
324324
// The charaters array must be initialized to a fixed size.
325325
// Loop over the string to get the number of charcters before decoding.
326-
uint256 size = length(_str);
326+
uint256 size = length(self);
327327
string[] memory characters = new string[](size);
328328

329329
string memory char;
330330
uint256 cursor = 0;
331-
uint256 len = bytes(_str).length;
331+
uint256 len = bytes(self).length;
332332
uint256 idx;
333333

334334
while (cursor < len) {
335-
(char, cursor) = decodeChar(_str, cursor);
335+
(char, cursor) = decodeChar(self, cursor);
336336
characters[idx] = char;
337337
idx++;
338338
}
339339

340340
return characters;
341341
}
342342

343-
/// @notice Get the UTF-8 character at `_idx` for `_str`
343+
/// @notice Get the UTF-8 character at `_idx` for `self`
344344
/// @dev charAt will error if the idx is out of bounds
345-
/// @param _str The input string
345+
/// @param self The input string
346346
/// @param _idx The index of the character to get
347347
/// @return The character at the given index
348-
function charAt(string calldata _str, uint256 _idx)
348+
function charAt(string calldata self, uint256 _idx)
349349
public
350350
pure
351351
returns (string memory)
352352
{
353353
string memory char;
354-
uint256 len = bytes(_str).length;
354+
uint256 len = bytes(self).length;
355355
uint256 cursor;
356356

357357
for (uint256 i = 0; i <= _idx; i++) {
358-
(char, cursor) = decodeChar(_str, cursor);
358+
(char, cursor) = decodeChar(self, cursor);
359359
// if we hit the end, it must be the _idx
360360
require(cursor < len || i == _idx, "index out of bounds");
361361
}
362362

363363
return char;
364364
}
365365

366-
/// @notice Get the Unicode code point at `_idx` for `_str`
366+
/// @notice Get the Unicode code point at `_idx` for `self`
367367
/// @dev codePointAt requires a valid UTF-8 string
368-
/// @param _str The input string
368+
/// @param self The input string
369369
/// @param _idx The index of the code point to get
370370
/// @return The Unicode code point at the given index
371-
function codePointAt(string calldata _str, uint256 _idx)
371+
function codePointAt(string calldata self, uint256 _idx)
372372
external
373373
pure
374374
returns (uint32)
375375
{
376-
return toCodePoint(charAt(_str, _idx));
376+
return toCodePoint(charAt(self, _idx));
377377
}
378378

379379
/// @notice The return value of indexOf and bytesIndicesOf if the character is not found
380380
/// @dev Use CHAR_NOT_FOUND to check if indexOf or bytesIndicesOf does not find the inputted character
381381
uint256 public constant CHAR_NOT_FOUND = type(uint256).max;
382382

383-
/// @notice Get the character index of `_of` in string `_str`
384-
/// @dev indexOf returns CHAR_NOT_FOUND if `_of` isn't found in `_str`
385-
/// @param _str The input string
383+
/// @notice Get the character index of `_of` in string `self`
384+
/// @dev indexOf returns CHAR_NOT_FOUND if `_of` isn't found in `self`
385+
/// @param self The input string
386386
/// @param _of The character to find the index of
387387
/// @return The index of the character in the given string
388-
function indexOf(string calldata _str, string calldata _of)
388+
function indexOf(string calldata self, string calldata _of)
389389
external
390390
pure
391391
returns (uint256)
392392
{
393393
string memory char;
394394
uint256 cursor = 0;
395-
uint256 len = bytes(_str).length;
395+
uint256 len = bytes(self).length;
396396
uint256 idx;
397397

398398
while (cursor < len) {
399-
(char, cursor) = decodeChar(_str, cursor);
399+
(char, cursor) = decodeChar(self, cursor);
400400
if (keccak256(bytes(char)) == keccak256(bytes(_of))) return idx;
401401
idx++;
402402
}
403403

404404
return CHAR_NOT_FOUND;
405405
}
406406

407-
/// @notice Get the starting (inclusive) and ending (exclusive) bytes indices of character `_of` in string `_str`
408-
/// @dev bytesIndicesOf returns (CHAR_NOT_FOUND, CHAR_NOT_FOUND) if `_of` isn't found in `_str`
409-
/// @param _str The input string
407+
/// @notice Get the starting (inclusive) and ending (exclusive) bytes indices of character `_of` in string `self`
408+
/// @dev bytesIndicesOf returns (CHAR_NOT_FOUND, CHAR_NOT_FOUND) if `_of` isn't found in `self`
409+
/// @param self The input string
410410
/// @param _of The character to find the bytes indices of
411411
/// @return The starting (inclusive) and ending (exclusive) indites the character in the bytes underlying the string
412-
function bytesIndicesOf(string calldata _str, string calldata _of)
412+
function bytesIndicesOf(string calldata self, string calldata _of)
413413
external
414414
pure
415415
returns (uint256, uint256)
416416
{
417417
string memory char;
418418
uint256 start;
419419
uint256 cursor = 0;
420-
uint256 len = bytes(_str).length;
420+
uint256 len = bytes(self).length;
421421

422422
while (cursor < len) {
423423
// start is the prev cursor before the character
424424
start = cursor;
425-
(char, cursor) = decodeChar(_str, cursor);
425+
(char, cursor) = decodeChar(self, cursor);
426426
if (keccak256(bytes(char)) == keccak256(bytes(_of)))
427427
return (start, cursor);
428428
}

0 commit comments

Comments
 (0)