diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-04-29 13:24:17 -0400 |
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2026-05-11 15:55:10 -0400 |
| commit | 345d8fa8867a37de7493d0bc353a36c6e2680419 (patch) | |
| tree | f8a8c8aebe1a520009345fae3b248646fc9a8cfe /src/lib | |
| parent | 5561871e9fe902a6787bd13475b94e37831b923a (diff) | |
| download | stasis-345d8fa8867a37de7493d0bc353a36c6e2680419.tar.gz | |
add non_format_len() function
Diffstat (limited to 'src/lib')
| -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 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; +} + |
