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 | |
parent | 3f04f6768745e5fe08cd7b681d48097536980c77 (diff) |
Facebook: Fix cutting unicode strings (fixes #711)
-rw-r--r-- | protocols/FacebookRM/src/json.cpp | 3 | ||||
-rw-r--r-- | protocols/FacebookRM/src/process.cpp | 16 | ||||
-rw-r--r-- | utils/std_string_utils.cpp | 12 | ||||
-rw-r--r-- | utils/std_string_utils.h | 1 |
4 files changed, 16 insertions, 16 deletions
diff --git a/protocols/FacebookRM/src/json.cpp b/protocols/FacebookRM/src/json.cpp index e559f982f8..6325c2cca2 100644 --- a/protocols/FacebookRM/src/json.cpp +++ b/protocols/FacebookRM/src/json.cpp @@ -314,8 +314,7 @@ void parseAttachments(FacebookProto *proto, std::string *message_text, const JSO std::string link = share["uri"].as_string(); // shorten long descriptions - if (description.length() > MAX_LINK_DESCRIPTION_LEN) - description = description.substr(0, MAX_LINK_DESCRIPTION_LEN) + TEXT_ELLIPSIS; + description = utils::text::truncate_utf8(description, MAX_LINK_DESCRIPTION_LEN); if (link.find("//www." FACEBOOK_SERVER_DOMAIN "/l.php") != std::string::npos || link.find("//l." FACEBOOK_SERVER_DOMAIN) != std::string::npos) { // de-facebook this link diff --git a/protocols/FacebookRM/src/process.cpp b/protocols/FacebookRM/src/process.cpp index 4bf96e0e3c..6497145c00 100644 --- a/protocols/FacebookRM/src/process.cpp +++ b/protocols/FacebookRM/src/process.cpp @@ -516,18 +516,6 @@ void FacebookProto::LoadHistory(void *pParam) // PUDeletePopup(popupHwnd); } -std::string truncateUtf8(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 ttext = ptrW(mir_utf8decodeW(text.c_str())); - if (ttext.length() > maxLength) { - ttext = ttext.substr(0, maxLength) + L"\x2026"; // unicode ellipsis - return std::string(_T2A(ttext.c_str(), CP_UTF8)); - } - // It's not longer, return given string - return text; -} - void parseFeeds(const std::string &text, std::vector<facebook_newsfeed *> &news, DWORD &last_post_time, bool filterAds = true) { std::string::size_type pos = 0; UINT limit = 0; @@ -629,7 +617,7 @@ void parseFeeds(const std::string &text, std::vector<facebook_newsfeed *> &news, utils::text::html_entities_decode( utils::text::remove_html(post_attachment))); - post_attachment = truncateUtf8(post_attachment, MAX_LINK_DESCRIPTION_LEN); + post_attachment = utils::text::truncate_utf8(post_attachment, MAX_LINK_DESCRIPTION_LEN); if (post_attachment.empty()) { // This is some textless attachment, so mention it @@ -642,7 +630,7 @@ void parseFeeds(const std::string &text, std::vector<facebook_newsfeed *> &news, utils::text::remove_html(post_message))); // Truncate text of newsfeed when it's too long - post_message = truncateUtf8(post_message, MAX_NEWSFEED_LEN); + post_message = utils::text::truncate_utf8(post_message, MAX_NEWSFEED_LEN); std::string content = ""; if (header_rest.length() > 2) 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);
|