- Commit
- 3317e5039f5c940225dffa5c735507e897c230b8
- Parent
- e76ccb29825b42c69c308ab3119c3b5ef68b8027
- Author
- John MacFarlane <jgm@berkeley.edu>
- Date
buffer: added strbuf_rtrim function.
My personal build of CMark ✏️
buffer: added strbuf_rtrim function.
2 files changed, 17 insertions, 9 deletions
Status | File Name | N° Changes | Insertions | Deletions |
Modified | src/buffer.c | 25 | 16 | 9 |
Modified | src/buffer.h | 1 | 1 | 0 |
diff --git a/src/buffer.c b/src/buffer.c @@ -291,19 +291,11 @@ void strbuf_drop(strbuf *buf, int n) } } -void strbuf_trim(strbuf *buf) +void strbuf_rtrim(strbuf *buf) { - int i = 0; - if (!buf->size) return; - while (i < buf->size && isspace(buf->ptr[i])) - i++; - - strbuf_drop(buf, i); - - /* rtrim */ while (buf->size > 0) { if (!isspace(buf->ptr[buf->size - 1])) break; @@ -314,6 +306,21 @@ void strbuf_trim(strbuf *buf) buf->ptr[buf->size] = '\0'; } +void strbuf_trim(strbuf *buf) +{ + int i = 0; + + if (!buf->size) + return; + + while (i < buf->size && isspace(buf->ptr[i])) + i++; + + strbuf_drop(buf, i); + + strbuf_rtrim(buf); +} + // Destructively modify string, collapsing consecutive // space and newline characters into a single space. void strbuf_normalize_whitespace(strbuf *s)
diff --git a/src/buffer.h b/src/buffer.h @@ -107,6 +107,7 @@ int strbuf_strchr(const strbuf *buf, int c, int pos); int strbuf_strrchr(const strbuf *buf, int c, int pos); void strbuf_drop(strbuf *buf, int n); void strbuf_truncate(strbuf *buf, int len); +void strbuf_rtrim(strbuf *buf); void strbuf_trim(strbuf *buf); void strbuf_normalize_whitespace(strbuf *s); void strbuf_unescape(strbuf *s);