Skip to content

Instantly share code, notes, and snippets.

@makoto
Created May 15, 2019 19:21
Show Gist options
  • Save makoto/c0241df25c16baff0701d1acad87b98c to your computer and use it in GitHub Desktop.
Save makoto/c0241df25c16baff0701d1acad87b98c to your computer and use it in GitHub Desktop.
pragma solidity ^0.5.0;
contract StringCounter {
function strlen(string memory s) internal pure returns (uint) {
uint len;
uint i = 0;
uint bytelength = bytes(s).length;
for(len = 0; i < bytelength; len++) {
byte b = bytes(s)[i];
if(b < 0x80) {
i += 1;
} else if (b < 0xE0) {
i += 2;
} else if (b < 0xF0) {
i += 3;
} else if (b < 0xF8) {
i += 4;
} else if (b < 0xFC) {
i += 5;
} else {
i += 6;
}
}
return len;
}
function valid(string memory name) public view returns(bool) {
return strlen(name) > 6;
}
function length(string memory name) public view returns(uint) {
return strlen(name);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment