X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=strbuf.c;h=1a7df12e8f233863cd931a960d453d54050f5584;hb=fa92f3233c65251c3311f0834526ce96a1963554;hp=09c43ae59a7d4715c26f13d72ca37bc759d1c76d;hpb=2cd517cdd3f52bcfb5a87e6991560cc000b11089;p=git.git diff --git a/strbuf.c b/strbuf.c index 09c43ae59..1a7df12e8 100644 --- a/strbuf.c +++ b/strbuf.c @@ -103,24 +103,27 @@ void strbuf_ltrim(struct strbuf *sb) sb->buf[sb->len] = '\0'; } -struct strbuf **strbuf_split(const struct strbuf *sb, int delim) +struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int max) { int alloc = 2, pos = 0; - char *n, *p; + const char *n, *p; struct strbuf **ret; struct strbuf *t; ret = xcalloc(alloc, sizeof(struct strbuf *)); - p = n = sb->buf; - while (n < sb->buf + sb->len) { + p = n = str; + while (n < str + slen) { int len; - n = memchr(n, delim, sb->len - (n - sb->buf)); + if (max <= 0 || pos + 1 < max) + n = memchr(n, delim, slen - (n - str)); + else + n = NULL; if (pos + 1 >= alloc) { alloc = alloc * 2; ret = xrealloc(ret, sizeof(struct strbuf *) * alloc); } if (!n) - n = sb->buf + sb->len - 1; + n = str + slen - 1; len = n - p + 1; t = xmalloc(sizeof(struct strbuf)); strbuf_init(t, len);