Created
July 17, 2020 11:26
-
-
Save alphaKAI/9b676e29739eb0b5d08ec19bfc780460 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
void show_char_bits(char c) { | |
for (size_t i = 0; i < 8; i++) { | |
printf("%i", (c >> (7 - i)) & 1); | |
} | |
printf("\n"); | |
} | |
size_t utf8_strlen(char *str, size_t len) { | |
size_t utf8_len = 0, idx = 0; | |
for (unsigned char c = *str; idx < len; c = *(str + idx), utf8_len++) { | |
size_t c_len; | |
if ((c >> 7) == 0) { | |
c_len = 1; | |
} else if ((c >> 5) == 0b110) { | |
c_len = 2; | |
} else if ((c >> 4) == 0b1110) { | |
c_len = 3; | |
} else { | |
c_len = 4; | |
} | |
idx += c_len; | |
} | |
return utf8_len; | |
} | |
int main(int argc, char const* argv[]) { | |
char *str1 = "abcde"; | |
char *str2 = "あいうえお"; | |
#define STRLEN_UTF8STRLEN_CMP(id) \ | |
printf("str" #id ": %s, strlen: %zu, utf8_strlen: %zu\n", str##id, strlen(str##id), utf8_strlen(str##id, strlen(str##id))) | |
STRLEN_UTF8STRLEN_CMP(1); | |
STRLEN_UTF8STRLEN_CMP(2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment