UTF-8 Character Count

后端 未结 4 433
时光说笑
时光说笑 2021-01-23 10:17

I\'m programming something that counts the number of UTF-8 characters in a file. I\'ve already written the base code but now, I\'m stuck in the part where the characters are su

4条回答
  •  不知归路
    2021-01-23 10:32

    In C, as in C++, there is no ready-made solution for counting UTF-8 characters. You can convert UTF-8 to UTF-16 using mbstowcs and use the wcslen function, but this is not the best way for performance (especially if you only need to count the number of characters and nothing else).

    I think a good answer to your question is here: counting unicode characters in c++.

    Еxample from answer on link:

    for (p; *p != 0; ++p)
        count += ((*p & 0xc0) != 0x80);
    

提交回复
热议问题