summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/til_str.c18
-rw-r--r--src/til_str.h1
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
© All Rights Reserved