aboutsummaryrefslogtreecommitdiff
path: root/src/common/Utilities/Util.cpp
diff options
context:
space:
mode:
authorTreeston <treeston.mmoc@gmail.com>2019-08-03 22:34:46 +0200
committerShauren <shauren.trinity@gmail.com>2021-12-18 11:25:59 +0100
commit87c2d247d9acbde4e3cad74bb987e9ebcc9c1d13 (patch)
tree52d1d44e35dfb48d0417296af216fe6c2628aaad /src/common/Utilities/Util.cpp
parentda8d6b5180a586f2cbf8ea4e859a1d5336ea8336 (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/common/Utilities/Util.cpp')
-rw-r--r--src/common/Utilities/Util.cpp68
1 files changed, 36 insertions, 32 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;