login
Header Space

 
 

[PATCH 1/6] Add strbuf_rtrim and strbuf_insert.

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: <git@...>
Cc: Pierre Habouzit <madcoder@...>
Date: Saturday, September 8, 2007 - 8:04 pm

* strbuf_rtrim removes trailing spaces.
  * strbuf_insert insert data at a given position.

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
---
 strbuf.c |   18 ++++++++++++++++++
 strbuf.h |    6 ++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/strbuf.c b/strbuf.c
index 7136de1..2643ce1 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -28,6 +28,24 @@ void strbuf_grow(struct strbuf *sb, size_t extra) {
 	ALLOC_GROW(sb->buf, sb->len + extra + 1, sb->alloc);
 }
 
+void strbuf_rtrim(struct strbuf *sb)
+{
+	while (sb->len > 0 && isspace((unsigned char)sb->buf[sb->len - 1]))
+		sb->len--;
+	sb->buf[sb->len] = '\0';
+}
+
+void strbuf_insert(struct strbuf *sb, size_t pos, const void *data, size_t len) {
+	strbuf_grow(sb, len);
+	if (pos >= sb->len) {
+		sb->len = pos;
+	} else {
+		memmove(sb->buf + pos + len, sb->buf + pos, sb->len - pos);
+	}
+	memcpy(sb->buf + pos, data, len);
+	strbuf_setlen(sb, sb->len + len);
+}
+
 void strbuf_add(struct strbuf *sb, const void *data, size_t len) {
 	strbuf_grow(sb, len);
 	memcpy(sb->buf + sb->len, data, len);
diff --git a/strbuf.h b/strbuf.h
index b40dc99..b90cbdf 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -68,6 +68,9 @@ static inline void strbuf_setlen(struct strbuf *sb, size_t len) {
 
 extern void strbuf_grow(struct strbuf *, size_t);
 
+/*----- content related -----*/
+extern void strbuf_rtrim(struct strbuf *);
+
 /*----- add data in your buffer -----*/
 static inline void strbuf_addch(struct strbuf *sb, int c) {
 	strbuf_grow(sb, 1);
@@ -75,6 +78,9 @@ static inline void strbuf_addch(struct strbuf *sb, int c) {
 	sb->buf[sb->len] = '\0';
 }
 
+/* inserts after pos, or appends if pos >= sb->len */
+extern void strbuf_insert(struct strbuf *, size_t pos, const void *, size_t);
+
 extern void strbuf_add(struct strbuf *, const void *, size_t);
 static inline void strbuf_addstr(struct strbuf *sb, const char *s) {
 	strbuf_add(sb, s, strlen(s));
-- 
1.5.3.1

-
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Use more strbufs series [on top of next], Pierre Habouzit, (Sat Sep 8, 8:04 pm)
[PATCH 1/6] Strbuf API extensions and fixes., Pierre Habouzit, (Mon Sep 10, 6:35 am)
Re: Use more strbufs series [on top of next], Pierre Habouzit, (Sat Sep 8, 8:12 pm)
[PATCH 1/6] Add strbuf_rtrim and strbuf_insert., Pierre Habouzit, (Sat Sep 8, 8:04 pm)
Re: [PATCH 1/6] Add strbuf_rtrim and strbuf_insert., Junio C Hamano, (Sun Sep 9, 6:24 pm)
Re: [PATCH 1/6] Add strbuf_rtrim and strbuf_insert., Pierre Habouzit, (Mon Sep 10, 5:00 am)
new patch 1/6, Pierre Habouzit, (Sun Sep 9, 8:04 am)
Re: [PATCH 1/6] Add strbuf_rtrim and strbuf_insert., Pierre Habouzit, (Sun Sep 9, 7:30 am)
speck-geostationary