Skip to content

Commit b0784ba

Browse files
committedJul 8, 2015
Use UTF-8 instead of narrow
Use wide_to_utf8 and utf8_to_wide instead of wide_to_narrow and narrow_to_wide at almost all places. Only exceptions: test functions for narrow conversion, and chat, which is done in a separate commit.
1 parent e234d8b commit b0784ba

19 files changed

+80
-67
lines changed
 

Diff for: ‎src/client/clientlauncher.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ bool ClientLauncher::run(GameParams &game_params, const Settings &cmd_args)
183183
{
184184
// Set the window caption
185185
const wchar_t *text = wgettext("Main Menu");
186-
device->setWindowCaption((narrow_to_wide(PROJECT_NAME_C) + L" [" + text + L"]").c_str());
186+
device->setWindowCaption((utf8_to_wide(PROJECT_NAME_C) + L" [" + text + L"]").c_str());
187187
delete[] text;
188188

189189
try { // This is used for catching disconnects

Diff for: ‎src/content_cao.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -970,7 +970,7 @@ void GenericCAO::addToScene(scene::ISceneManager *smgr, ITextureSource *tsrc,
970970
if (node && m_is_player && !m_is_local_player) {
971971
// Add a text node for showing the name
972972
gui::IGUIEnvironment* gui = irr->getGUIEnvironment();
973-
std::wstring wname = narrow_to_wide(m_name);
973+
std::wstring wname = utf8_to_wide(m_name);
974974
m_textnode = smgr->addTextSceneNode(gui->getSkin()->getFont(),
975975
wname.c_str(), m_nametag_color, node);
976976
m_textnode->grab();

Diff for: ‎src/fontengine.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class FontEngine
5959
unsigned int font_size=FONT_SIZE_UNSPECIFIED,
6060
FontMode mode=FM_Unspecified)
6161
{
62-
return getTextWidth(narrow_to_wide(text));
62+
return getTextWidth(utf8_to_wide(text));
6363
}
6464

6565
/** get text width if a text for a specific font */

Diff for: ‎src/game.cpp

+29-29
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct TextDestNodeMetadata : public TextDest {
8585
// This is deprecated I guess? -celeron55
8686
void gotText(std::wstring text)
8787
{
88-
std::string ntext = wide_to_narrow(text);
88+
std::string ntext = wide_to_utf8(text);
8989
infostream << "Submitting 'text' field of node at (" << m_p.X << ","
9090
<< m_p.Y << "," << m_p.Z << "): " << ntext << std::endl;
9191
StringMap fields;
@@ -183,7 +183,7 @@ struct LocalFormspecHandler : public TextDest {
183183
(fields.find("quit") != fields.end())) {
184184
StringMap::const_iterator it = fields.find("f_text");
185185
if (it != fields.end())
186-
m_client->typeChatMessage(narrow_to_wide(it->second));
186+
m_client->typeChatMessage(utf8_to_wide(it->second));
187187

188188
return;
189189
}
@@ -445,7 +445,7 @@ void update_profiler_gui(gui::IGUIStaticText *guitext_profiler, FontEngine *fe,
445445

446446
std::ostringstream os(std::ios_base::binary);
447447
g_profiler->printPage(os, show_profiler, show_profiler_max);
448-
std::wstring text = narrow_to_wide(os.str());
448+
std::wstring text = utf8_to_wide(os.str());
449449
guitext_profiler->setText(text.c_str());
450450
guitext_profiler->setVisible(true);
451451

@@ -582,16 +582,16 @@ class ProfilerGraph
582582
s32 texth = 15;
583583
char buf[10];
584584
snprintf(buf, 10, "%.3g", show_max);
585-
font->draw(narrow_to_wide(buf).c_str(),
585+
font->draw(utf8_to_wide(buf).c_str(),
586586
core::rect<s32>(textx, y - graphh,
587587
textx2, y - graphh + texth),
588588
meta.color);
589589
snprintf(buf, 10, "%.3g", show_min);
590-
font->draw(narrow_to_wide(buf).c_str(),
590+
font->draw(utf8_to_wide(buf).c_str(),
591591
core::rect<s32>(textx, y - texth,
592592
textx2, y),
593593
meta.color);
594-
font->draw(narrow_to_wide(id).c_str(),
594+
font->draw(utf8_to_wide(id).c_str(),
595595
core::rect<s32>(textx, y - graphh / 2 - texth / 2,
596596
textx2, y - graphh / 2 + texth / 2),
597597
meta.color);
@@ -1049,7 +1049,7 @@ static void show_chat_menu(GUIFormSpecMenu **cur_formspec,
10491049
FORMSPEC_VERSION_STRING
10501050
SIZE_TAG
10511051
"field[3,2.35;6,0.5;f_text;;" + text + "]"
1052-
"button_exit[4,3;3,0.5;btn_send;" + wide_to_narrow(wstrgettext("Proceed")) + "]"
1052+
"button_exit[4,3;3,0.5;btn_send;" + wide_to_utf8(wstrgettext("Proceed")) + "]"
10531053
;
10541054

10551055
/* Create menu */
@@ -1089,7 +1089,7 @@ static void show_pause_menu(GUIFormSpecMenu **cur_formspec,
10891089
bool singleplayermode)
10901090
{
10911091
#ifdef __ANDROID__
1092-
std::string control_text = wide_to_narrow(wstrgettext("Default Controls:\n"
1092+
std::string control_text = wide_to_utf8(wstrgettext("Default Controls:\n"
10931093
"No menu visible:\n"
10941094
"- single tap: button activate\n"
10951095
"- double tap: place/use\n"
@@ -1103,7 +1103,7 @@ static void show_pause_menu(GUIFormSpecMenu **cur_formspec,
11031103
" --> place single item to slot\n"
11041104
));
11051105
#else
1106-
std::string control_text = wide_to_narrow(wstrgettext("Default Controls:\n"
1106+
std::string control_text = wide_to_utf8(wstrgettext("Default Controls:\n"
11071107
"- WASD: move\n"
11081108
"- Space: jump/climb\n"
11091109
"- Shift: sneak/go down\n"
@@ -1122,23 +1122,23 @@ static void show_pause_menu(GUIFormSpecMenu **cur_formspec,
11221122

11231123
os << FORMSPEC_VERSION_STRING << SIZE_TAG
11241124
<< "button_exit[4," << (ypos++) << ";3,0.5;btn_continue;"
1125-
<< wide_to_narrow(wstrgettext("Continue")) << "]";
1125+
<< wide_to_utf8(wstrgettext("Continue")) << "]";
11261126

11271127
if (!singleplayermode) {
11281128
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_change_password;"
1129-
<< wide_to_narrow(wstrgettext("Change Password")) << "]";
1129+
<< wide_to_utf8(wstrgettext("Change Password")) << "]";
11301130
}
11311131

11321132
#ifndef __ANDROID__
11331133
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_sound;"
1134-
<< wide_to_narrow(wstrgettext("Sound Volume")) << "]";
1134+
<< wide_to_utf8(wstrgettext("Sound Volume")) << "]";
11351135
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_key_config;"
1136-
<< wide_to_narrow(wstrgettext("Change Keys")) << "]";
1136+
<< wide_to_utf8(wstrgettext("Change Keys")) << "]";
11371137
#endif
11381138
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_menu;"
1139-
<< wide_to_narrow(wstrgettext("Exit to Menu")) << "]";
1139+
<< wide_to_utf8(wstrgettext("Exit to Menu")) << "]";
11401140
os << "button_exit[4," << (ypos++) << ";3,0.5;btn_exit_os;"
1141-
<< wide_to_narrow(wstrgettext("Exit to OS")) << "]"
1141+
<< wide_to_utf8(wstrgettext("Exit to OS")) << "]"
11421142
<< "textarea[7.5,0.25;3.9,6.25;;" << control_text << ";]"
11431143
<< "textarea[0.4,0.25;3.5,6;;" << PROJECT_NAME_C "\n"
11441144
<< g_build_info << "\n"
@@ -1167,7 +1167,7 @@ static void updateChat(Client &client, f32 dtime, bool show_debug,
11671167

11681168
// Get new messages from error log buffer
11691169
while (!chat_log_error_buf.empty()) {
1170-
chat_backend.addMessage(L"", narrow_to_wide(chat_log_error_buf.get()));
1170+
chat_backend.addMessage(L"", utf8_to_wide(chat_log_error_buf.get()));
11711171
}
11721172

11731173
// Get new messages from client
@@ -2059,7 +2059,7 @@ bool Game::createClient(const std::string &playername,
20592059

20602060
/* Set window caption
20612061
*/
2062-
std::wstring str = narrow_to_wide(PROJECT_NAME_C);
2062+
std::wstring str = utf8_to_wide(PROJECT_NAME_C);
20632063
str += L" [";
20642064
str += driver->getName();
20652065
str += L"]";
@@ -2087,7 +2087,7 @@ bool Game::initGui()
20872087
{
20882088
// First line of debug text
20892089
guitext = guienv->addStaticText(
2090-
narrow_to_wide(PROJECT_NAME_C).c_str(),
2090+
utf8_to_wide(PROJECT_NAME_C).c_str(),
20912091
core::rect<s32>(0, 0, 0, 0),
20922092
false, false, guiroot);
20932093

@@ -2342,7 +2342,7 @@ bool Game::getServerContent(bool *aborted)
23422342
}
23432343

23442344
progress = 30 + client->mediaReceiveProgress() * 35 + 0.5;
2345-
draw_load_screen(narrow_to_wide(message.str()), device,
2345+
draw_load_screen(utf8_to_wide(message.str()), device,
23462346
guienv, dtime, progress);
23472347
}
23482348
}
@@ -2660,7 +2660,7 @@ void Game::processKeyboardInput(VolatileRunFlags *flags,
26602660

26612661
if (quicktune->hasMessage()) {
26622662
std::string msg = quicktune->getMessage();
2663-
statustext = narrow_to_wide(msg);
2663+
statustext = utf8_to_wide(msg);
26642664
*statustext_time = 0;
26652665
}
26662666
}
@@ -2947,7 +2947,7 @@ void Game::increaseViewRange(float *statustext_time)
29472947
s16 range = g_settings->getS16("viewing_range_nodes_min");
29482948
s16 range_new = range + 10;
29492949
g_settings->set("viewing_range_nodes_min", itos(range_new));
2950-
statustext = narrow_to_wide("Minimum viewing range changed to "
2950+
statustext = utf8_to_wide("Minimum viewing range changed to "
29512951
+ itos(range_new));
29522952
*statustext_time = 0;
29532953
}
@@ -2962,7 +2962,7 @@ void Game::decreaseViewRange(float *statustext_time)
29622962
range_new = range;
29632963

29642964
g_settings->set("viewing_range_nodes_min", itos(range_new));
2965-
statustext = narrow_to_wide("Minimum viewing range changed to "
2965+
statustext = utf8_to_wide("Minimum viewing range changed to "
29662966
+ itos(range_new));
29672967
*statustext_time = 0;
29682968
}
@@ -3577,13 +3577,13 @@ void Game::handlePointingAtNode(GameRunData *runData,
35773577
NodeMetadata *meta = map.getNodeMetadata(nodepos);
35783578

35793579
if (meta) {
3580-
infotext = narrow_to_wide(meta->getString("infotext"));
3580+
infotext = utf8_to_wide(meta->getString("infotext"));
35813581
} else {
35823582
MapNode n = map.getNodeNoEx(nodepos);
35833583

35843584
if (nodedef_manager->get(n).tiledef[0].name == "unknown_node.png") {
35853585
infotext = L"Unknown node: ";
3586-
infotext += narrow_to_wide(nodedef_manager->get(n).name);
3586+
infotext += utf8_to_wide(nodedef_manager->get(n).name);
35873587
}
35883588
}
35893589

@@ -3649,10 +3649,10 @@ void Game::handlePointingAtObject(GameRunData *runData,
36493649
const v3f &player_position,
36503650
bool show_debug)
36513651
{
3652-
infotext = narrow_to_wide(runData->selected_object->infoText());
3652+
infotext = utf8_to_wide(runData->selected_object->infoText());
36533653

36543654
if (infotext == L"" && show_debug) {
3655-
infotext = narrow_to_wide(runData->selected_object->debugInfoText());
3655+
infotext = utf8_to_wide(runData->selected_object->debugInfoText());
36563656
}
36573657

36583658
if (input->getLeftState()) {
@@ -4117,12 +4117,12 @@ void Game::updateGui(float *statustext_time, const RunStats &stats,
41174117
<< ", v_range = " << draw_control->wanted_range
41184118
<< std::setprecision(3)
41194119
<< ", RTT = " << client->getRTT();
4120-
guitext->setText(narrow_to_wide(os.str()).c_str());
4120+
guitext->setText(utf8_to_wide(os.str()).c_str());
41214121
guitext->setVisible(true);
41224122
} else if (flags.show_hud || flags.show_chat) {
41234123
std::ostringstream os(std::ios_base::binary);
41244124
os << PROJECT_NAME_C " " << g_version_hash;
4125-
guitext->setText(narrow_to_wide(os.str()).c_str());
4125+
guitext->setText(utf8_to_wide(os.str()).c_str());
41264126
guitext->setVisible(true);
41274127
} else {
41284128
guitext->setVisible(false);
@@ -4159,7 +4159,7 @@ void Game::updateGui(float *statustext_time, const RunStats &stats,
41594159
}
41604160
}
41614161

4162-
guitext2->setText(narrow_to_wide(os.str()).c_str());
4162+
guitext2->setText(utf8_to_wide(os.str()).c_str());
41634163
guitext2->setVisible(true);
41644164

41654165
core::rect<s32> rect(

Diff for: ‎src/gettext.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ const char* MSVC_LocaleLookup(const char* raw_shortname) {
102102

103103
last_raw_value = shortname;
104104

105-
if (glb_supported_locales.find(narrow_to_wide(shortname)) != glb_supported_locales.end()) {
106-
last_full_name = wide_to_narrow(glb_supported_locales[narrow_to_wide(shortname)]);
105+
if (glb_supported_locales.find(utf8_to_wide(shortname)) != glb_supported_locales.end()) {
106+
last_full_name = wide_to_utf8(
107+
glb_supported_locales[utf8_to_wide(shortname)]);
107108
return last_full_name.c_str();
108109
}
109110

Diff for: ‎src/gettext.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ void init_gettext(const char *path, const std::string &configured_language,
3939
void init_gettext(const char *path, const std::string &configured_language);
4040
#endif
4141

42-
extern wchar_t *narrow_to_wide_c(const char *str);
42+
extern wchar_t *utf8_to_wide_c(const char *str);
4343

4444
// You must free the returned string!
4545
// The returned string is allocated using new
4646
inline const wchar_t *wgettext(const char *str)
4747
{
48-
return narrow_to_wide_c(gettext(str));
48+
return utf8_to_wide_c(gettext(str));
4949
}
5050

5151
inline std::wstring wstrgettext(const std::string &text)

Diff for: ‎src/guiEngine.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ void TextDestGuiEngine::gotText(const StringMap &fields)
6161
/******************************************************************************/
6262
void TextDestGuiEngine::gotText(std::wstring text)
6363
{
64-
m_engine->getScriptIface()->handleMainMenuEvent(wide_to_narrow(text));
64+
m_engine->getScriptIface()->handleMainMenuEvent(wide_to_utf8(text));
6565
}
6666

6767
/******************************************************************************/
@@ -172,7 +172,7 @@ GUIEngine::GUIEngine( irr::IrrlichtDevice* dev,
172172
m_sound_manager = &dummySoundManager;
173173

174174
//create topleft header
175-
std::wstring t = narrow_to_wide(std::string(PROJECT_NAME_C " ") +
175+
std::wstring t = utf8_to_wide(std::string(PROJECT_NAME_C " ") +
176176
g_version_hash);
177177

178178
core::rect<s32> rect(0, 0, g_fontengine->getTextWidth(t), g_fontengine->getTextHeight());
@@ -572,13 +572,13 @@ bool GUIEngine::downloadFile(std::string url, std::string target)
572572
/******************************************************************************/
573573
void GUIEngine::setTopleftText(std::string append)
574574
{
575-
std::wstring toset = narrow_to_wide(std::string(PROJECT_NAME_C " ") +
576-
g_version_hash);
575+
std::wstring toset = utf8_to_wide(std::string(PROJECT_NAME_C " ") +
576+
g_version_hash);
577577

578578
if (append != "")
579579
{
580580
toset += L" / ";
581-
toset += narrow_to_wide(append);
581+
toset += utf8_to_wide(append);
582582
}
583583

584584
m_irr_toplefttext->setText(toset.c_str());

Diff for: ‎src/guiFileSelectMenu.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ GUIFileSelectMenu::GUIFileSelectMenu(gui::IGUIEnvironment* env,
2626
std::string title, std::string formname) :
2727
GUIModalMenu(env, parent, id, menumgr)
2828
{
29-
m_title = narrow_to_wide(title);
29+
m_title = utf8_to_wide(title);
3030
m_parent = parent;
3131
m_formname = formname;
3232
m_text_dst = 0;
@@ -87,7 +87,7 @@ void GUIFileSelectMenu::acceptInput() {
8787
StringMap fields;
8888

8989
if (m_accepted)
90-
fields[m_formname + "_accepted"] = wide_to_narrow(m_fileOpenDialog->getFileName());
90+
fields[m_formname + "_accepted"] = wide_to_utf8(m_fileOpenDialog->getFileName());
9191
else
9292
fields[m_formname + "_canceled"] = m_formname;
9393

Diff for: ‎src/guiPasswordChange.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,8 @@ bool GUIPasswordChange::acceptInput()
203203
e->setVisible(true);
204204
return false;
205205
}
206-
m_client->sendChangePassword(wide_to_narrow(oldpass),
207-
wide_to_narrow(newpass));
206+
m_client->sendChangePassword(wide_to_utf8(oldpass),
207+
wide_to_utf8(newpass));
208208
return true;
209209
}
210210

Diff for: ‎src/guiTable.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ s32 GUITable::allocString(const std::string &text)
929929
std::map<std::string, s32>::iterator it = m_alloc_strings.find(text);
930930
if (it == m_alloc_strings.end()) {
931931
s32 id = m_strings.size();
932-
std::wstring wtext = narrow_to_wide(text);
932+
std::wstring wtext = utf8_to_wide(text);
933933
m_strings.push_back(core::stringw(wtext.c_str()));
934934
m_alloc_strings.insert(std::make_pair(text, id));
935935
return id;

Diff for: ‎src/hud.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ void Hud::drawLuaElements(v3s16 camera_offset) {
275275
(e->number >> 8) & 0xFF,
276276
(e->number >> 0) & 0xFF);
277277
core::rect<s32> size(0, 0, e->scale.X, text_height * e->scale.Y);
278-
std::wstring text = narrow_to_wide(e->text);
278+
std::wstring text = utf8_to_wide(e->text);
279279
core::dimension2d<u32> textsize = font->getDimension(text.c_str());
280280
v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
281281
(e->align.Y - 1.0) * (textsize.Height / 2));
@@ -310,11 +310,11 @@ void Hud::drawLuaElements(v3s16 camera_offset) {
310310
(e->number >> 8) & 0xFF,
311311
(e->number >> 0) & 0xFF);
312312
core::rect<s32> size(0, 0, 200, 2 * text_height);
313-
std::wstring text = narrow_to_wide(e->name);
313+
std::wstring text = utf8_to_wide(e->name);
314314
font->draw(text.c_str(), size + pos, color);
315315
std::ostringstream os;
316-
os<<distance<<e->text;
317-
text = narrow_to_wide(os.str());
316+
os << distance << e->text;
317+
text = utf8_to_wide(os.str());
318318
pos.Y += text_height;
319319
font->draw(text.c_str(), size + pos, color);
320320
break; }
@@ -552,7 +552,7 @@ void drawItemStack(video::IVideoDriver *driver,
552552
{
553553
// Get the item count as a string
554554
std::string text = itos(item.count);
555-
v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
555+
v2u32 dim = font->getDimension(utf8_to_wide(text).c_str());
556556
v2s32 sdim(dim.X,dim.Y);
557557

558558
core::rect<s32> rect2(

Diff for: ‎src/network/clientpackethandler.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ void Client::handleCommand_AccessDenied(NetworkPacket* pkt)
228228
if (pkt->getSize() >= 2) {
229229
std::wstring wide_reason;
230230
*pkt >> wide_reason;
231-
m_access_denied_reason = wide_to_narrow(wide_reason);
231+
m_access_denied_reason = wide_to_utf8(wide_reason);
232232
}
233233
}
234234
}

0 commit comments

Comments
 (0)
Please sign in to comment.