Skip to content

Commit f422891

Browse files
Trim keybind whitespace to avoid duplicates (#2124)
* Trim keybind whitespace to avoid duplicates * Avoid trying to dup null * Free szCompArguments * Update CKeyBinds.cpp * Update CKeyBinds.cpp
1 parent 696ccf0 commit f422891

File tree

3 files changed

+47
-4
lines changed

3 files changed

+47
-4
lines changed

Client/core/CKeyBinds.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,13 @@ CCommandBind* CKeyBinds::FindCommandMatch(const char* szKey, const char* szComma
926926
{
927927
NullEmptyStrings(szKey, szArguments, szResource, szOriginalScriptKey);
928928

929+
char* szCompArguments = nullptr;
930+
if (szArguments)
931+
szCompArguments = strdup(szArguments);
932+
if (szCompArguments)
933+
szCompArguments = SharedUtil::Trim(szCompArguments);
934+
935+
CCommandBind* pResult = nullptr;
929936
list<CKeyBind*>::const_iterator iter = m_pList->begin();
930937
for (; iter != m_pList->end(); iter++)
931938
{
@@ -938,15 +945,16 @@ CCommandBind* CKeyBinds::FindCommandMatch(const char* szKey, const char* szComma
938945
{
939946
if (!bCheckState || (pBind->bHitState == bState))
940947
{
941-
if (!szArguments || (pBind->szArguments && strcmp(pBind->szArguments, szArguments) == 0))
948+
if (!szCompArguments || (pBind->szArguments && strcmp(pBind->szArguments, szCompArguments) == 0))
942949
{
943950
if (!szResource || (pBind->szResource && strcmp(pBind->szResource, szResource) == 0))
944951
{
945952
if (!bCheckScriptCreated || (pBind->bScriptCreated == bScriptCreated))
946953
{
947954
if (!szOriginalScriptKey || (pBind->strOriginalScriptKey == szOriginalScriptKey))
948955
{
949-
return pBind;
956+
pResult = pBind;
957+
break;
950958
}
951959
}
952960
}
@@ -956,7 +964,8 @@ CCommandBind* CKeyBinds::FindCommandMatch(const char* szKey, const char* szComma
956964
}
957965
}
958966
}
959-
return NULL;
967+
free(szCompArguments);
968+
return pResult;
960969
}
961970

962971
//
@@ -2706,7 +2715,12 @@ void CKeyBinds::BindCommand(const char* szCmdLine)
27062715

27072716
if (szCommand)
27082717
{
2709-
char* szArguments = strtok(NULL, "\0");
2718+
char* szArguments = strtok(NULL, "\0");
2719+
if (szArguments)
2720+
szArguments = SharedUtil::Trim(szArguments);
2721+
if (szArguments != nullptr && szArguments[0] == '\0')
2722+
szArguments = nullptr;
2723+
27102724
SString strKeyState("%s", bState ? "down" : "up");
27112725
SString strCommandAndArguments("%s%s%s", szCommand, szArguments ? " " : "", szArguments ? szArguments : "");
27122726

Shared/sdk/SharedUtil.Misc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ namespace SharedUtil
246246
bool IsLuaCompiledScript(const void* pData, uint uiLength);
247247
bool IsLuaObfuscatedScript(const void* pData, uint uiLength);
248248

249+
// Return a pointer to the (shifted) trimmed string
250+
// @ref https://stackoverflow.com/a/26984026
251+
char* Trim(char* szText);
252+
249253
//
250254
// Some templates
251255
//

Shared/sdk/SharedUtil.Misc.hpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,6 +1466,31 @@ bool SharedUtil::IsColorCodeW(const wchar_t* wszColorCode)
14661466
return true;
14671467
}
14681468

1469+
char* SharedUtil::Trim(char* szText)
1470+
{
1471+
char* szOriginal = szText;
1472+
size_t uiLen = 0;
1473+
1474+
while (isspace((unsigned char)*szText))
1475+
szText++;
1476+
1477+
if (*szText)
1478+
{
1479+
char* p = szText;
1480+
while (*p)
1481+
p++;
1482+
while (isspace((unsigned char)*(--p)))
1483+
;
1484+
p[1] = '\0';
1485+
uiLen = (size_t)(p - szText + 1);
1486+
}
1487+
1488+
if (szText == szOriginal)
1489+
return szText;
1490+
1491+
return static_cast<char*>(memmove(szOriginal, szText, uiLen + 1));
1492+
}
1493+
14691494
// Convert a standard multibyte UTF-8 std::string into a UTF-16 std::wstring
14701495
std::wstring SharedUtil::MbUTF8ToUTF16(const SString& input)
14711496
{

0 commit comments

Comments
 (0)