From 357e6a0efc62f2151ba24b4730cc35015559bd41 Mon Sep 17 00:00:00 2001 From: Karel Zak Date: Wed, 11 Oct 2023 12:37:40 +0200 Subject: [PATCH] lib/strutils: add ul_next_string() The function jumps to the next string in buffer where strings are separated by \0. Signed-off-by: Karel Zak --- include/strutils.h | 17 +++++++++++++++++ lib/strutils.c | 9 +++++++++ 2 files changed, 26 insertions(+) diff --git a/include/strutils.h b/include/strutils.h index 7ae8511ab8..7b44b4e00b 100644 --- a/include/strutils.h +++ b/include/strutils.h @@ -388,6 +388,23 @@ static inline void strrem(char *s, int rem) *p = '\0'; } +/* returns next string after \0 if before @end */ +static inline char *ul_next_string(char *p, char *end) +{ + char *last; + + if (!p || !end || p >= end) + return NULL; + + for (last = p; p < end; p++) { + if (*last == '\0' && p != last) + return p; + last = p; + } + + return NULL; +} + extern char *strnconcat(const char *s, const char *suffix, size_t b); extern char *strconcat(const char *s, const char *suffix); extern char *strfconcat(const char *s, const char *format, ...) diff --git a/lib/strutils.c b/lib/strutils.c index 2bfc8ac6cf..ccf71b987f 100644 --- a/lib/strutils.c +++ b/lib/strutils.c @@ -1414,6 +1414,15 @@ int main(int argc, char *argv[]) printf("\"%s\" --> \"%s\"\n", argv[2], ul_strchr_escaped(argv[2], *argv[3])); return EXIT_SUCCESS; + } else if (argc == 2 && strcmp(argv[1], "--next-string") == 0) { + char *buf = "abc\0Y\0\0xyz\0X"; + char *end = buf + 12; + char *p = buf; + + do { + printf("str: '%s'\n", p); + } while ((p = ul_next_string(p, end))); + } else { fprintf(stderr, "usage: %1$s --size [suffix]\n" " %1$s --cmp-paths \n" -- 2.47.3