1
0
mirror of https://git.tartarus.org/simon/putty.git synced 2025-01-09 17:38:00 +00:00

A few more ptrlen functions.

ptrlen_contains and ptrlen_contains_only are useful for checking that
a string is composed entirely of certain characters, or avoids them.

ptrlen_end makes a pointer to the byte just past the end of the
specified string. And it can be used with make_ptrlen_startend, which
makes a ptrlen out of two pointers instead of a pointer and a length.
This commit is contained in:
Simon Tatham 2022-06-12 11:27:27 +01:00
parent 1a568e3535
commit 76205b89e2
2 changed files with 33 additions and 0 deletions

17
misc.h
View File

@ -169,6 +169,21 @@ static inline ptrlen make_ptrlen(const void *ptr, size_t len)
return pl; return pl;
} }
static inline const void *ptrlen_end(ptrlen pl)
{
return (const char *)pl.ptr + pl.len;
}
static inline ptrlen make_ptrlen_startend(const void *startv, const void *endv)
{
const char *start = (const char *)startv, *end = (const char *)endv;
assert(end >= start);
ptrlen pl;
pl.ptr = start;
pl.len = end - start;
return pl;
}
static inline ptrlen ptrlen_from_asciz(const char *str) static inline ptrlen ptrlen_from_asciz(const char *str)
{ {
return make_ptrlen(str, strlen(str)); return make_ptrlen(str, strlen(str));
@ -190,6 +205,8 @@ int ptrlen_strcmp(ptrlen pl1, ptrlen pl2);
bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail); bool ptrlen_startswith(ptrlen whole, ptrlen prefix, ptrlen *tail);
bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail); bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail);
ptrlen ptrlen_get_word(ptrlen *input, const char *separators); ptrlen ptrlen_get_word(ptrlen *input, const char *separators);
bool ptrlen_contains(ptrlen input, const char *characters);
bool ptrlen_contains_only(ptrlen input, const char *characters);
char *mkstr(ptrlen pl); char *mkstr(ptrlen pl);
int string_length_for_printf(size_t); int string_length_for_printf(size_t);
/* Derive two printf arguments from a ptrlen, suitable for "%.*s" */ /* Derive two printf arguments from a ptrlen, suitable for "%.*s" */

View File

@ -54,6 +54,22 @@ bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail)
return false; return false;
} }
bool ptrlen_contains(ptrlen input, const char *characters)
{
for (const char *p = input.ptr, *end = p + input.len; p < end; p++)
if (strchr(characters, *p))
return true;
return false;
}
bool ptrlen_contains_only(ptrlen input, const char *characters)
{
for (const char *p = input.ptr, *end = p + input.len; p < end; p++)
if (!strchr(characters, *p))
return false;
return true;
}
ptrlen ptrlen_get_word(ptrlen *input, const char *separators) ptrlen ptrlen_get_word(ptrlen *input, const char *separators)
{ {
const char *p = input->ptr, *end = p + input->len; const char *p = input->ptr, *end = p + input->len;