mirror of
https://github.com/ddnet/ddnet.git
synced 2024-11-10 10:08:18 +00:00
Merge #1469
1469: Don't use strtok. Close #1468 r=heinrich5991 a=Learath2 ^ Co-authored-by: Learath <learath2@gmail.com>
This commit is contained in:
commit
d8e8a21a30
|
@ -3052,7 +3052,7 @@ unsigned str_quickhash(const char *str)
|
|||
return hash;
|
||||
}
|
||||
|
||||
static const char *str_token_next(const char *str, const char *delim, int *length)
|
||||
static const char *str_token_get(const char *str, const char *delim, int *length)
|
||||
{
|
||||
str += strspn(str, delim);
|
||||
if(!*str)
|
||||
|
@ -3067,7 +3067,7 @@ int str_in_list(const char *list, const char *delim, const char *needle)
|
|||
const char *tok = list;
|
||||
int len = 0, notfound = 1, needlelen = str_length(needle);
|
||||
|
||||
while(notfound && (tok = str_token_next(tok, delim, &len)))
|
||||
while(notfound && (tok = str_token_get(tok, delim, &len)))
|
||||
{
|
||||
notfound = needlelen != len || str_comp_num(tok, needle, len);
|
||||
tok = tok + len;
|
||||
|
@ -3076,6 +3076,20 @@ int str_in_list(const char *list, const char *delim, const char *needle)
|
|||
return !notfound;
|
||||
}
|
||||
|
||||
const char *str_next_token(const char *str, const char *delim, char *buffer, int buffer_size)
|
||||
{
|
||||
int len = 0;
|
||||
const char *tok = str_token_get(str, delim, &len);
|
||||
if(len < 0)
|
||||
return NULL;
|
||||
|
||||
len = buffer_size > len ? len : buffer_size - 1;
|
||||
mem_copy(buffer, tok, len);
|
||||
buffer[len] = '\0';
|
||||
|
||||
return tok + len;
|
||||
}
|
||||
|
||||
int pid()
|
||||
{
|
||||
#if defined(CONF_FAMILY_WINDOWS)
|
||||
|
|
|
@ -1832,6 +1832,21 @@ int str_utf16le_encode(char *ptr, int chr);
|
|||
*/
|
||||
int str_utf8_check(const char *str);
|
||||
|
||||
/*
|
||||
Function: str_next_token
|
||||
Writes the next token after str into buf, returns the rest of the string.
|
||||
Parameters:
|
||||
str - Pointer to string.
|
||||
delim - Delimiter for tokenization.
|
||||
buffer - Buffer to store token in.
|
||||
buffer_size - Size of the buffer.
|
||||
Returns:
|
||||
Pointer to rest of the string.
|
||||
Remarks:
|
||||
- The token is always null-terminated.
|
||||
*/
|
||||
const char *str_next_token(const char *str, const char *delim, char *buffer, int buffer_size);
|
||||
|
||||
/*
|
||||
Function: str_in_list
|
||||
Checks if needle is in list delimited by delim
|
||||
|
|
|
@ -286,7 +286,7 @@ void CServerBrowser::Filter()
|
|||
{
|
||||
MatchFound = 1;
|
||||
}
|
||||
|
||||
|
||||
// match against gametype
|
||||
if(str_find_nocase(m_ppServerlist[i]->m_Info.m_aGameType, g_Config.m_BrExcludeString))
|
||||
{
|
||||
|
@ -1204,44 +1204,25 @@ void CServerBrowser::DDNetFilterRem(char *pFilter, const char *pName)
|
|||
|
||||
// rewrite exclude/filter list
|
||||
char aBuf[128];
|
||||
char *p;
|
||||
|
||||
str_copy(aBuf, pFilter, sizeof(aBuf));
|
||||
pFilter[0] = '\0';
|
||||
|
||||
p = strtok(aBuf, ",");
|
||||
|
||||
while(p)
|
||||
char aToken[128];
|
||||
for(const char *tok = aBuf; (tok = str_next_token(tok, ",", aToken, sizeof(aToken)));)
|
||||
{
|
||||
if(str_comp_nocase(pName, p) != 0)
|
||||
if(str_comp_nocase(pName, aToken) != 0)
|
||||
{
|
||||
char aBuf2[128];
|
||||
str_format(aBuf2, sizeof(aBuf2), ",%s", p);
|
||||
str_format(aBuf2, sizeof(aBuf2), ",%s", aToken);
|
||||
str_append(pFilter, aBuf2, 128);
|
||||
}
|
||||
|
||||
p = strtok(NULL, ",");
|
||||
}
|
||||
}
|
||||
|
||||
bool CServerBrowser::DDNetFiltered(char *pFilter, const char *pName)
|
||||
{
|
||||
char aBuf[128];
|
||||
char *p;
|
||||
|
||||
str_copy(aBuf, pFilter, sizeof(aBuf));
|
||||
|
||||
p = strtok(aBuf, ",");
|
||||
|
||||
while(p)
|
||||
{
|
||||
if(str_comp_nocase(pName, p) == 0)
|
||||
return true; // country excluded
|
||||
|
||||
p = strtok(NULL, ",");
|
||||
}
|
||||
|
||||
return false; // country not excluded
|
||||
return str_in_list(pFilter, ",", pName); // country not excluded
|
||||
}
|
||||
|
||||
void CServerBrowser::DDNetCountryFilterClean()
|
||||
|
|
|
@ -122,14 +122,11 @@ void CFileScore::Init()
|
|||
if (g_Config.m_SvCheckpointSave)
|
||||
{
|
||||
std::getline(f, TmpCpLine);
|
||||
char *pTime = strtok((char*) TmpCpLine.c_str(), " ");
|
||||
|
||||
std::istringstream iss(TmpCpLine);
|
||||
int i = 0;
|
||||
while (pTime != NULL && i < NUM_CHECKPOINTS)
|
||||
{
|
||||
aTmpCpTime[i] = atof(pTime);
|
||||
pTime = strtok(NULL, " ");
|
||||
i++;
|
||||
}
|
||||
for(std::string p; std::getline(iss, p, ' '); i++)
|
||||
aTmpCpTime[i] = std::stof(p, NULL);
|
||||
}
|
||||
m_Top.add(
|
||||
*new CPlayerScore(TmpName.c_str(), atof(TmpScore.c_str()),
|
||||
|
|
|
@ -136,6 +136,20 @@ TEST(Str, HexDecode)
|
|||
EXPECT_EQ(str_hex_decode(aOut, 4, "41424344"), 0); EXPECT_STREQ(aOut, "ABCD");
|
||||
}
|
||||
|
||||
TEST(Str, Tokenize)
|
||||
{
|
||||
char aTest[] = "GER,RUS,ZAF,BRA,CAN";
|
||||
const char *aOut[] = {"GER", "RUS", "ZAF", "BRA", "CAN"};
|
||||
char aBuf[4];
|
||||
|
||||
int n = 0;
|
||||
for(const char *tok = aTest; (tok = str_next_token(tok, ",", aBuf, sizeof(aBuf)));)
|
||||
EXPECT_STREQ(aOut[n++], aBuf);
|
||||
|
||||
char aTest2[] = "";
|
||||
EXPECT_EQ(str_next_token(aTest2, ",", aBuf, sizeof(aBuf)), nullptr);
|
||||
}
|
||||
|
||||
TEST(Str, InList)
|
||||
{
|
||||
char aTest[] = "GER,RUS,ZAF,BRA,CAN";
|
||||
|
|
Loading…
Reference in a new issue