Skip to content

Instantly share code, notes, and snippets.

@Hero-Development
Created November 19, 2022 18:16
Show Gist options
  • Save Hero-Development/c651b18b9632c9357c0344d3a3ed46fe to your computer and use it in GitHub Desktop.
Save Hero-Development/c651b18b9632c9357c0344d3a3ed46fe to your computer and use it in GitHub Desktop.
Solidity code to find space, octothorp, or ampersand and remove them
function remove(string calldata input) external pure returns(bytes memory result, uint256 resultLength){
//bit mask
bytes1 precheck = bytes1(" ");
uint256 needlesLength = 3;
bytes1[] memory needles = new bytes1[](needlesLength);
needles[0] = bytes1(" "); // 0010 0000, 32, 20
needles[1] = bytes1("#"); // 0010 0111, 35, 23
needles[2] = bytes1("&"); // 0010 1011, 38, 26
bytes1 char;
bool add = true;
bytes memory inputBytes = bytes(input);
uint256 inputLength = inputBytes.length;
result = new bytes(inputBytes.length);
unchecked{
for(uint256 i = 0; i < inputLength; ++i){
char = inputBytes[i];
if(uint8(char & precheck) > 0){
for(uint256 j = 0; j < needlesLength; ++j ){
if(char == needles[j]){
add = false;
break;
}
}
}
if(add)
result[resultLength++] = char;
else
add = true;
}
}
return (result, resultLength);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment