diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2023-06-26 14:53:55 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2023-07-04 21:09:16 -0700 |
commit | a33c742c87c5e267f00e561c27f96ddfb522f6bc (patch) | |
tree | 413437ee5043d9094bcc852fc3a40c1f883f489c | |
parent | 8fc32619d8e63518ee8e205da9f96e274e178b2d (diff) |
til_str: introduce til_str_chomp()
Helper for trimming off a trailing CRNL or NL if present
Clearly I once knew perl if this is the name that came to mind
-rw-r--r-- | src/til_str.c | 18 | ||||
-rw-r--r-- | src/til_str.h | 1 |
2 files changed, 19 insertions, 0 deletions
diff --git a/src/til_str.c b/src/til_str.c index f3cc5e8..fa256d7 100644 --- a/src/til_str.c +++ b/src/til_str.c @@ -179,3 +179,21 @@ char * til_str_to_buf(til_str_t *str, size_t *res_len) return buf; } + + +/* truncate off trailing \n or \r\n if present + * str is passed through for convenience (til_str_to_buf(til_str_chomp(str), &len)) etc... + */ +til_str_t * til_str_chomp(til_str_t *str) +{ + assert(str); + + if (str->size.used > 1 && str->buf[str->size.used - 2] == '\n') { + if (str->size.used > 2 && str->buf[str->size.used - 3] == '\r') + str->size.used--; + str->size.used--; + str->buf[str->size.used - 1] = '\0'; + } + + return str; +} diff --git a/src/til_str.h b/src/til_str.h index e653884..544185c 100644 --- a/src/til_str.h +++ b/src/til_str.h @@ -10,5 +10,6 @@ int til_str_appendf(til_str_t *str, const char *format, ...); char * til_str_strdup(const til_str_t *str); const char * til_str_buf(const til_str_t *str, size_t *res_len); char * til_str_to_buf(til_str_t *str, size_t *res_len); +til_str_t * til_str_chomp(til_str_t *str); #endif |