diff options
author | Simon Tatham <anakin@pobox.com> | 2022-06-12 13:27:27 +0300 |
---|---|---|
committer | Simon Tatham <anakin@pobox.com> | 2022-06-25 16:30:39 +0300 |
commit | 76205b89e2a7889eb557bb4d86761e21bf04606c (patch) | |
tree | c968d541f44a3ec1e1ad59eacf9d4bfe25727bd3 /utils | |
parent | 1a568e3535a19b75647a7b9e4ac2192b5e9be536 (diff) |
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.
Diffstat (limited to 'utils')
-rw-r--r-- | utils/ptrlen.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/utils/ptrlen.c b/utils/ptrlen.c index 7d4e12ec..37972e49 100644 --- a/utils/ptrlen.c +++ b/utils/ptrlen.c @@ -54,6 +54,22 @@ bool ptrlen_endswith(ptrlen whole, ptrlen suffix, ptrlen *tail) 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) { const char *p = input->ptr, *end = p + input->len; |