-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
77 changed files
with
2,877 additions
and
1,040 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2020-2024 Trust Computing GmbH. | ||
// This file is part of Litentry. | ||
// | ||
// Litentry is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Litentry is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Litentry. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
/** | ||
* @title StringCleaner | ||
* @dev A library for cleaning strings by removing non-visible ASCII characters. | ||
*/ | ||
library StringCleaner { | ||
/** | ||
* @dev Cleans the input string by removing non-visible ASCII characters. | ||
* It iterates through each character in the string and retains only visible characters | ||
* (ASCII range 0x20 to 0x7E). | ||
* @param str The input string to be cleaned. | ||
* @return The cleaned string with only visible characters. | ||
*/ | ||
function cleanString( | ||
string memory str | ||
) internal pure returns (string memory) { | ||
bytes memory b = bytes(str); | ||
bytes memory cleaned = new bytes(b.length); | ||
uint256 cleanedIndex = 0; | ||
|
||
for (uint256 i = 0; i < b.length; i++) { | ||
bytes1 char = b[i]; | ||
if (isVisibleChar(char)) { | ||
cleaned[cleanedIndex] = char; | ||
cleanedIndex++; | ||
} | ||
} | ||
|
||
// Create a new bytes array of the correct length and copy cleaned characters into it | ||
bytes memory trimmedCleaned = new bytes(cleanedIndex); | ||
for (uint256 j = 0; j < cleanedIndex; j++) { | ||
trimmedCleaned[j] = cleaned[j]; | ||
} | ||
|
||
return string(trimmedCleaned); | ||
} | ||
|
||
/** | ||
* @dev Checks if a given character is a visible ASCII character. | ||
* This includes characters in the ASCII range from 0x20 (space) to 0x7E (~). | ||
* @param char The character to be checked. | ||
* @return True if the character is visible, false otherwise. | ||
*/ | ||
function isVisibleChar(bytes1 char) internal pure returns (bool) { | ||
return (char >= 0x20 && char <= 0x7E); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// Copyright 2020-2024 Trust Computing GmbH. | ||
// This file is part of Litentry. | ||
// | ||
// Litentry is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// Litentry is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with Litentry. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
// SPDX-License-Identifier: GPL-3.0-or-later | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
library StringComparison { | ||
/** | ||
* @notice Converts a given string to lowercase. | ||
* @dev This function iterates through each character of the input string, | ||
* checks if it is an uppercase letter (A-Z), and converts it to the | ||
* corresponding lowercase letter (a-z). Characters outside the range | ||
* of uppercase letters remain unchanged. | ||
* @param str The input string to be converted to lowercase. | ||
* @return A new string with all uppercase letters converted to lowercase. | ||
*/ | ||
function toLower(string memory str) internal pure returns (string memory) { | ||
bytes memory bStr = bytes(str); | ||
bytes memory bLower = new bytes(bStr.length); | ||
|
||
for (uint i = 0; i < bStr.length; i++) { | ||
// Uppercase character range in ASCII: A-Z (65-90) | ||
if (bStr[i] >= 0x41 && bStr[i] <= 0x5A) { | ||
// Convert to lowercase by adding 32 (A -> a, B -> b, ..., Z -> z) | ||
bLower[i] = bytes1(uint8(bStr[i]) + 32); | ||
} else { | ||
bLower[i] = bStr[i]; | ||
} | ||
} | ||
|
||
return string(bLower); | ||
} | ||
|
||
/** | ||
* @notice Compares two strings for equality, ignoring case. | ||
* @dev Converts both input strings to lowercase using the `toLower` function | ||
* and then compares their keccak256 hashes to determine if they are equal. | ||
* @param str1 The first string to compare. | ||
* @param str2 The second string to compare. | ||
* @return A boolean value indicating whether the two strings are equal, ignoring case. | ||
*/ | ||
function compareStringsIgnoreCase( | ||
string memory str1, | ||
string memory str2 | ||
) internal pure returns (bool) { | ||
return | ||
keccak256(abi.encodePacked(toLower(str1))) == | ||
keccak256(abi.encodePacked(toLower(str2))); | ||
} | ||
} |
Oops, something went wrong.