From c6e195a39963874d4ba0d838dea58676de4f28eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Mon, 23 May 2022 22:55:04 +0200 Subject: [PATCH] Use std::vector instead of array --- src/engine/server/name_ban.cpp | 17 ++++++----------- src/engine/server/name_ban.h | 4 ++-- src/engine/server/server.cpp | 28 +++++++++++++--------------- src/engine/server/server.h | 4 +--- src/test/name_ban.cpp | 31 +++++++++++++++++-------------- 5 files changed, 39 insertions(+), 45 deletions(-) diff --git a/src/engine/server/name_ban.cpp b/src/engine/server/name_ban.cpp index d5701c7f5..83c07875b 100644 --- a/src/engine/server/name_ban.cpp +++ b/src/engine/server/name_ban.cpp @@ -1,8 +1,6 @@ #include "name_ban.h" -#include // std::size - -CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans) +CNameBan *IsNameBanned(const char *pName, std::vector &vNameBans) { char aTrimmed[MAX_NAME_LENGTH]; str_copy(aTrimmed, str_utf8_skip_whitespaces(pName), sizeof(aTrimmed)); @@ -12,15 +10,12 @@ CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans) int SkeletonLength = str_utf8_to_skeleton(aTrimmed, aSkeleton, std::size(aSkeleton)); int aBuffer[MAX_NAME_SKELETON_LENGTH * 2 + 2]; - CNameBan *pResult = 0; - for(int i = 0; i < NumNameBans; i++) + CNameBan *pResult = nullptr; + for(CNameBan &Ban : vNameBans) { - CNameBan *pBan = &pNameBans[i]; - int Distance = str_utf32_dist_buffer(aSkeleton, SkeletonLength, pBan->m_aSkeleton, pBan->m_SkeletonLength, aBuffer, std::size(aBuffer)); - if(Distance <= pBan->m_Distance || (pBan->m_IsSubstring == 1 && str_utf8_find_nocase(pName, pBan->m_aName))) - { - pResult = pBan; - } + int Distance = str_utf32_dist_buffer(aSkeleton, SkeletonLength, Ban.m_aSkeleton, Ban.m_SkeletonLength, aBuffer, std::size(aBuffer)); + if(Distance <= Ban.m_Distance || (Ban.m_IsSubstring == 1 && str_utf8_find_nocase(pName, Ban.m_aName))) + pResult = &Ban; } return pResult; } diff --git a/src/engine/server/name_ban.h b/src/engine/server/name_ban.h index 399ff1f80..6e6bca901 100644 --- a/src/engine/server/name_ban.h +++ b/src/engine/server/name_ban.h @@ -4,7 +4,7 @@ #include #include -#include // std::size +#include enum { @@ -31,6 +31,6 @@ public: int m_IsSubstring; }; -CNameBan *IsNameBanned(const char *pName, CNameBan *pNameBans, int NumNameBans); +CNameBan *IsNameBanned(const char *pName, std::vector &vNameBans); #endif // ENGINE_SERVER_NAME_BAN_H diff --git a/src/engine/server/server.cpp b/src/engine/server/server.cpp index 65f521c96..6cd6f62f2 100644 --- a/src/engine/server/server.cpp +++ b/src/engine/server/server.cpp @@ -449,7 +449,7 @@ bool CServer::SetClientNameImpl(int ClientID, const char *pNameRequest, bool Set if(m_aClients[ClientID].m_State < CClient::STATE_READY) return false; - CNameBan *pBanned = IsNameBanned(pNameRequest, m_aNameBans.base_ptr(), m_aNameBans.size()); + CNameBan *pBanned = IsNameBanned(pNameRequest, m_vNameBans); if(pBanned) { if(m_aClients[ClientID].m_State == CClient::STATE_READY && Set) @@ -3237,21 +3237,20 @@ void CServer::ConNameBan(IConsole::IResult *pResult, void *pUser) int Distance = pResult->NumArguments() > 1 ? pResult->GetInteger(1) : str_length(pName) / 3; int IsSubstring = pResult->NumArguments() > 2 ? pResult->GetInteger(2) : 0; - for(int i = 0; i < pThis->m_aNameBans.size(); i++) + for(auto &Ban : pThis->m_vNameBans) { - CNameBan *pBan = &pThis->m_aNameBans[i]; - if(str_comp(pBan->m_aName, pName) == 0) + if(str_comp(Ban.m_aName, pName) == 0) { - str_format(aBuf, sizeof(aBuf), "changed name='%s' distance=%d old_distance=%d is_substring=%d old_is_substring=%d reason='%s' old_reason='%s'", pName, Distance, pBan->m_Distance, IsSubstring, pBan->m_IsSubstring, pReason, pBan->m_aReason); + str_format(aBuf, sizeof(aBuf), "changed name='%s' distance=%d old_distance=%d is_substring=%d old_is_substring=%d reason='%s' old_reason='%s'", pName, Distance, Ban.m_Distance, IsSubstring, Ban.m_IsSubstring, pReason, Ban.m_aReason); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); - pBan->m_Distance = Distance; - pBan->m_IsSubstring = IsSubstring; - str_copy(pBan->m_aReason, pReason, sizeof(pBan->m_aReason)); + Ban.m_Distance = Distance; + Ban.m_IsSubstring = IsSubstring; + str_copy(Ban.m_aReason, pReason, sizeof(Ban.m_aReason)); return; } } - pThis->m_aNameBans.add(CNameBan(pName, Distance, IsSubstring, pReason)); + pThis->m_vNameBans.emplace_back(pName, Distance, IsSubstring, pReason); str_format(aBuf, sizeof(aBuf), "added name='%s' distance=%d is_substring=%d reason='%s'", pName, Distance, IsSubstring, pReason); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); } @@ -3261,15 +3260,15 @@ void CServer::ConNameUnban(IConsole::IResult *pResult, void *pUser) CServer *pThis = (CServer *)pUser; const char *pName = pResult->GetString(0); - for(int i = 0; i < pThis->m_aNameBans.size(); i++) + for(size_t i = 0; i < pThis->m_vNameBans.size(); i++) { - CNameBan *pBan = &pThis->m_aNameBans[i]; + CNameBan *pBan = &pThis->m_vNameBans[i]; if(str_comp(pBan->m_aName, pName) == 0) { char aBuf[128]; str_format(aBuf, sizeof(aBuf), "removed name='%s' distance=%d is_substring=%d reason='%s'", pBan->m_aName, pBan->m_Distance, pBan->m_IsSubstring, pBan->m_aReason); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); - pThis->m_aNameBans.remove_index(i); + pThis->m_vNameBans.erase(pThis->m_vNameBans.begin() + i); } } } @@ -3278,11 +3277,10 @@ void CServer::ConNameBans(IConsole::IResult *pResult, void *pUser) { CServer *pThis = (CServer *)pUser; - for(int i = 0; i < pThis->m_aNameBans.size(); i++) + for(auto &Ban : pThis->m_vNameBans) { - CNameBan *pBan = &pThis->m_aNameBans[i]; char aBuf[128]; - str_format(aBuf, sizeof(aBuf), "name='%s' distance=%d is_substring=%d reason='%s'", pBan->m_aName, pBan->m_Distance, pBan->m_IsSubstring, pBan->m_aReason); + str_format(aBuf, sizeof(aBuf), "name='%s' distance=%d is_substring=%d reason='%s'", Ban.m_aName, Ban.m_Distance, Ban.m_IsSubstring, Ban.m_aReason); pThis->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "name_ban", aBuf); } } diff --git a/src/engine/server/server.h b/src/engine/server/server.h index 0bc3f5a3a..faf6e9b42 100644 --- a/src/engine/server/server.h +++ b/src/engine/server/server.h @@ -21,8 +21,6 @@ #include #include -#include - #include #include @@ -268,7 +266,7 @@ public: char m_aErrorShutdownReason[128]; - array m_aNameBans; + std::vector m_vNameBans; CServer(); ~CServer(); diff --git a/src/test/name_ban.cpp b/src/test/name_ban.cpp index 6bfaf85a5..2eb765973 100644 --- a/src/test/name_ban.cpp +++ b/src/test/name_ban.cpp @@ -4,26 +4,29 @@ TEST(NameBan, Empty) { - EXPECT_FALSE(IsNameBanned("", 0, 0)); - EXPECT_FALSE(IsNameBanned("abc", 0, 0)); + std::vector vBans; + EXPECT_FALSE(IsNameBanned("", vBans)); + EXPECT_FALSE(IsNameBanned("abc", vBans)); } TEST(NameBan, Equality) { - CNameBan Abc0("abc", 0, 0); - EXPECT_TRUE(IsNameBanned("abc", &Abc0, 1)); - EXPECT_TRUE(IsNameBanned(" abc", &Abc0, 1)); - EXPECT_TRUE(IsNameBanned("abc ", &Abc0, 1)); - EXPECT_TRUE(IsNameBanned("abc foo", &Abc0, 1)); // Maximum name length. - EXPECT_TRUE(IsNameBanned("äbc", &Abc0, 1)); // Confusables - EXPECT_FALSE(IsNameBanned("def", &Abc0, 1)); - EXPECT_FALSE(IsNameBanned("abcdef", &Abc0, 1)); + std::vector vBans; + vBans.emplace_back("abc", 0, 0); + EXPECT_TRUE(IsNameBanned("abc", vBans)); + EXPECT_TRUE(IsNameBanned(" abc", vBans)); + EXPECT_TRUE(IsNameBanned("abc ", vBans)); + EXPECT_TRUE(IsNameBanned("abc foo", vBans)); // Maximum name length. + EXPECT_TRUE(IsNameBanned("äbc", vBans)); // Confusables + EXPECT_FALSE(IsNameBanned("def", vBans)); + EXPECT_FALSE(IsNameBanned("abcdef", vBans)); } TEST(NameBan, Substring) { - CNameBan Xyz("xyz", 0, 1); - EXPECT_TRUE(IsNameBanned("abcxyz", &Xyz, 1)); - EXPECT_TRUE(IsNameBanned("abcxyzdef", &Xyz, 1)); - EXPECT_FALSE(IsNameBanned("abcdef", &Xyz, 1)); + std::vector vBans; + vBans.emplace_back("xyz", 0, 1); + EXPECT_TRUE(IsNameBanned("abcxyz", vBans)); + EXPECT_TRUE(IsNameBanned("abcxyzdef", vBans)); + EXPECT_FALSE(IsNameBanned("abcdef", vBans)); }