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);
/*
Function: str_delimiters_around_offset
Parameters:
haystack - String to search in
needle - String to search for
Returns:
true if both delimiters were found
false if a delimiter is missing (it uses haystart start and end as fallback)
Remarks:
- The strings are treated as zero-terminated strings.
*/
/**
* @ingroup Strings
*
* @param haystack String to search in
* @param delim String to search for
* @param offset Number of characters into the haystack
* @param start Will be set to the first delimiter on the left side of the offset (or haystack start)
* @param end Will be set to the furst delimiter on the right side of the offset (or haystack end)
*
* @return `true` if both delimiters were found
* @return 'false' if a delimiter is missing (it uses haystack start and end as fallback)
*
* @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);
/**

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];
str_copy(aInput, pInput);
int Start, End;
m_CompletionCommandStart = 0;
m_CompletionCommandEnd = 0;
char aaSeperators[][2] = {";", "\"", " "};
for(auto *pSeperator : aaSeperators)
char aaSeparators[][2] = {";", "\""};
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_CompletionCommandEnd = m_CompletionCommandStart + (End - Start);
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)
@ -472,8 +472,8 @@ bool CGameConsole::CInstance::OnInput(const IInput::CEvent &Event)
if(!m_Searching)
{
char aSearch[128];
GetCommand(m_aCompletionBuffer, aSearch, sizeof(aSearch));
char aSearch[IConsole::CMDLINE_LENGTH];
GetCommand(m_aCompletionBuffer, aSearch);
// command completion
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
{
char aCmd[IConsole::CMDLINE_LENGTH];
GetCommand(GetString(), aCmd, sizeof(aCmd));
GetCommand(GetString(), aCmd);
char aBuf[IConsole::CMDLINE_LENGTH];
StrCopyUntilSpace(aBuf, sizeof(aBuf), aCmd);
@ -1144,8 +1144,8 @@ void CGameConsole::OnRender()
Info.m_pOffsetChange = &pConsole->m_CompletionRenderOffsetChange;
Info.m_Width = Screen.w;
Info.m_TotalWidth = 0.0f;
char aCmd[128];
pConsole->GetCommand(pConsole->m_aCompletionBuffer, aCmd, sizeof(aCmd));
char aCmd[IConsole::CMDLINE_LENGTH];
pConsole->GetCommand(pConsole->m_aCompletionBuffer, 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);

View file

@ -125,10 +125,9 @@ class CGameConsole : public CComponent
* The result would be " world "
*
* @param pInput the console input line
* @param pCmd the command the cursor is at
* @param CmdSize size of the pCmd buffer
* @param aCmd the command the cursor is at
*/
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 PossibleArgumentsCompleteCallback(int Index, const char *pStr, void *pUser);

View file

@ -9,53 +9,51 @@ TEST(Str, StrDelim)
int Start, End;
// 0123456
// 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(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(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(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(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(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(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(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(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(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(End, 3);
bool Found = str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 4, &Start, &End);
EXPECT_EQ(Found, true);
EXPECT_EQ(str_delimiters_around_offset("foo;;;;bar;;;;;;", ";", 4, &Start, &End), true);
EXPECT_EQ(Start, 4);
EXPECT_EQ(End, 4);
Found = str_delimiters_around_offset("", ";", 4, &Start, &End);
EXPECT_EQ(Found, false);
EXPECT_EQ(str_delimiters_around_offset("", ";", 4, &Start, &End), false);
EXPECT_EQ(Start, 0);
EXPECT_EQ(End, 0);
}