Skip to content

Commit c834d2a

Browse files
committedFeb 2, 2021
Drop wide/narrow conversion functions
The only valid usecase for these is interfacing with OS APIs that want a locale/OS-specific multibyte encoding. But they weren't used for that anywhere, instead UTF-8 is pretty much assumed when it comes to that. Since these are only a potential source of bugs and do not fulfil their purpose at all, drop them entirely.
1 parent 5e392cf commit c834d2a

File tree

11 files changed

+41
-143
lines changed

11 files changed

+41
-143
lines changed
 

‎src/chat.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -485,8 +485,8 @@ void ChatPrompt::nickCompletion(const std::list<std::string>& names, bool backwa
485485
// find all names that start with the selected prefix
486486
std::vector<std::wstring> completions;
487487
for (const std::string &name : names) {
488-
if (str_starts_with(narrow_to_wide(name), prefix, true)) {
489-
std::wstring completion = narrow_to_wide(name);
488+
std::wstring completion = utf8_to_wide(name);
489+
if (str_starts_with(completion, prefix, true)) {
490490
if (prefix_start == 0)
491491
completion += L": ";
492492
completions.push_back(completion);

‎src/client/client.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ void Client::sendChatMessage(const std::wstring &message)
11961196
if (canSendChatMessage()) {
11971197
u32 now = time(NULL);
11981198
float time_passed = now - m_last_chat_message_sent;
1199-
m_last_chat_message_sent = time(NULL);
1199+
m_last_chat_message_sent = now;
12001200

12011201
m_chat_message_allowance += time_passed * (CLIENT_CHAT_MESSAGE_LIMIT_PER_10S / 8.0f);
12021202
if (m_chat_message_allowance > CLIENT_CHAT_MESSAGE_LIMIT_PER_10S)
@@ -1832,7 +1832,7 @@ void Client::makeScreenshot()
18321832
sstr << "Failed to save screenshot '" << filename << "'";
18331833
}
18341834
pushToChatQueue(new ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
1835-
narrow_to_wide(sstr.str())));
1835+
utf8_to_wide(sstr.str())));
18361836
infostream << sstr.str() << std::endl;
18371837
image->drop();
18381838
}

‎src/client/keycode.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,8 @@ KeyPress::KeyPress(const char *name)
316316
int chars_read = mbtowc(&Char, name, 1);
317317
FATAL_ERROR_IF(chars_read != 1, "Unexpected multibyte character");
318318
m_name = "";
319-
warningstream << "KeyPress: Unknown key '" << name << "', falling back to first char.";
319+
warningstream << "KeyPress: Unknown key '" << name
320+
<< "', falling back to first char." << std::endl;
320321
}
321322

322323
KeyPress::KeyPress(const irr::SEvent::SKeyInput &in, bool prefer_character)

‎src/gui/guiConfirmRegistration.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,7 @@ void GUIConfirmRegistration::acceptInput()
192192

193193
bool GUIConfirmRegistration::processInput()
194194
{
195-
std::wstring m_password_ws = narrow_to_wide(m_password);
196-
if (m_password_ws != m_pass_confirm) {
195+
if (utf8_to_wide(m_password) != m_pass_confirm) {
197196
gui::IGUIElement *e = getElementFromId(ID_message);
198197
if (e)
199198
e->setVisible(true);

‎src/network/serverpackethandler.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -778,15 +778,13 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)
778778
return;
779779
}
780780

781-
// Get player name of this client
782781
std::string name = player->getName();
783-
std::wstring wname = narrow_to_wide(name);
784782

785-
std::wstring answer_to_sender = handleChat(name, wname, message, true, player);
783+
std::wstring answer_to_sender = handleChat(name, message, true, player);
786784
if (!answer_to_sender.empty()) {
787785
// Send the answer to sender
788-
SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_NORMAL,
789-
answer_to_sender, wname));
786+
SendChatMessage(peer_id, ChatMessage(CHATMESSAGE_TYPE_SYSTEM,
787+
answer_to_sender));
790788
}
791789
}
792790

‎src/script/lua_api/l_server.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ int ModApiServer::l_request_shutdown(lua_State *L)
4444
int ModApiServer::l_get_server_status(lua_State *L)
4545
{
4646
NO_MAP_LOCK_REQUIRED;
47-
lua_pushstring(L, wide_to_narrow(getServer(L)->getStatusString()).c_str());
47+
lua_pushstring(L, getServer(L)->getStatusString().c_str());
4848
return 1;
4949
}
5050

‎src/server.cpp

+23-38
Original file line numberDiff line numberDiff line change
@@ -2955,7 +2955,7 @@ void Server::handleChatInterfaceEvent(ChatEvent *evt)
29552955
}
29562956
}
29572957

2958-
std::wstring Server::handleChat(const std::string &name, const std::wstring &wname,
2958+
std::wstring Server::handleChat(const std::string &name,
29592959
std::wstring wmessage, bool check_shout_priv, RemotePlayer *player)
29602960
{
29612961
// If something goes wrong, this player is to blame
@@ -2993,7 +2993,7 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
29932993

29942994
auto message = trim(wide_to_utf8(wmessage));
29952995
if (message.find_first_of("\n\r") != std::wstring::npos) {
2996-
return L"New lines are not permitted in chat messages";
2996+
return L"Newlines are not permitted in chat messages";
29972997
}
29982998

29992999
// Run script hook, exit if script ate the chat message
@@ -3014,10 +3014,10 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
30143014
the Cyrillic alphabet and some characters on older Android devices
30153015
*/
30163016
#ifdef __ANDROID__
3017-
line += L"<" + wname + L"> " + wmessage;
3017+
line += L"<" + utf8_to_wide(name) + L"> " + wmessage;
30183018
#else
3019-
line += narrow_to_wide(m_script->formatChatMessage(name,
3020-
wide_to_narrow(wmessage)));
3019+
line += utf8_to_wide(m_script->formatChatMessage(name,
3020+
wide_to_utf8(wmessage)));
30213021
#endif
30223022
}
30233023

@@ -3030,35 +3030,23 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
30303030
/*
30313031
Send the message to others
30323032
*/
3033-
actionstream << "CHAT: " << wide_to_narrow(unescape_enriched(line)) << std::endl;
3033+
actionstream << "CHAT: " << wide_to_utf8(unescape_enriched(line)) << std::endl;
30343034

3035-
std::vector<session_t> clients = m_clients.getClientIDs();
3036-
3037-
/*
3038-
Send the message back to the inital sender
3039-
if they are using protocol version >= 29
3040-
*/
3041-
3042-
session_t peer_id_to_avoid_sending =
3043-
(player ? player->getPeerId() : PEER_ID_INEXISTENT);
3035+
ChatMessage chatmsg(line);
30443036

3045-
if (player && player->protocol_version >= 29)
3046-
peer_id_to_avoid_sending = PEER_ID_INEXISTENT;
3037+
std::vector<session_t> clients = m_clients.getClientIDs();
3038+
for (u16 cid : clients)
3039+
SendChatMessage(cid, chatmsg);
30473040

3048-
for (u16 cid : clients) {
3049-
if (cid != peer_id_to_avoid_sending)
3050-
SendChatMessage(cid, ChatMessage(line));
3051-
}
30523041
return L"";
30533042
}
30543043

30553044
void Server::handleAdminChat(const ChatEventChat *evt)
30563045
{
30573046
std::string name = evt->nick;
3058-
std::wstring wname = utf8_to_wide(name);
30593047
std::wstring wmessage = evt->evt_msg;
30603048

3061-
std::wstring answer = handleChat(name, wname, wmessage);
3049+
std::wstring answer = handleChat(name, wmessage);
30623050

30633051
// If asked to send answer to sender
30643052
if (!answer.empty()) {
@@ -3095,46 +3083,43 @@ PlayerSAO *Server::getPlayerSAO(session_t peer_id)
30953083
return player->getPlayerSAO();
30963084
}
30973085

3098-
std::wstring Server::getStatusString()
3086+
std::string Server::getStatusString()
30993087
{
3100-
std::wostringstream os(std::ios_base::binary);
3101-
os << L"# Server: ";
3088+
std::ostringstream os(std::ios_base::binary);
3089+
os << "# Server: ";
31023090
// Version
3103-
os << L"version=" << narrow_to_wide(g_version_string);
3091+
os << "version=" << g_version_string;
31043092
// Uptime
3105-
os << L", uptime=" << m_uptime_counter->get();
3093+
os << ", uptime=" << m_uptime_counter->get();
31063094
// Max lag estimate
3107-
os << L", max_lag=" << (m_env ? m_env->getMaxLagEstimate() : 0);
3095+
os << ", max_lag=" << (m_env ? m_env->getMaxLagEstimate() : 0);
31083096

31093097
// Information about clients
31103098
bool first = true;
3111-
os << L", clients={";
3099+
os << ", clients={";
31123100
if (m_env) {
31133101
std::vector<session_t> clients = m_clients.getClientIDs();
31143102
for (session_t client_id : clients) {
31153103
RemotePlayer *player = m_env->getPlayer(client_id);
31163104

31173105
// Get name of player
3118-
std::wstring name = L"unknown";
3119-
if (player)
3120-
name = narrow_to_wide(player->getName());
3106+
const char *name = player ? player->getName() : "<unknown>";
31213107

31223108
// Add name to information string
31233109
if (!first)
3124-
os << L", ";
3110+
os << ", ";
31253111
else
31263112
first = false;
3127-
31283113
os << name;
31293114
}
31303115
}
3131-
os << L"}";
3116+
os << "}";
31323117

31333118
if (m_env && !((ServerMap*)(&m_env->getMap()))->isSavingEnabled())
3134-
os << std::endl << L"# Server: " << " WARNING: Map saving is disabled.";
3119+
os << std::endl << "# Server: " << " WARNING: Map saving is disabled.";
31353120

31363121
if (!g_settings->get("motd").empty())
3137-
os << std::endl << L"# Server: " << narrow_to_wide(g_settings->get("motd"));
3122+
os << std::endl << "# Server: " << g_settings->get("motd");
31383123

31393124
return os.str();
31403125
}

‎src/server.h

+3-5
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
219219
void onMapEditEvent(const MapEditEvent &event);
220220

221221
// Connection must be locked when called
222-
std::wstring getStatusString();
222+
std::string getStatusString();
223223
inline double getUptime() const { return m_uptime_counter->get(); }
224224

225225
// read shutdown state
@@ -495,10 +495,8 @@ class Server : public con::PeerHandler, public MapEventReceiver,
495495
void handleChatInterfaceEvent(ChatEvent *evt);
496496

497497
// This returns the answer to the sender of wmessage, or "" if there is none
498-
std::wstring handleChat(const std::string &name, const std::wstring &wname,
499-
std::wstring wmessage_input,
500-
bool check_shout_priv = false,
501-
RemotePlayer *player = NULL);
498+
std::wstring handleChat(const std::string &name, std::wstring wmessage_input,
499+
bool check_shout_priv = false, RemotePlayer *player = nullptr);
502500
void handleAdminChat(const ChatEventChat *evt);
503501

504502
// When called, connection mutex should be locked

‎src/unittest/test_utilities.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ void TestUtilities::testStartsWith()
247247

248248
void TestUtilities::testStrEqual()
249249
{
250-
UASSERT(str_equal(narrow_to_wide("abc"), narrow_to_wide("abc")));
251-
UASSERT(str_equal(narrow_to_wide("ABC"), narrow_to_wide("abc"), true));
250+
UASSERT(str_equal(utf8_to_wide("abc"), utf8_to_wide("abc")));
251+
UASSERT(str_equal(utf8_to_wide("ABC"), utf8_to_wide("abc"), true));
252252
}
253253

254254

‎src/util/string.cpp

+2-57
Original file line numberDiff line numberDiff line change
@@ -175,62 +175,6 @@ wchar_t *utf8_to_wide_c(const char *str)
175175
return ret_c;
176176
}
177177

178-
// You must free the returned string!
179-
// The returned string is allocated using new
180-
wchar_t *narrow_to_wide_c(const char *str)
181-
{
182-
wchar_t *nstr = nullptr;
183-
#if defined(_WIN32)
184-
int nResult = MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, 0, 0);
185-
if (nResult == 0) {
186-
errorstream<<"gettext: MultiByteToWideChar returned null"<<std::endl;
187-
} else {
188-
nstr = new wchar_t[nResult];
189-
MultiByteToWideChar(CP_UTF8, 0, (LPCSTR) str, -1, (WCHAR *) nstr, nResult);
190-
}
191-
#else
192-
size_t len = strlen(str);
193-
nstr = new wchar_t[len + 1];
194-
195-
std::wstring intermediate = narrow_to_wide(str);
196-
memset(nstr, 0, (len + 1) * sizeof(wchar_t));
197-
memcpy(nstr, intermediate.c_str(), len * sizeof(wchar_t));
198-
#endif
199-
200-
return nstr;
201-
}
202-
203-
std::wstring narrow_to_wide(const std::string &mbs) {
204-
#ifdef __ANDROID__
205-
return utf8_to_wide(mbs);
206-
#else
207-
size_t wcl = mbs.size();
208-
Buffer<wchar_t> wcs(wcl + 1);
209-
size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
210-
if (len == (size_t)(-1))
211-
return L"<invalid multibyte string>";
212-
wcs[len] = 0;
213-
return *wcs;
214-
#endif
215-
}
216-
217-
218-
std::string wide_to_narrow(const std::wstring &wcs)
219-
{
220-
#ifdef __ANDROID__
221-
return wide_to_utf8(wcs);
222-
#else
223-
size_t mbl = wcs.size() * 4;
224-
SharedBuffer<char> mbs(mbl+1);
225-
size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
226-
if (len == (size_t)(-1))
227-
return "Character conversion failed!";
228-
229-
mbs[len] = 0;
230-
return *mbs;
231-
#endif
232-
}
233-
234178

235179
std::string urlencode(const std::string &str)
236180
{
@@ -757,7 +701,8 @@ void translate_string(const std::wstring &s, Translations *translations,
757701
} else {
758702
// This is an escape sequence *inside* the template string to translate itself.
759703
// This should not happen, show an error message.
760-
errorstream << "Ignoring escape sequence '" << wide_to_narrow(escape_sequence) << "' in translation" << std::endl;
704+
errorstream << "Ignoring escape sequence '"
705+
<< wide_to_utf8(escape_sequence) << "' in translation" << std::endl;
761706
}
762707
}
763708

‎src/util/string.h

-28
Original file line numberDiff line numberDiff line change
@@ -73,16 +73,6 @@ std::string wide_to_utf8(const std::wstring &input);
7373
// The returned string is allocated using new[]
7474
wchar_t *utf8_to_wide_c(const char *str);
7575

76-
// NEVER use those two functions unless you have a VERY GOOD reason to
77-
// they just convert between wide and multibyte encoding
78-
// multibyte encoding depends on current locale, this is no good, especially on Windows
79-
80-
// You must free the returned string!
81-
// The returned string is allocated using new
82-
wchar_t *narrow_to_wide_c(const char *str);
83-
std::wstring narrow_to_wide(const std::string &mbs);
84-
std::string wide_to_narrow(const std::wstring &wcs);
85-
8676
std::string urlencode(const std::string &str);
8777
std::string urldecode(const std::string &str);
8878
u32 readFlagString(std::string str, const FlagDesc *flagdesc, u32 *flagmask);
@@ -355,11 +345,6 @@ inline s32 mystoi(const std::string &str, s32 min, s32 max)
355345
return i;
356346
}
357347

358-
359-
// MSVC2010 includes it's own versions of these
360-
//#if !defined(_MSC_VER) || _MSC_VER < 1600
361-
362-
363348
/**
364349
* Returns a 32-bit value reprensented by the string \p str (decimal).
365350
* @see atoi(3) for further limitations
@@ -369,17 +354,6 @@ inline s32 mystoi(const std::string &str)
369354
return atoi(str.c_str());
370355
}
371356

372-
373-
/**
374-
* Returns s 32-bit value represented by the wide string \p str (decimal).
375-
* @see atoi(3) for further limitations
376-
*/
377-
inline s32 mystoi(const std::wstring &str)
378-
{
379-
return mystoi(wide_to_narrow(str));
380-
}
381-
382-
383357
/**
384358
* Returns a float reprensented by the string \p str (decimal).
385359
* @see atof(3)
@@ -389,8 +363,6 @@ inline float mystof(const std::string &str)
389363
return atof(str.c_str());
390364
}
391365

392-
//#endif
393-
394366
#define stoi mystoi
395367
#define stof mystof
396368

0 commit comments

Comments
 (0)
Please sign in to comment.