All of lore.kernel.org
 help / color / mirror / Atom feed
From: Pierre Habouzit <madcoder@debian.org>
To: git@vger.kernel.org
Cc: Pierre Habouzit <madcoder@debian.org>
Subject: [PATCH 1/6] Add strbuf_rtrim and strbuf_insert.
Date: Sun,  9 Sep 2007 02:04:31 +0200	[thread overview]
Message-ID: <11892962761617-git-send-email-madcoder@debian.org> (raw)
In-Reply-To: <11892962763548-git-send-email-madcoder@debian.org>

  * 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

  reply	other threads:[~2007-09-09  8:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-09-09  0:04 Use more strbufs series [on top of next] Pierre Habouzit
2007-09-09  0:04 ` Pierre Habouzit [this message]
2007-09-09  0:04   ` [PATCH 2/6] Change semantics of interpolate to work like snprintf Pierre Habouzit
2007-09-09  0:04     ` [PATCH 3/6] Rework pretty_print_commit to use strbufs instead of custom buffers Pierre Habouzit
2007-09-09  0:04       ` [PATCH 4/6] Use strbuf_read in builtin-fetch-tool.c Pierre Habouzit
2007-09-09  0:04         ` [PATCH 5/6] Use strbufs to in read_message (imap-send.c), custom buffer-- Pierre Habouzit
2007-09-09  0:04           ` [PATCH 6/6] Replace all read_fd use with strbuf_read, and get rid of it Pierre Habouzit
     [not found]       ` <7vsl5nflj2.fsf@gitster.siamese.dyndns.org>
     [not found]         ` <20070910090656.GC3417@artemis.corp>
     [not found]           ` <7vir6isvu1.fsf@gitster.siamese.dyndns.org>
     [not found]             ` <7v642iqumt.fsf@gitster.siamese.dyndns.org>
     [not found]               ` <20070910183256.GD32442@artemis.corp>
2007-09-27 22:56                 ` return void expressions in C (Was: [PATCH 3/6] Rework pretty_print_commit ...) Linus Torvalds
2007-09-09  0:12 ` Use more strbufs series [on top of next] Pierre Habouzit

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=11892962761617-git-send-email-madcoder@debian.org \
    --to=madcoder@debian.org \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.