From 3bb55e8aa86f054c081c372b227f723e3aad14c7 Mon Sep 17 00:00:00 2001 From: Brian Gesiak Date: Wed, 30 Apr 2014 17:58:06 +0900 Subject: [PATCH 1/2] strbuf: use _rtrim and _ltrim in strbuf_trim strbuf_trim() strips whitespace from the end, then the beginning of a strbuf. Those operations are duplicated in strbuf_rtrim() and strbuf_ltrim(). Replace strbuf_trim() implementation with calls to strbuf_rtrim(), then strbuf_ltrim(). Signed-off-by: Brian Gesiak Reviewed-by: Jeff King Signed-off-by: Junio C Hamano --- strbuf.c | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/strbuf.c b/strbuf.c index ee96dcfb81..4d31567a1a 100644 --- a/strbuf.c +++ b/strbuf.c @@ -78,15 +78,8 @@ void strbuf_grow(struct strbuf *sb, size_t extra) void strbuf_trim(struct strbuf *sb) { - char *b = sb->buf; - while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1])) - sb->len--; - while (sb->len > 0 && isspace(*b)) { - b++; - sb->len--; - } - memmove(sb->buf, b, sb->len); - sb->buf[sb->len] = '\0'; + strbuf_rtrim(sb); + strbuf_ltrim(sb); } void strbuf_rtrim(struct strbuf *sb) { From 10f5b034b6aaff706c4776b3cc0ee993c33e8f43 Mon Sep 17 00:00:00 2001 From: Brian Gesiak Date: Wed, 30 Apr 2014 17:58:07 +0900 Subject: [PATCH 2/2] api-strbuf.txt: add docs for _trim and _ltrim API documentation for strbuf does not document strbuf_trim() or strbuf_ltrim(). Add documentation for these two functions. Signed-off-by: Brian Gesiak Reviewed-by: Jeff King Signed-off-by: Junio C Hamano --- Documentation/technical/api-strbuf.txt | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Documentation/technical/api-strbuf.txt b/Documentation/technical/api-strbuf.txt index 3350d97dda..4396be9dda 100644 --- a/Documentation/technical/api-strbuf.txt +++ b/Documentation/technical/api-strbuf.txt @@ -121,10 +121,19 @@ Functions * Related to the contents of the buffer +`strbuf_trim`:: + + Strip whitespace from the beginning and end of a string. + Equivalent to performing `strbuf_rtrim()` followed by `strbuf_ltrim()`. + `strbuf_rtrim`:: Strip whitespace from the end of a string. +`strbuf_ltrim`:: + + Strip whitespace from the beginning of a string. + `strbuf_cmp`:: Compare two buffers. Returns an integer less than, equal to, or greater