28 lines
569 B
C
28 lines
569 B
C
#include "utf8.h"
|
|
|
|
int charlen_utf8(char c) {
|
|
int charlen = 0;
|
|
|
|
if ((c & 0x80) == 0) // lead bit is zero, must be a single ascii
|
|
charlen = 1;
|
|
else if ((c & 0xE0) == 0xC0) // 110x xxxx
|
|
charlen = 2;
|
|
else if ((c & 0xF0) == 0xE0) // 1110 xxxx
|
|
charlen = 3;
|
|
else if ((c & 0xF8) == 0xF0) // 1111 0xxx
|
|
charlen = 4;
|
|
|
|
return charlen;
|
|
}
|
|
|
|
int strlen_utf8(const char *str) {
|
|
int utf8_len = 0;
|
|
int i = 0;
|
|
|
|
while (str[i] != '\0') {
|
|
i += charlen_utf8(str[i]);
|
|
utf8_len++;
|
|
}
|
|
|
|
return utf8_len;
|
|
} |