Remove support for space separation

This commit is contained in:
ChillerDragon 2024-07-06 08:48:33 +08:00
parent 43a493445b
commit 7b540ddb16
4 changed files with 39 additions and 41 deletions

View file

@ -1605,19 +1605,20 @@ const char *str_find_nocase(const char *haystack, const char *needle);
*/ */
const char *str_find(const char *haystack, const char *needle); const char *str_find(const char *haystack, const char *needle);
/* /**
Function: str_delimiters_around_offset * @ingroup Strings
Parameters: *
haystack - String to search in * @param haystack String to search in
needle - String to search for * @param delim String to search for
* @param offset Number of characters into the haystack
Returns: * @param start Will be set to the first delimiter on the left side of the offset (or haystack start)
true if both delimiters were found * @param end Will be set to the furst delimiter on the right side of the offset (or haystack end)
false if a delimiter is missing (it uses haystart start and end as fallback) *
* @return `true` if both delimiters were found
Remarks: * @return 'false' if a delimiter is missing (it uses haystack start and end as fallback)
- The strings are treated as zero-terminated strings. *
*/ * @remark The strings are treated as zero-terminated strings.
*/
bool str_delimiters_around_offset(const char *haystay, const char *delim, int offset, int *start, int *end); bool str_delimiters_around_offset(const char *haystay, const char *delim, int offset, int *start, int *end);
/** /**

View file

@ -331,24 +331,24 @@ void CGameConsole::CInstance::PossibleCommandsCompleteCallback(int Index, const
} }
} }
void CGameConsole::CInstance::GetCommand(const char *pInput, char *pCmd, size_t CmdSize) void CGameConsole::CInstance::GetCommand(const char *pInput, char (&aCmd)[IConsole::CMDLINE_LENGTH])
{ {
char aInput[IConsole::CMDLINE_LENGTH]; char aInput[IConsole::CMDLINE_LENGTH];
str_copy(aInput, pInput); str_copy(aInput, pInput);
int Start, End;
m_CompletionCommandStart = 0; m_CompletionCommandStart = 0;
m_CompletionCommandEnd = 0; m_CompletionCommandEnd = 0;
char aaSeperators[][2] = {";", "\"", " "}; char aaSeparators[][2] = {";", "\""};
for(auto *pSeperator : aaSeperators) for(auto *pSeparator : aaSeparators)
{ {
str_delimiters_around_offset(aInput + m_CompletionCommandStart, pSeperator, m_Input.GetCursorOffset() - m_CompletionCommandStart, &Start, &End); int Start, End;
str_delimiters_around_offset(aInput + m_CompletionCommandStart, pSeparator, m_Input.GetCursorOffset() - m_CompletionCommandStart, &Start, &End);
m_CompletionCommandStart += Start; m_CompletionCommandStart += Start;
m_CompletionCommandEnd = m_CompletionCommandStart + (End - Start); m_CompletionCommandEnd = m_CompletionCommandStart + (End - Start);
aInput[m_CompletionCommandEnd] = '\0'; aInput[m_CompletionCommandEnd] = '\0';
} }
str_copy(pCmd, aInput + m_CompletionCommandStart, CmdSize); str_copy(aCmd, aInput + m_CompletionCommandStart, sizeof(aCmd));
} }
static void StrCopyUntilSpace(char *pDest, size_t DestSize, const char *pSrc) static void StrCopyUntilSpace(char *pDest, size_t DestSize, const char *pSrc)
@ -472,8 +472,8 @@ bool CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
if(!m_Searching) if(!m_Searching)
{ {
char aSearch[128]; char aSearch[IConsole::CMDLINE_LENGTH];
GetCommand(m_aCompletionBuffer, aSearch, sizeof(aSearch)); GetCommand(m_aCompletionBuffer, aSearch);
// command completion // command completion
const bool UseTempCommands = m_Type == CGameConsole::CONSOLETYPE_REMOTE && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands(); const bool UseTempCommands = m_Type == CGameConsole::CONSOLETYPE_REMOTE && m_pGameConsole->Client()->RconAuthed() && m_pGameConsole->Client()->UseTempRconCommands();
@ -612,7 +612,7 @@ bool CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
// find the current command // find the current command
{ {
char aCmd[IConsole::CMDLINE_LENGTH]; char aCmd[IConsole::CMDLINE_LENGTH];
GetCommand(GetString(), aCmd, sizeof(aCmd)); GetCommand(GetString(), aCmd);
char aBuf[IConsole::CMDLINE_LENGTH]; char aBuf[IConsole::CMDLINE_LENGTH];
StrCopyUntilSpace(aBuf, sizeof(aBuf), aCmd); StrCopyUntilSpace(aBuf, sizeof(aBuf), aCmd);
@ -1144,8 +1144,8 @@ void CGameConsole::OnRender()
Info.m_pOffsetChange = &pConsole->m_CompletionRenderOffsetChange; Info.m_pOffsetChange = &pConsole->m_CompletionRenderOffsetChange;
Info.m_Width = Screen.w; Info.m_Width = Screen.w;
Info.m_TotalWidth = 0.0f; Info.m_TotalWidth = 0.0f;
char aCmd[128]; char aCmd[IConsole::CMDLINE_LENGTH];
pConsole->GetCommand(pConsole->m_aCompletionBuffer, aCmd, sizeof(aCmd)); pConsole->GetCommand(pConsole->m_aCompletionBuffer, aCmd);
Info.m_pCurrentCmd = aCmd; Info.m_pCurrentCmd = aCmd;
TextRender()->SetCursor(&Info.m_Cursor, InitialX - Info.m_Offset, InitialY + RowHeight + 2.0f, FONT_SIZE, TEXTFLAG_RENDER | TEXTFLAG_STOP_AT_END); TextRender()->SetCursor(&Info.m_Cursor, InitialX - Info.m_Offset, InitialY + RowHeight + 2.0f, FONT_SIZE, TEXTFLAG_RENDER | TEXTFLAG_STOP_AT_END);

View file

@ -125,10 +125,9 @@ class CGameConsole : public CComponent
* The result would be " world " * The result would be " world "
* *
* @param pInput the console input line * @param pInput the console input line
* @param pCmd the command the cursor is at * @param aCmd the command the cursor is at
* @param CmdSize size of the pCmd buffer
*/ */
void GetCommand(const char *pInput, char *pCmd, size_t CmdSize); void GetCommand(const char *pInput, char (&aCmd)[IConsole::CMDLINE_LENGTH]);
static void PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser); static void PossibleCommandsCompleteCallback(int Index, const char *pStr, void *pUser);
static void PossibleArgumentsCompleteCallback(int Index, const char *pStr, void *pUser); static void PossibleArgumentsCompleteCallback(int Index, const char *pStr, void *pUser);

View file

@ -9,53 +9,51 @@ TEST(Str, StrDelim)
int Start, End; int Start, End;
// 0123456 // 0123456
// 01234567891111111 // 01234567891111111
str_delimiters_around_offset("123;123456789;aaa", ";", 5, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("123;123456789;aaa", ";", 5, &Start, &End), true);
EXPECT_EQ(Start, 4); EXPECT_EQ(Start, 4);
EXPECT_EQ(End, 13); EXPECT_EQ(End, 13);
str_delimiters_around_offset("123;123", ";", 1, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("123;123", ";", 1, &Start, &End), false);
EXPECT_EQ(Start, 0); EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 3); EXPECT_EQ(End, 3);
str_delimiters_around_offset("---foo---bar---baz---hello", "---", 1, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("---foo---bar---baz---hello", "---", 1, &Start, &End), false);
EXPECT_EQ(Start, 0); EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 0); EXPECT_EQ(End, 0);
str_delimiters_around_offset("---foo---bar---baz---hello", "---", 2, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("---foo---bar---baz---hello", "---", 2, &Start, &End), false);
EXPECT_EQ(Start, 0); EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 0); EXPECT_EQ(End, 0);
str_delimiters_around_offset("---foo---bar---baz---hello", "---", 3, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("---foo---bar---baz---hello", "---", 3, &Start, &End), true);
EXPECT_EQ(Start, 3); EXPECT_EQ(Start, 3);
EXPECT_EQ(End, 6); EXPECT_EQ(End, 6);
str_delimiters_around_offset("---foo---bar---baz---hello", "---", 4, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("---foo---bar---baz---hello", "---", 4, &Start, &End), true);
EXPECT_EQ(Start, 3); EXPECT_EQ(Start, 3);
EXPECT_EQ(End, 6); EXPECT_EQ(End, 6);
str_delimiters_around_offset("---foo---bar---baz---hello", "---", 9, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("---foo---bar---baz---hello", "---", 9, &Start, &End), true);
EXPECT_EQ(Start, 9); EXPECT_EQ(Start, 9);
EXPECT_EQ(End, 12); EXPECT_EQ(End, 12);
str_delimiters_around_offset("---foo---bar---baz---hello", "---", 22, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("---foo---bar---baz---hello", "---", 22, &Start, &End), false);
EXPECT_EQ(Start, 21); EXPECT_EQ(Start, 21);
EXPECT_EQ(End, 26); EXPECT_EQ(End, 26);
str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 2, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 2, &Start, &End), false);
EXPECT_EQ(Start, 0); EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 3); EXPECT_EQ(End, 3);
str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 3, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 3, &Start, &End), false);
EXPECT_EQ(Start, 0); EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 3); EXPECT_EQ(End, 3);
bool Found = str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 4, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 4, &Start, &End), true);
EXPECT_EQ(Found, true);
EXPECT_EQ(Start, 4); EXPECT_EQ(Start, 4);
EXPECT_EQ(End, 4); EXPECT_EQ(End, 4);
Found = str_delimiters_around_offset("", ";", 4, &Start, &End); EXPECT_EQ(str_delimiters_around_offset("", ";", 4, &Start, &End), false);
EXPECT_EQ(Found, false);
EXPECT_EQ(Start, 0); EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 0); EXPECT_EQ(End, 0);
} }