[PATCH 1/7] strbuf: add "include_delim" parameter to "strbuf_split"
From: Christian Couder <hidden>
Date: 2016-06-15 22:46:22
Subsystem:
the rest · Maintainer:
Linus Torvalds
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 <redacted> --- 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