Skip to contentSkip to conversation

strbuf: Use _rtrim and _ltrim in strbuf_trim

PATCH In review Brian Gesiak 2 participants 3 messages Jun 15, 2016 Documentation

lore
Share & tools
lore
#0 lore raw

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.

---

This is tangential to my GSoC project; I noticed the duplication and thought it could be remedied.

 strbuf.c | 11 ++---------
 1 file changed, 2 insertions(+), 9 deletions(-)
strbuf.c +2 -9
index 83caf4a..382cf68 100644
@@ -96,15 +96,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)
 {
Signature · 1 line
1.9.2.507.g779792a
#1 lore raw

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.

---
 Documentation/technical/api-strbuf.txt | 9 +++++++++
 1 file changed, 9 insertions(+)
Documentation/technical/api-strbuf.txt +9 -0
index 3350d97..4396be9 100644
@@ -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
Signature · 1 line
1.9.2.507.g779792a
#2 lore raw
In reply to Brian Gesiak
Quoted 6 lines · Brian Gesiak

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.

Thanks, this looks good. I wondered if perhaps doing them together inline might have been more efficient, but there really is no overlap in what they compute.

The documentation patch looks good to me, too.

-Peff