diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-29 13:24:17 -0400 |
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-29 13:24:17 -0400 |
| commit | c354f4743c7897e2fe8ca69266146d7e8b86a222 (patch) | |
| tree | b8b992ed1207ca4b03bbf55fd1516e93891ffff2 | |
| parent | 9ee2ab3927b62e1cb32d0eb94b78161ebeaabc93 (diff) | |
| download | stasis-c354f4743c7897e2fe8ca69266146d7e8b86a222.tar.gz | |
add non_format_len() function
| -rw-r--r-- | src/lib/core/include/utils.h | 7 | ||||
| -rw-r--r-- | src/lib/core/utils.c | 20 |
2 files changed, 27 insertions, 0 deletions
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; +} + |
