Skip to content

Instantly share code, notes, and snippets.

@AlmostEfficient
Created February 9, 2022 18:21
Show Gist options
  • Save AlmostEfficient/669ac250214f30347097a1aeedcdfa12 to your computer and use it in GitHub Desktop.
Save AlmostEfficient/669ac250214f30347097a1aeedcdfa12 to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
// Source:
// https://github.com/ensdomains/ens-contracts/blob/master/contracts/ethregistrar/StringUtils.sol
pragma solidity >=0.8.4;
library StringUtils {
/**
* @dev Returns the length of a given string
*
* @param s The string to measure the length of
* @return The length of the input string
*/
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++) {
bytes1 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;
}
}
@RV12R
Copy link

RV12R commented Jul 16, 2022

Thanks !

@developeruche
Copy link

Amazing code!!!

@cluck135
Copy link

Awesomee

@samailamalima
Copy link

Awesome code man

@RayanAbid
Copy link

Awesome

@meneergroot
Copy link

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment