From 345d8fa8867a37de7493d0bc353a36c6e2680419 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/core') diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index 1a5e97f..5bd037f 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -484,4 +484,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 78d713d..5ba10c4 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -1229,3 +1229,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