Further reorganise unicode.hpp

Create a unicode_cast<utf8::string, ucs4::char_t> specialisation.
Move remaining functions in the utils namespace to implemenation.
This commit is contained in:
Alexander van Gessel 2014-04-01 03:11:59 +02:00
parent 9d0a05cadc
commit ea8a3bbcd9
5 changed files with 19 additions and 17 deletions

View File

@ -3347,7 +3347,7 @@ std::string get_first_word(const std::string &s)
ucs4::char_t firstchar = *ch;
if (font::is_cjk_char(firstchar)) {
re = utils::ucs4char_to_string(firstchar);
re = unicode_cast<utf8::string>(firstchar);
}
return re;
}

View File

@ -462,7 +462,7 @@ void hotkey_item::save(config& item) const
if (get_hat() >= 0) item["hat"] = get_hat();
if (get_value() >= 0) item["value"] = get_value();
if (get_keycode() >= 0) item["key"] = SDL_GetKeyName(SDLKey(get_keycode()));
if (get_character() >= 0) item["key"] = utils::ucs4char_to_string(get_character());
if (get_character() >= 0) item["key"] = unicode_cast<utf8::string, ucs4::char_t>(get_character()); // Second template argument because get_character returns a signed int
if (get_mouse() >= 0) item["mouse"] = get_mouse();
if (get_button() >= 0) item["button"] = get_button();

View File

@ -52,7 +52,7 @@ size_t byte_size_from_ucs4_codepoint(ucs4::char_t ch)
}
} // anonymous namespace
namespace utils {
namespace implementation {
std::string ucs4string_to_string(const ucs4::string &src)
{
std::string ret;
@ -139,7 +139,7 @@ utf16::string ucs4string_to_utf16string(const ucs4::string &src)
return res;
}
} // utils namespace
} // implementation namespace
namespace utf8 {
@ -257,7 +257,7 @@ utf8::string lowercase(const utf8::string& s)
{
if(!s.empty()) {
utf8::iterator itor(s);
std::string res;
utf8::string res;
for(;itor != utf8::iterator::end(s); ++itor) {
ucs4::char_t uchar = *itor;
@ -265,7 +265,7 @@ utf8::string lowercase(const utf8::string& s)
if(uchar <= static_cast<ucs4::char_t>(std::numeric_limits<wchar_t>::max()) &&
uchar >= static_cast<ucs4::char_t>(std::numeric_limits<wchar_t>::min()))
uchar = towlower(static_cast<wchar_t>(uchar));
res += utils::ucs4char_to_string(uchar);
res += unicode_cast<utf8::string>(uchar);
}
res.append(itor.substr().second, s.end());
@ -327,10 +327,10 @@ utf8::string& truncate(utf8::string& str, const size_t size)
void truncate_as_ucs4(utf8::string &str, const size_t size)
{
ucs4::string u4_str = utils::string_to_ucs4string(str);
ucs4::string u4_str = unicode_cast<ucs4::string>(str);
if(u4_str.size() > size) {
u4_str.resize(size);
str = utils::ucs4string_to_string(u4_str);
str = unicode_cast<utf8::string>(u4_str);
}
}

View File

@ -117,15 +117,12 @@ namespace utf8 {
void truncate_as_ucs4(utf8::string& str, const size_t size);
} // end namespace utf8
namespace utils {
namespace implementation {
std::string ucs4string_to_string(const ucs4::string &);
ucs4::string string_to_ucs4string(const std::string &);
std::string ucs4char_to_string(const ucs4::char_t);
utf16::string ucs4string_to_utf16string(const ucs4::string &);
} // end namespace utils
} // end namespace implementation
template <typename To, typename From> inline
To unicode_cast(const From &) {
@ -135,17 +132,22 @@ To unicode_cast(const From &) {
template <> inline
utf8::string unicode_cast<utf8::string, ucs4::string>(const ucs4::string &in) {
return utils::ucs4string_to_string(in);
return implementation::ucs4string_to_string(in);
}
template <> inline
ucs4::string unicode_cast<ucs4::string, utf8::string>(const utf8::string &in) {
return utils::string_to_ucs4string(in);
return implementation::string_to_ucs4string(in);
}
template <> inline
utf8::string unicode_cast<utf8::string, ucs4::char_t>(const ucs4::char_t &in) {
return implementation::ucs4char_to_string(in);
}
template <> inline
utf16::string unicode_cast<utf16::string, ucs4::string>(const ucs4::string &in) {
return utils::ucs4string_to_utf16string(in);
return implementation::ucs4string_to_utf16string(in);
}
template <> inline

View File

@ -313,7 +313,7 @@ surface textbox::add_text_line(const ucs4::string& text, const SDL_Color& color)
if(char(*itor) == ' ') {
backup_itor = itor;
}
visible_string.append(utils::ucs4char_to_string(*itor));
visible_string.append(unicode_cast<utf8::string>(*itor));
if(char(*itor) == '\n') {
backup_itor = text.end();