Skip to content

Commit

Permalink
Rename utils::trancate_as_ucs4string to utf8::truncate_as_ucs4
Browse files Browse the repository at this point in the history
  • Loading branch information
AI0867 committed Apr 1, 2014
1 parent 7e128d0 commit 9d0a05c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/addon/manager_ui.cpp
Expand Up @@ -664,10 +664,10 @@ void show_addons_manager_dialog(display& disp, addons_client& client, addons_lis
// Versions are too important in upgrades mode, so don't
// truncate them then.
if(!updates_only) {
utils::truncate_as_ucs4string(display_version, 12);
utf8::truncate_as_ucs4(display_version, 12);

if(state == ADDON_INSTALLED_UPGRADABLE || state == ADDON_INSTALLED_OUTDATED) {
utils::truncate_as_ucs4string(display_old_version, 12);
utf8::truncate_as_ucs4(display_old_version, 12);

if(state == ADDON_INSTALLED_UPGRADABLE) {
display_version =
Expand Down
18 changes: 9 additions & 9 deletions src/serialization/unicode.cpp
Expand Up @@ -139,15 +139,6 @@ utf16::string ucs4string_to_utf16string(const ucs4::string &src)
return res;
}

void truncate_as_ucs4string(std::string& str, const size_t size)
{
ucs4::string u4_str = utils::string_to_ucs4string(str);
if(u4_str.size() > size) {
u4_str.resize(size);
str = utils::ucs4string_to_string(u4_str);
}
}

} // utils namespace

namespace utf8 {
Expand Down Expand Up @@ -334,4 +325,13 @@ utf8::string& truncate(utf8::string& str, const size_t size)
return erase(str, size);
}

void truncate_as_ucs4(utf8::string &str, const size_t size)
{
ucs4::string u4_str = utils::string_to_ucs4string(str);
if(u4_str.size() > size) {
u4_str.resize(size);
str = utils::ucs4string_to_string(u4_str);
}
}

} // end namespace utf8
28 changes: 14 additions & 14 deletions src/serialization/unicode.hpp
Expand Up @@ -101,6 +101,20 @@ namespace utf8 {
*/
utf8::string& truncate(utf8::string& str, const size_t size);

/**
* Truncate a UTF-8 encoded string.
*
* If the string has more than @p size UTF-8 characters it will be truncated
* to this size.
* The output is guaranteed to be valid UTF-8.
*
* @param[in, out] str The parameter's usage is:
* - Input: String encoded in UTF-8.
* - Output: String encoded UTF-8 that contains at most @p size
* codepoints.
* @param size The size to truncate at.
*/
void truncate_as_ucs4(utf8::string& str, const size_t size);
} // end namespace utf8

namespace utils {
Expand All @@ -110,20 +124,6 @@ namespace utils {

utf16::string ucs4string_to_utf16string(const ucs4::string &);

/**
* Truncates a string.
*
* If the string send has more than size utf-8 characters it will be truncated
* to this size.
* No assumptions can be made about the actual size of the string.
*
* @param[in, out] str The parameter's usage is:
* - Input: String which can be converted to utf-8.
* - Output: String which contains maximal size utf-8
* characters.
* @param size The size to truncate at.
*/
void truncate_as_ucs4string(std::string& str, const size_t size);

} // end namespace utils

Expand Down
2 changes: 1 addition & 1 deletion src/server/player_network.cpp
Expand Up @@ -34,7 +34,7 @@ void truncate_message(const simple_wml::string_span& str, simple_wml::node& mess
std::string tmp(str.begin(), str.end());
// The string can contain utf-8 characters so truncate as ucs4::string otherwise
// a corrupted utf-8 string can be returned.
utils::truncate_as_ucs4string(tmp, max_message_length);
utf8::truncate_as_ucs4(tmp, max_message_length);
message.set_attr_dup("message", tmp.c_str());
}
}
Expand Down

0 comments on commit 9d0a05c

Please sign in to comment.