git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/7] strbuf: add "include_delim" parameter to "strbuf_split"
@ 2009-03-12  7:51 Christian Couder
       [not found] ` <20090312190846.6117@nanako3.lavabit.com>
  0 siblings, 1 reply; 9+ messages in thread
From: Christian Couder @ 2009-03-12  7:51 UTC (permalink / raw)
  To: Junio C Hamano
  Cc: git, Ingo Molnar, John Tapsell, Johannes Schindelin,
	Pierre Habouzit

The "strbuf_split" function used to include the delimiter character
at the end of the splited strbufs it produced.

This behavior is not wanted in many cases, so this patch adds a new
"include_delim" parameter to the function to let us switch it on or
off as we want.

Signed-off-by: Christian Couder <chriscool@tuxfamily.org>
---
 builtin-mailinfo.c |    2 +-
 imap-send.c        |    2 +-
 strbuf.c           |    8 +++++---
 strbuf.h           |    2 +-
 4 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/builtin-mailinfo.c b/builtin-mailinfo.c
index 2789ccd..b96ea1a 100644
--- a/builtin-mailinfo.c
+++ b/builtin-mailinfo.c
@@ -814,7 +814,7 @@ static void handle_body(void)
 			 * multiple new lines.  Pass only one chunk
 			 * at a time to handle_filter()
 			 */
-			lines = strbuf_split(&line, '\n');
+			lines = strbuf_split(&line, '\n', 1);
 			for (it = lines; (sb = *it); it++) {
 				if (*(it + 1) == NULL) /* The last line */
 					if (sb->buf[sb->len - 1] != '\n') {
diff --git a/imap-send.c b/imap-send.c
index cb518eb..a27f744 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1289,7 +1289,7 @@ static void wrap_in_html(struct msg_data *msg)
 	int added_header = 0;
 
 	strbuf_attach(&buf, msg->data, msg->len, msg->len);
-	lines = strbuf_split(&buf, '\n');
+	lines = strbuf_split(&buf, '\n', 1);
 	strbuf_release(&buf);
 	for (p = lines; *p; p++) {
 		if (! added_header) {
diff --git a/strbuf.c b/strbuf.c
index 6ed0684..4ef9084 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -97,7 +97,9 @@ void strbuf_tolower(struct strbuf *sb)
 		sb->buf[i] = tolower(sb->buf[i]);
 }
 
-struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
+struct strbuf **strbuf_split(const struct strbuf *sb,
+			     int delim,
+			     int include_delim)
 {
 	int alloc = 2, pos = 0;
 	char *n, *p;
@@ -114,8 +116,8 @@ struct strbuf **strbuf_split(const struct strbuf *sb, int delim)
 			ret = xrealloc(ret, sizeof(struct strbuf *) * alloc);
 		}
 		if (!n)
-			n = sb->buf + sb->len - 1;
-		len = n - p + 1;
+			n = sb->buf + sb->len - (include_delim ? 1 : 0);
+		len = n - p + (include_delim ? 1 : 0);
 		t = xmalloc(sizeof(struct strbuf));
 		strbuf_init(t, len);
 		strbuf_add(t, p, len);
diff --git a/strbuf.h b/strbuf.h
index 89bd36e..2202d28 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -83,7 +83,7 @@ extern void strbuf_ltrim(struct strbuf *);
 extern int strbuf_cmp(const struct strbuf *, const struct strbuf *);
 extern void strbuf_tolower(struct strbuf *);
 
-extern struct strbuf **strbuf_split(const struct strbuf *, int delim);
+extern struct strbuf **strbuf_split(const struct strbuf *, int delim, int include_delim);
 extern void strbuf_list_free(struct strbuf **);
 
 /*----- add data in your buffer -----*/
-- 
1.6.2.83.g012a16.dirty

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-03-14 12:09 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-12  7:51 [PATCH 1/7] strbuf: add "include_delim" parameter to "strbuf_split" Christian Couder
     [not found] ` <20090312190846.6117@nanako3.lavabit.com>
2009-03-13  4:48   ` Christian Couder
2009-03-13  5:17     ` Junio C Hamano
2009-03-13  6:02       ` Christian Couder
2009-03-13  6:53         ` Junio C Hamano
2009-03-14  7:46           ` Migrate bisect to C (was: [PATCH 1/7] strbuf: add "include_delim" parameter to "strbuf_split") Christian Couder
2009-03-14  8:16             ` Migrate bisect to C Junio C Hamano
2009-03-14 12:09               ` fetch--tool, was " Johannes Schindelin
2009-03-13  7:06         ` [PATCH 1/7] strbuf: add "include_delim" parameter to "strbuf_split" Junio C Hamano

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).