Skip to content

Commit

Permalink
Android: fix handling non-latin characters on older Android devices (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
Maksim committed Apr 17, 2020
1 parent 4e2473e commit 23c6d0c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/server.cpp
Expand Up @@ -3031,8 +3031,16 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
line += L"-!- You don't have permission to shout.";
broadcast_line = false;
} else {
/*
Workaround for fixing chat on Android. Lua doesn't handle
the Cyrillic alphabet and some characters on older Android devices
*/
#ifdef __ANDROID__
line += L"<" + wname + L"> " + wmessage;
#else
line += narrow_to_wide(m_script->formatChatMessage(name,
wide_to_narrow(wmessage)));
#endif
}

/*
Expand Down
8 changes: 8 additions & 0 deletions src/util/string.cpp
Expand Up @@ -209,18 +209,25 @@ wchar_t *narrow_to_wide_c(const char *str)
}

std::wstring narrow_to_wide(const std::string &mbs) {
#ifdef __ANDROID__
return utf8_to_wide(mbs);
#else
size_t wcl = mbs.size();
Buffer<wchar_t> wcs(wcl + 1);
size_t len = mbstowcs(*wcs, mbs.c_str(), wcl);
if (len == (size_t)(-1))
return L"<invalid multibyte string>";
wcs[len] = 0;
return *wcs;
#endif
}


std::string wide_to_narrow(const std::wstring &wcs)
{
#ifdef __ANDROID__
return wide_to_utf8(wcs);
#else
size_t mbl = wcs.size() * 4;
SharedBuffer<char> mbs(mbl+1);
size_t len = wcstombs(*mbs, wcs.c_str(), mbl);
Expand All @@ -229,6 +236,7 @@ std::string wide_to_narrow(const std::wstring &wcs)

mbs[len] = 0;
return *mbs;
#endif
}


Expand Down

0 comments on commit 23c6d0c

Please sign in to comment.