History of Idiom 169 > diff from v90 to v91
Edit summary for version 91 by programming-idioms.org:
[C++] Remove comments from snippet
[C++] Remove comments from snippet
âˇ
Version 90
2021-04-30, 23:08:58
Version 91
2021-05-23, 20:04:27
Idiom #169 String length
Assign to integer n the number of characters of string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.
Idiom #169 String length
Assign to integer n the number of characters of string s.
Make sure that multibyte characters are properly handled.
n can be different from the number of bytes of s.
Variables
n,sVariables
n,sExtra Keywords
size characters chars number runesExtra Keywords
size characters chars number runesCode
//function
auto utf8len(std::string_view const& str) -> size_t
{
std::setlocale(LC_ALL, "en_US.utf8");
auto charCount = size_t{};
auto size = size_t{};
auto mb = std::mbstate_t{};
while(size < str.length())
{
size += mbrlen(str.data() + size, str.length() - size, &mb);
charCount += 1;
}
return charCount;
}
//use
constexpr auto s = "zĂć°´đ";
auto n = utf8len(s); // 4