mirror of
https://git.tartarus.org/simon/putty.git
synced 2025-07-09 07:13:43 -05:00
New utility function: ptrlen_get_word().
This is similar to strtok, only it operates on a ptrlen. Therefore it can be properly stateless, or rather, it stores its state by overwriting the input ptrlen to point to a tail of its previous value. Also in this commit I add a clarifying comment about when ptrlen_{starts,ends}with will write through its 'tail' pointer.
This commit is contained in:
20
utils.c
20
utils.c
@ -938,6 +938,26 @@ bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
|
||||
return false;
|
||||
}
|
||||
|
||||
ptrlen ptrlen_get_word(ptrlen *input, const char *separators)
|
||||
{
|
||||
const char *p = input->ptr, *end = p + input->len;
|
||||
ptrlen toret;
|
||||
|
||||
while (p < end && strchr(separators, *p))
|
||||
p++;
|
||||
toret.ptr = p;
|
||||
while (p < end && !strchr(separators, *p))
|
||||
p++;
|
||||
toret.len = p - (const char *)toret.ptr;
|
||||
|
||||
size_t to_consume = p - (const char *)input->ptr;
|
||||
assert(to_consume <= input->len);
|
||||
input->ptr = (const char *)input->ptr + to_consume;
|
||||
input->len -= to_consume;
|
||||
|
||||
return toret;
|
||||
}
|
||||
|
||||
char *mkstr(ptrlen pl)
|
||||
{
|
||||
char *p = snewn(pl.len + 1, char);
|
||||
|
Reference in New Issue
Block a user