History of Idiom 169 > diff from v89 to v90
Edit summary for version 90 by Steranoid:
New C++ implementation by user [Steranoid]
New C++ implementation by user [Steranoid]
âˇ
Version 89
2021-03-24, 21:46:14
Version 90
2021-04-30, 23:08:58
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