Core/Misc: Rewrite some old name handling functions that use raw buffers to no longer be old name handling functions that use raw buffers.

This commit is contained in:
Treeston
2019-08-03 22:34:46 +02:00
parent ed54e24b69
commit 309851ea24
2 changed files with 38 additions and 38 deletions

View File

@@ -391,43 +391,47 @@ void wstrToLower(std::wstring& str)
std::wstring GetMainPartOfName(std::wstring const& wname, uint32 declension)
{
// supported only Cyrillic cases
if (wname.size() < 1 || !isCyrillicCharacter(wname[0]) || declension > 5)
if (wname.empty() || !isCyrillicCharacter(wname[0]) || declension > 5)
return wname;
// Important: end length must be <= MAX_INTERNAL_PLAYER_NAME-MAX_PLAYER_NAME (3 currently)
static std::wstring const a_End = { wchar_t(0x0430), wchar_t(0x0000) };
static std::wstring const o_End = { wchar_t(0x043E), wchar_t(0x0000) };
static std::wstring const ya_End = { wchar_t(0x044F), wchar_t(0x0000) };
static std::wstring const ie_End = { wchar_t(0x0435), wchar_t(0x0000) };
static std::wstring const i_End = { wchar_t(0x0438), wchar_t(0x0000) };
static std::wstring const yeru_End = { wchar_t(0x044B), wchar_t(0x0000) };
static std::wstring const u_End = { wchar_t(0x0443), wchar_t(0x0000) };
static std::wstring const yu_End = { wchar_t(0x044E), wchar_t(0x0000) };
static std::wstring const oj_End = { wchar_t(0x043E), wchar_t(0x0439), wchar_t(0x0000) };
static std::wstring const ie_j_End = { wchar_t(0x0435), wchar_t(0x0439), wchar_t(0x0000) };
static std::wstring const io_j_End = { wchar_t(0x0451), wchar_t(0x0439), wchar_t(0x0000) };
static std::wstring const o_m_End = { wchar_t(0x043E), wchar_t(0x043C), wchar_t(0x0000) };
static std::wstring const io_m_End = { wchar_t(0x0451), wchar_t(0x043C), wchar_t(0x0000) };
static std::wstring const ie_m_End = { wchar_t(0x0435), wchar_t(0x043C), wchar_t(0x0000) };
static std::wstring const soft_End = { wchar_t(0x044C), wchar_t(0x0000) };
static std::wstring const j_End = { wchar_t(0x0439), wchar_t(0x0000) };
static wchar_t const a_End[] = { wchar_t(1), wchar_t(0x0430), wchar_t(0x0000)};
static wchar_t const o_End[] = { wchar_t(1), wchar_t(0x043E), wchar_t(0x0000)};
static wchar_t const ya_End[] = { wchar_t(1), wchar_t(0x044F), wchar_t(0x0000)};
static wchar_t const ie_End[] = { wchar_t(1), wchar_t(0x0435), wchar_t(0x0000)};
static wchar_t const i_End[] = { wchar_t(1), wchar_t(0x0438), wchar_t(0x0000)};
static wchar_t const yeru_End[] = { wchar_t(1), wchar_t(0x044B), wchar_t(0x0000)};
static wchar_t const u_End[] = { wchar_t(1), wchar_t(0x0443), wchar_t(0x0000)};
static wchar_t const yu_End[] = { wchar_t(1), wchar_t(0x044E), wchar_t(0x0000)};
static wchar_t const oj_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x0439), wchar_t(0x0000)};
static wchar_t const ie_j_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x0439), wchar_t(0x0000)};
static wchar_t const io_j_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x0439), wchar_t(0x0000)};
static wchar_t const o_m_End[] = { wchar_t(2), wchar_t(0x043E), wchar_t(0x043C), wchar_t(0x0000)};
static wchar_t const io_m_End[] = { wchar_t(2), wchar_t(0x0451), wchar_t(0x043C), wchar_t(0x0000)};
static wchar_t const ie_m_End[] = { wchar_t(2), wchar_t(0x0435), wchar_t(0x043C), wchar_t(0x0000)};
static wchar_t const soft_End[] = { wchar_t(1), wchar_t(0x044C), wchar_t(0x0000)};
static wchar_t const j_End[] = { wchar_t(1), wchar_t(0x0439), wchar_t(0x0000)};
static std::array<std::array<std::wstring const*, 7>, 6> const dropEnds = {{
{ &a_End, &o_End, &ya_End, &ie_End, &soft_End, &j_End, nullptr },
{ &a_End, &ya_End, &yeru_End, &i_End, nullptr, nullptr, nullptr },
{ &ie_End, &u_End, &yu_End, &i_End, nullptr, nullptr, nullptr },
{ &u_End, &yu_End, &o_End, &ie_End, &soft_End, &ya_End, &a_End },
{ &oj_End, &io_j_End, &ie_j_End, &o_m_End, &io_m_End, &ie_m_End, &yu_End },
{ &ie_End, &i_End, nullptr, nullptr, nullptr, nullptr, nullptr }
}};
static wchar_t const* const dropEnds[6][8] = {
{ &a_End[1], &o_End[1], &ya_End[1], &ie_End[1], &soft_End[1], &j_End[1], nullptr, nullptr },
{ &a_End[1], &ya_End[1], &yeru_End[1], &i_End[1], nullptr, nullptr, nullptr, nullptr },
{ &ie_End[1], &u_End[1], &yu_End[1], &i_End[1], nullptr, nullptr, nullptr, nullptr },
{ &u_End[1], &yu_End[1], &o_End[1], &ie_End[1], &soft_End[1], &ya_End[1], &a_End[1], nullptr },
{ &oj_End[1], &io_j_End[1], &ie_j_End[1], &o_m_End[1], &io_m_End[1], &ie_m_End[1], &yu_End[1], nullptr },
{ &ie_End[1], &i_End[1], nullptr, nullptr, nullptr, nullptr, nullptr, nullptr }
};
for (wchar_t const* const* itr = &dropEnds[declension][0]; *itr; ++itr)
std::size_t const thisLen = wname.length();
std::array<std::wstring const*, 7> const& endings = dropEnds[declension];
for (auto itr = endings.begin(), end = endings.end(); (itr != end) && *itr; ++itr)
{
size_t len = size_t((*itr)[-1]); // get length from string size field
std::wstring const& ending = **itr;
std::size_t const endLen = ending.length();
if (!(endLen <= thisLen))
continue;
if (wname.substr(wname.size()-len, len)==*itr)
return wname.substr(0, wname.size()-len);
if (wname.substr(thisLen-endLen, thisLen) == ending)
return wname.substr(0, thisLen-endLen);
}
return wname;

View File

@@ -143,17 +143,13 @@ bool normalizePlayerName(std::string& name)
if (name.empty())
return false;
wchar_t wstr_buf[MAX_INTERNAL_PLAYER_NAME+1];
size_t wstr_len = MAX_INTERNAL_PLAYER_NAME;
if (!Utf8toWStr(name, &wstr_buf[0], wstr_len))
std::wstring tmp;
if (!Utf8toWStr(name, tmp))
return false;
wstr_buf[0] = wcharToUpper(wstr_buf[0]);
for (size_t i = 1; i < wstr_len; ++i)
wstr_buf[i] = wcharToLower(wstr_buf[i]);
wstrToLower(tmp);
if (!WStrToUtf8(wstr_buf, wstr_len, name))
if (!WStrToUtf8(tmp, name))
return false;
return true;