diff options
Diffstat (limited to 'src')
| -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; +} + |
