diff options
author | Robert Pösel <robyer@seznam.cz> | 2017-03-05 09:15:39 +0100 |
---|---|---|
committer | Robert Pösel <robyer@seznam.cz> | 2017-03-05 09:15:39 +0100 |
commit | cc8f9defc89f3c56088ac8de2e979938a0e85947 (patch) | |
tree | 42768529e6e51ba7855b416b0ab33a2f8409d7b9 /utils | |
parent | 3f04f6768745e5fe08cd7b681d48097536980c77 (diff) |
Facebook: Fix cutting unicode strings (fixes #711)
Diffstat (limited to 'utils')
-rw-r--r-- | utils/std_string_utils.cpp | 12 | ||||
-rw-r--r-- | utils/std_string_utils.h | 1 |
2 files changed, 13 insertions, 0 deletions
diff --git a/utils/std_string_utils.cpp b/utils/std_string_utils.cpp index c1b46a8015..dc763524f3 100644 --- a/utils/std_string_utils.cpp +++ b/utils/std_string_utils.cpp @@ -417,6 +417,18 @@ std::string utils::text::rand_string(int len, const char *chars, unsigned int *n return out.str(); } +std::string utils::text::truncate_utf8(const std::string &text, size_t maxLength) { + // To not split some unicode character we need to transform it to wchar_t first, then split it, and then convert it back, because we want std::string as result + // TODO: Probably there is much simpler and nicer way + std::wstring wtext = ptrW(mir_utf8decodeW(text.c_str())); + if (wtext.length() > maxLength) { + wtext = wtext.substr(0, maxLength) + L"\x2026"; // unicode ellipsis + return std::string(_T2A(wtext.c_str(), CP_UTF8)); + } + // It's not longer, return given string + return text; +} + int utils::number::random(int min, int max, unsigned int *number) { if (number != NULL) { diff --git a/utils/std_string_utils.h b/utils/std_string_utils.h index a49062a0d1..96ee5ca4d9 100644 --- a/utils/std_string_utils.h +++ b/utils/std_string_utils.h @@ -73,6 +73,7 @@ namespace utils std::string source_get_value2(std::string* data, const char *term, const char *endings, bool wholeString = false);
std::string source_get_form_data(std::string* data, bool hiddenOnly = false);
std::string rand_string(int len, const char *chars = "0123456789" "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz", unsigned int *number = NULL);
+ std::string truncate_utf8(const std::string &text, size_t maxLength);
void explode(std::string str, const std::string &separator, std::vector<std::string>* results);
void append_ordinal(unsigned long value, std::string* data);
std::wstring prepare_name(const std::wstring &name, bool withSurnameLetter);
|