Skip to content

Instantly share code, notes, and snippets.

@thomasmaclean
Created October 6, 2017 17:28
Show Gist options
  • Save thomasmaclean/b7fec811301afe64e34095aa9280b4e8 to your computer and use it in GitHub Desktop.
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
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