Improve interface to function

This commit is contained in:
Learath 2019-03-11 14:39:54 +03:00
parent c4af290df5
commit 9713f6c2cd
4 changed files with 20 additions and 34 deletions

View file

@ -3041,7 +3041,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)
@ -3056,7 +3056,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;
@ -3065,25 +3065,18 @@ int str_in_list(const char *list, const char *delim, const char *needle)
return !notfound;
}
int str_tokenize(const char *str, const char *delim, const char **state, char *buf, int bufsz)
const char *str_next_token(const char *str, const char *delim, char *buffer, int buffer_size)
{
const char *ret = NULL;
int len = 0;
const char *tok = str_token_get(str, delim, &len);
if(len < 0)
return NULL;
if((!str && !state) || !buf)
return -1;
len = buffer_size > len ? len : buffer_size - 1;
mem_copy(buffer, tok, len);
buffer[len] = '\0';
str = str ? str : *state;
if(!(ret = str_token_next(str, delim, &len)))
return -1;
*state = ret + len;
len = bufsz > len ? len : bufsz - 1;
mem_copy(buf, ret, len);
buf[len] = '\0';
return len;
return tok + len;
}
int pid()

View file

@ -1832,20 +1832,19 @@ int str_utf16le_encode(char *ptr, int chr);
int str_utf8_check(const char *str);
/*
Function: str_tokenize
Tokenizes a string.
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.
state - Pointer to remaining string
buf - Buffer to store token in.
bufsz - Size of the buffer.
buffer - Buffer to store token in.
buffer_size - Size of the buffer.
Returns:
The number of characters written to buf or -1 for end of string
Pointer to rest of the string.
Remarks:
- The token is always null-terminated.
*/
int str_tokenize(const char *str, const char *delim, const char **state, char *buf, int bufsz);
const char *str_next_token(const char *str, const char *delim, char *buffer, int buffer_size);
/*
Function: str_in_list

View file

@ -1208,11 +1208,8 @@ void CServerBrowser::DDNetFilterRem(char *pFilter, const char *pName)
str_copy(aBuf, pFilter, sizeof(aBuf));
pFilter[0] = '\0';
const char *pState;
char aToken[128];
str_tokenize(aBuf, ",", &pState, aToken, sizeof(aToken));
do
for(const char *tok = aBuf; (tok = str_next_token(tok, ",", aToken, sizeof (aToken)));)
{
if(str_comp_nocase(pName, aToken) != 0)
{
@ -1220,7 +1217,7 @@ void CServerBrowser::DDNetFilterRem(char *pFilter, const char *pName)
str_format(aBuf2, sizeof(aBuf2), ",%s", aToken);
str_append(pFilter, aBuf2, 128);
}
} while(str_tokenize(NULL, ",", &pState, aToken, sizeof(aToken)) >= 0);
}
}
bool CServerBrowser::DDNetFiltered(char *pFilter, const char *pName)

View file

@ -140,17 +140,14 @@ TEST(Str, Tokenize)
{
char aTest[] = "GER,RUS,ZAF,BRA,CAN";
const char *aOut[] = {"GER", "RUS", "ZAF", "BRA", "CAN"};
const char *pState;
char aBuf[4];
int n = 0;
str_tokenize(aTest, ",", &pState, aBuf, sizeof aBuf);
do {
for(const char *tok = aTest; (tok = str_next_token(tok, ",", aBuf, sizeof aBuf));)
EXPECT_STREQ(aOut[n++], aBuf);
} while(str_tokenize(NULL, ",", &pState, aBuf, sizeof aBuf) >= 0);
char aTest2[] = "";
EXPECT_EQ(str_tokenize(aTest2, ",", &pState, aBuf, sizeof aBuf), -1);
EXPECT_EQ(str_next_token(aTest2, ",", aBuf, sizeof aBuf), nullptr);
}
TEST(Str, InList)