Created
October 6, 2017 17:28
-
-
Save thomasmaclean/b7fec811301afe64e34095aa9280b4e8 to your computer and use it in GitHub Desktop.
Solidity contract that filters out non-ASCII characters and allow only non-boundary, single-spaces
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
pragma solidity ^0.4.11; | |
contract FilterString { | |
// Filters out non-ASCII characters and allow only non-boundary, single-spaces | |
function _filter(string str) public returns (string) { | |
bytes memory bytesStr = bytes(str); | |
bytes memory valid = new bytes(bytesStr.length); | |
uint chr = 0; | |
uint ltrim = 0; | |
for (uint i = 0; i < bytesStr.length; i++) { | |
// Only add space characters if they're not on a boundary or duplicates | |
if (bytesStr[i] == 32) { | |
if (i != 0 && i != bytesStr.length - 1 && chr != 0 && valid[chr - 1] != 32) { | |
valid[chr] = bytesStr[i]; | |
chr++; | |
} | |
// Filter out control characters | |
} else if (bytesStr[i] < 127 && bytesStr[i] > 31) { | |
valid[chr] = bytesStr[i]; | |
chr++; | |
} | |
} | |
// If nothing has been filtered by now we're clean and can return | |
if (bytesStr.length == chr) { | |
return string(valid); | |
} | |
// Remove any additional boundary spaces | |
while (valid[chr - 1] == 32) { | |
chr--; | |
} | |
while (valid[ltrim] == 32) { | |
ltrim++; | |
} | |
// Create a bytes array at the new length and return as a string | |
bytes memory shortened = new bytes(chr - ltrim); | |
for (uint c = ltrim; c < chr; c++) { | |
shortened[c] = valid[c]; | |
} | |
return string(shortened); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment