Skip to content

Commit

Permalink
move UTF-8 functions to a separate namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
aquileia committed Mar 22, 2014
1 parent 9351a25 commit 2f6a6a7
Show file tree
Hide file tree
Showing 25 changed files with 267 additions and 256 deletions.
2 changes: 1 addition & 1 deletion src/addon/manager_ui.cpp
Expand Up @@ -460,7 +460,7 @@ struct addon_pointer_list_sorter
switch(sort_) {
case SORT_NAMES:
// Alphanumerical by name, case insensitive.
return utils::lowercase(a->second.title) < utils::lowercase(b->second.title);
return utf8::lowercase(a->second.title) < utf8::lowercase(b->second.title);
case SORT_UPDATED:
// Numerical by last upload TS.
return a->second.updated < b->second.updated;
Expand Down
2 changes: 1 addition & 1 deletion src/campaign_server/campaign_server.cpp
Expand Up @@ -489,7 +489,7 @@ namespace {
std::transform(name.begin(), name.end(), lc_name.begin(), tolower);
config *campaign = NULL;
BOOST_FOREACH(config &c, campaigns().child_range("campaign")) {
if (utils::lowercase(c["name"]) == lc_name) {
if (utf8::lowercase(c["name"]) == lc_name) {
campaign = &c;
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/construct_dialog.cpp
Expand Up @@ -140,7 +140,7 @@ dialog::dialog(display &disp, const std::string& title, const std::string& messa
try {
std::string msg = font::word_wrap_text(message, message_font_size, screen.getx() / 2, screen.gety() / 2);
message_ = new label(screen, msg, message_font_size, font::NORMAL_COLOR, false);
} catch(utils::invalid_utf8_exception&) {
} catch(utf8::invalid_utf8_exception&) {
ERR_DP << "Problem handling utf8 in message '" << message << "'\n";
throw;
}
Expand Down
2 changes: 1 addition & 1 deletion src/construct_dialog.hpp
Expand Up @@ -223,7 +223,7 @@ class dialog {

//Constructor & destructor
//dialog - throws button::error() if standard buttons fail to initialize
// throws utils::invalid_utf8_exception() if message is invalid
// throws utf8::invalid_utf8_exception() if message is invalid
dialog(display &disp,
const std::string& title="",
const std::string& message="",
Expand Down
2 changes: 1 addition & 1 deletion src/dialogs.cpp
Expand Up @@ -938,7 +938,7 @@ std::string load_game_dialog(display& disp, const config& game_config, bool* sel
std::vector<savegame::save_info>::const_iterator i;
for(i = games.begin(); i != games.end(); ++i) {
std::string name = i->name();
utils::u8truncate(name, 40); // truncate only acts if the name is longer
utf8::truncate(name, 40); // truncate only acts if the name is longer

std::ostringstream str;
str << name << COLUMN_SEPARATOR << util::format_time_summary(i->modified());
Expand Down
2 changes: 1 addition & 1 deletion src/filesystem.cpp
Expand Up @@ -371,7 +371,7 @@ bool make_directory(const std::string& path)

bool looks_like_pbl(const std::string& file)
{
return utils::wildcard_string_match(utils::lowercase(file), "*.pbl");
return utils::wildcard_string_match(utf8::lowercase(file), "*.pbl");
}

// This deletes a directory with no hidden files and subdirectories.
Expand Down
10 changes: 5 additions & 5 deletions src/font.cpp
Expand Up @@ -177,10 +177,10 @@ static std::vector<text_chunk> split_text(std::string const & utf8_text) {
return chunks;

try {
utils::utf8_iterator ch(utf8_text);
utf8::iterator ch(utf8_text);
int sub = char_blocks.get_id(*ch);
if (sub >= 0) current_chunk.subset = sub;
for(utils::utf8_iterator end = utils::utf8_iterator::end(utf8_text); ch != end; ++ch)
for(utf8::iterator end = utf8::iterator::end(utf8_text); ch != end; ++ch)
{
sub = char_blocks.get_id(*ch);
if (sub >= 0 && sub != current_chunk.subset) {
Expand All @@ -194,7 +194,7 @@ static std::vector<text_chunk> split_text(std::string const & utf8_text) {
chunks.push_back(current_chunk);
}
}
catch(utils::invalid_utf8_exception&) {
catch(utf8::invalid_utf8_exception&) {
WRN_FT << "Invalid UTF-8 string: \"" << utf8_text << "\"\n";
}
return chunks;
Expand Down Expand Up @@ -886,9 +886,9 @@ std::string make_text_ellipsis(const std::string &text, int font_size,

std::string current_substring;

utils::utf8_iterator itor(text);
utf8::iterator itor(text);

for(; itor != utils::utf8_iterator::end(text); ++itor) {
for(; itor != utf8::iterator::end(text); ++itor) {
std::string tmp = current_substring;
tmp.append(itor.substr().first, itor.substr().second);

Expand Down
2 changes: 1 addition & 1 deletion src/game_display.cpp
Expand Up @@ -1062,7 +1062,7 @@ void game_display::add_chat_message(const time_t& time, const std::string& speak
// We've had a joker who send an invalid utf-8 message to crash clients
// so now catch the exception and ignore the message.
msg = font::word_wrap_text(msg,font::SIZE_SMALL,map_outside_area().w*3/4);
} catch (utils::invalid_utf8_exception&) {
} catch (utf8::invalid_utf8_exception&) {
ERR_NG << "Invalid utf-8 found, chat message is ignored.\n";
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/game_events/action_wml.cpp
Expand Up @@ -1493,7 +1493,7 @@ WML_HANDLER_FUNCTION(object, event_info, cfg)

try {
gui2::show_transient_message(resources::screen->video(), caption, text, image, true);
} catch(utils::invalid_utf8_exception&) {
} catch(utf8::invalid_utf8_exception&) {
// we already had a warning so do nothing.
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/gui/auxiliary/filter.hpp
Expand Up @@ -66,7 +66,7 @@ inline bool contains(const tpane::titem& item,
const std::string& tag,
const ttext_box& text_box)
{
return at(item.tags, tag).find(utils::lowercase(text_box.text()))
return at(item.tags, tag).find(utf8::lowercase(text_box.text()))
!= std::string::npos;
}

Expand Down
18 changes: 9 additions & 9 deletions src/gui/dialogs/addon_list.cpp
Expand Up @@ -176,16 +176,16 @@ void taddon_list::pre_show(CVideo& /*video*/, twindow& window)
item["label"] = c["icon"];
data.insert(std::make_pair("icon", item));

utf8_string tmp = c["name"];
item["label"] = utils::u8truncate(tmp, 20);
utf8::string tmp = c["name"];
item["label"] = utf8::truncate(tmp, 20);
data.insert(std::make_pair("name", item));

tmp = c["version"].str();
item["label"] = utils::u8truncate(tmp, 12);
item["label"] = utf8::truncate(tmp, 12);
data.insert(std::make_pair("version", item));

tmp = c["author"].str();
item["label"] = utils::u8truncate(tmp, 16);
item["label"] = utf8::truncate(tmp, 16);
data.insert(std::make_pair("author", item));

item["label"] = c["downloads"];
Expand All @@ -209,16 +209,16 @@ void taddon_list::create_campaign(tpane& pane, const config& campaign)
item["label"] = campaign["icon"];
data.insert(std::make_pair("icon", item));

utf8_string tmp = campaign["name"];
item["label"] = utils::u8truncate(tmp, 20);
utf8::string tmp = campaign["name"];
item["label"] = utf8::truncate(tmp, 20);
data.insert(std::make_pair("name", item));

tmp = campaign["version"].str();
item["label"] = utils::u8truncate(tmp, 12);
item["label"] = utf8::truncate(tmp, 12);
data.insert(std::make_pair("version", item));

tmp = campaign["author"].str();
item["label"] = utils::u8truncate(tmp, 16);
item["label"] = utf8::truncate(tmp, 16);
data.insert(std::make_pair("author", item));

item["label"] = campaign["downloads"];
Expand All @@ -241,7 +241,7 @@ void taddon_list::create_campaign(tpane& pane, const config& campaign)
filter << campaign["version"] << '\n' << campaign["author"] << '\n'
<< campaign["type"] << '\n' << campaign["description"];

tags.insert(std::make_pair("filter", utils::lowercase(filter.str())));
tags.insert(std::make_pair("filter", utf8::lowercase(filter.str())));

/***** Add the campaign. *****/

Expand Down
8 changes: 4 additions & 4 deletions src/gui/dialogs/chat_log.cpp
Expand Up @@ -106,7 +106,7 @@ class tchat_log::model

void populate_chat_message_list(int first, int last)
{
const std::string& lcfilter = utils::lowercase(filter->get_value());
const std::string& lcfilter = utf8::lowercase(filter->get_value());
std::stringstream str;
LOG_CHAT_LOG
<< "entering tchat_log::model::add_row_to_chat_message_list\n";
Expand All @@ -120,9 +120,9 @@ class tchat_log::model
= preferences::get_chat_timestamp(t.time());

if(lcfilter.empty() == false) {
const std::string& lcsample = utils::lowercase(timestamp)
+ utils::lowercase(t.nick())
+ utils::lowercase(t.text());
const std::string& lcsample = utf8::lowercase(timestamp)
+ utf8::lowercase(t.nick())
+ utf8::lowercase(t.text());

if(lcsample.find(lcfilter) == std::string::npos) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion src/gui/widgets/password_box.cpp
Expand Up @@ -40,7 +40,7 @@ namespace

size_t get_text_length(const std::string& str)
{
return utils::u8size(str);
return utf8::size(str);
}

} // namespace
Expand Down
10 changes: 5 additions & 5 deletions src/gui/widgets/text.cpp
Expand Up @@ -145,15 +145,15 @@ void ttext_::copy_selection(const bool mouse)
if(selection_length_ == 0) return;

unsigned end,start = selection_start_;
const utf8_string txt = text_.text();
const utf8::string txt = text_.text();

if(selection_length_ > 0) {
end = utils::u8index(txt,start+selection_length_);
start = utils::u8index(txt,start);
end = utf8::index(txt,start+selection_length_);
start = utf8::index(txt,start);
} else {
// inverse selection: selection_start_ is in fact the end
end = utils::u8index(txt,start);
start = utils::u8index(txt,start+selection_length_);
end = utf8::index(txt,start);
start = utf8::index(txt,start+selection_length_);
}
copy_to_clipboard(txt.substr(start,end-start), mouse);
}
Expand Down
4 changes: 2 additions & 2 deletions src/gui/widgets/text_box.cpp
Expand Up @@ -208,8 +208,8 @@ void ttext_box::delete_selection()
start -= len;
}

utf8_string tmp = get_value();
set_value(utils::u8erase(tmp, start, len));
utf8::string tmp = get_value();
set_value(utf8::erase(tmp, start, len));
set_cursor(start, false);
}

Expand Down
6 changes: 3 additions & 3 deletions src/help.cpp
Expand Up @@ -3310,7 +3310,7 @@ std::vector<std::string> split_in_width(const std::string &s, const int font_siz
res.push_back(s.substr(first_line.size()));
}
}
catch (utils::invalid_utf8_exception&)
catch (utf8::invalid_utf8_exception&)
{
throw parse_error (_("corrupted original file"));
}
Expand Down Expand Up @@ -3341,8 +3341,8 @@ std::string get_first_word(const std::string &s)
//if no gap(' ' or '\n') found, test if it is CJK character
std::string re = s.substr(0, first_word_end);

utils::utf8_iterator ch(re);
if (ch == utils::utf8_iterator::end(re))
utf8::iterator ch(re);
if (ch == utf8::iterator::end(re))
return re;

wchar_t firstchar = *ch;
Expand Down
10 changes: 5 additions & 5 deletions src/marked-up_text.cpp
Expand Up @@ -321,10 +321,10 @@ bool is_cjk_char(const wchar_t c)
static void cut_word(std::string& line, std::string& word, int font_size, int style, int max_width)
{
std::string tmp = line;
utils::utf8_iterator tc(word);
utf8::iterator tc(word);
bool first = true;

for(;tc != utils::utf8_iterator::end(word); ++tc) {
for(;tc != utf8::iterator::end(word); ++tc) {
tmp.append(tc.substr().first, tc.substr().second);
SDL_Rect tsize = line_size(tmp, font_size, style);
if(tsize.w > max_width) {
Expand Down Expand Up @@ -440,7 +440,7 @@ std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
{
VALIDATE(max_width > 0, _("The maximum text width is less than 1."));

utils::utf8_iterator ch(unwrapped_text);
utf8::iterator ch(unwrapped_text);
std::string current_word;
std::string current_line;
size_t line_width = 0;
Expand All @@ -453,7 +453,7 @@ std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
SDL_Color color;
int font_sz = font_size;
int style = TTF_STYLE_NORMAL;
utils::utf8_iterator end = utils::utf8_iterator::end(unwrapped_text);
utf8::iterator end = utf8::iterator::end(unwrapped_text);

while(1) {
if(start_of_line) {
Expand Down Expand Up @@ -483,7 +483,7 @@ std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
++ch;
} else {
wchar_t previous = 0;
for(;ch != utils::utf8_iterator::end(unwrapped_text) &&
for(;ch != utf8::iterator::end(unwrapped_text) &&
*ch != ' ' && *ch != '\n'; ++ch) {

if(!current_word.empty() &&
Expand Down
2 changes: 1 addition & 1 deletion src/savegame.cpp
Expand Up @@ -566,7 +566,7 @@ void loadgame::load_game(
gui2::show_error_message(gui_.video(),
_("Warning: The file you have tried to load is corrupt. Loading anyway.\n") +
error_log);
} catch (utils::invalid_utf8_exception&) {
} catch (utf8::invalid_utf8_exception&) {
gui2::show_error_message(gui_.video(),
_("Warning: The file you have tried to load is corrupt. Loading anyway.\n") +
std::string("(UTF-8 ERROR)"));
Expand Down

0 comments on commit 2f6a6a7

Please sign in to comment.