diff options
author | Treeston <treeston.mmoc@gmail.com> | 2019-08-03 22:34:46 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2021-12-18 11:25:59 +0100 |
commit | 87c2d247d9acbde4e3cad74bb987e9ebcc9c1d13 (patch) | |
tree | 52d1d44e35dfb48d0417296af216fe6c2628aaad /src | |
parent | da8d6b5180a586f2cbf8ea4e859a1d5336ea8336 (diff) |
Core/Misc: Rewrite some old name handling functions that use raw buffers to no longer be old name handling functions that use raw buffers.
(cherry picked from commit 309851ea2442457c87f791bb44abfa89e04af1af)
Diffstat (limited to 'src')
-rw-r--r-- | src/common/Utilities/Util.cpp | 68 | ||||
-rw-r--r-- | src/server/game/Globals/ObjectMgr.cpp | 12 |
2 files changed, 40 insertions, 40 deletions
diff --git a/src/common/Utilities/Util.cpp b/src/common/Utilities/Util.cpp index 4b8c206c9ff..fab84bb5fbd 100644 --- a/src/common/Utilities/Util.cpp +++ b/src/common/Utilities/Util.cpp @@ -572,43 +572,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 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 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) + 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 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 } + }}; + + 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; diff --git a/src/server/game/Globals/ObjectMgr.cpp b/src/server/game/Globals/ObjectMgr.cpp index df97c30ff11..6ef8fe4ce09 100644 --- a/src/server/game/Globals/ObjectMgr.cpp +++ b/src/server/game/Globals/ObjectMgr.cpp @@ -156,17 +156,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; |