diff --git a/services/backup/src/Tools.cpp b/services/backup/src/Tools.cpp --- a/services/backup/src/Tools.cpp +++ b/services/backup/src/Tools.cpp @@ -3,6 +3,7 @@ #include "GlobalConstants.h" #include "GlobalTools.h" +#include #include #include #include @@ -52,6 +53,22 @@ return result; } +std::size_t getUtf8Length(std::string &str) { + + // How it works? + // Every byte of multibyte utf8 character starts with information about its + // own type. Additional bytes always start with a 2bit sequence "10xx xxxx" + // (which is equal to hex 0x80). To count the length of utf8 string we need + // to skip these bytes, so we use a bitmask. + // + // We need to compare first 2 bits of a byte, so we use bitwise operator + // & with mask 0xC0. If returned value is equal to 0x80, we skip it. + + return std::count_if(str.begin(), str.end(), [](char c) { + return (static_cast(c) & 0xC0) != 0x80; + }); +} + } // namespace tools } // namespace network } // namespace comm