From c354f4743c7897e2fe8ca69266146d7e8b86a222 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Wed, 29 Apr 2026 13:24:17 -0400 Subject: add non_format_len() function --- src/lib/core/include/utils.h | 7 +++++++ src/lib/core/utils.c | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'src/lib') diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index c1ee513..22835ee 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -483,4 +483,11 @@ int str_to_timeout(char *s); const char *get_random_generator_file(); int get_random_bytes(char *result, size_t maxlen); +/** + * Get length of `s` as if any formatters are not present + * @param s format string + * @return length + */ +int non_format_len(const char *s); + #endif //STASIS_UTILS_H diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index b70b713..4f06d14 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -1219,3 +1219,23 @@ int get_random_bytes(char *result, size_t maxlen) { result[bytes ? bytes - 1 : 0] = '\0'; return 0; } + +int non_format_len(const char *s) { + int len = 0; + int until_space = 0; + for (size_t i = 0; i < strlen(s); i++) { + if (until_space && isspace(s[i])) { + until_space = 0; + } + if (until_space && !isspace(s[i])) { + continue; + } + if (s[i] == '%') { + until_space = 1; + continue; + } + len++; + } + return len; +} + -- cgit