static string TruncateUTF8(const char* str, size_t maxLength) { string wordTrunc; if (NotShorterThan(str, maxLength)) { size_t len = 0; const char* pStr = str; while (len < maxLength) { size_t nextLen = NextCharLength(pStr); pStr += nextLen; len += nextLen; } wordTrunc = FromSubstr(str, len); } else { wordTrunc = str; } return wordTrunc; }
/** * Truncates a string with a maximal length in byte. * No UTF8 character will be broken. */ static string TruncateUTF8(const char* str, size_t maxByteLength) { string wordTrunc; if (NotShorterThan(str, maxByteLength)) { size_t len = 0; const char* pStr = str; for (;;) { const size_t charLength = NextCharLength(pStr); if (len + charLength > maxByteLength) { break; } pStr += charLength; len += charLength; } wordTrunc = FromSubstr(str, len); } else { wordTrunc = str; } return wordTrunc; }