Skip to content

Instantly share code, notes, and snippets.

@hpcx82
Created July 24, 2011 08:40
Show Gist options
  • Save hpcx82/1102410 to your computer and use it in GitHub Desktop.
Save hpcx82/1102410 to your computer and use it in GitHub Desktop.
// UTF-8 is a variable-width encoding, with each character represented by one to four bytes, and for each UTF character, all // its non-first byte are start with 10xxxxxx, so the number of UTF characters could be counted by count the bytes that not
// start with 10xxxxxx, and here comes the algorithrm
// http://en.wikipedia.org/wiki/UTF-8#Design
int strlen_utf8_c(char *s) {
int i = 0, j = 0;
while (s[i]) {
if ((s[i] & 0xc0) != 0x80) j++;
i++;
}
return j;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment