| 170 | /////////////////////////////////////////// |
| 171 | |
| 172 | size_t utf_charlen(const char* str_utf8) { |
| 173 | if (!str_utf8) return 0; |
| 174 | |
| 175 | // Single byte UTF8 characters all start with 0b0, so anything smaller than |
| 176 | // 0b1 must be single byte! |
| 177 | const unsigned char single_byte = (unsigned char)0b10000000; |
| 178 | // The first byte of multibyte UTF8 characters all begin with 0b11, so |
| 179 | // anything larger than 0b10111111 is a multibye character indicator! |
| 180 | const unsigned char multi_byte = (unsigned char)0b10111111; |
| 181 | |
| 182 | size_t result = 0; |
| 183 | const unsigned char* curr = (unsigned char*)str_utf8; |
| 184 | while (*curr != '\0') { |
| 185 | if (*curr < single_byte || *curr > multi_byte) { result += 1; } |
| 186 | curr++; |
| 187 | } |
| 188 | return result; |
| 189 | } |
| 190 | |
| 191 | /////////////////////////////////////////// |
| 192 | |