diff options
author | Michael Haggerty <mhagger@alum.mit.edu> | 2012-11-04 07:46:52 +0100 |
---|---|---|
committer | Jeff King <peff@peff.net> | 2012-11-04 06:46:55 -0500 |
commit | 1173bb331103b7e6d9e95549c7b7be12546d0697 (patch) | |
tree | 20519a5aedba8ac129b00e7c35ddac50556f98ff /strbuf.c | |
parent | b8c2c1fa35f240ea8eee719c0f5a657285864573 (diff) | |
download | git-1173bb331103b7e6d9e95549c7b7be12546d0697.tar.gz git-1173bb331103b7e6d9e95549c7b7be12546d0697.tar.xz |
strbuf_split_buf(): simplify iteration
While iterating, update str and slen to keep track of the part of the
string that hasn't been processed yet rather than computing things
relative to the start of the original string. This eliminates one
local variable, reduces the scope of another, and reduces the amount
of arithmetic needed within the loop.
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: Jeff King <peff@peff.net>
Diffstat (limited to 'strbuf.c')
-rw-r--r-- | strbuf.c | 23 |
1 files changed, 10 insertions, 13 deletions
@@ -110,25 +110,22 @@ struct strbuf **strbuf_split_buf(const char *str, size_t slen, int delim, int ma { struct strbuf **ret = NULL; size_t nr = 0, alloc = 0; - const char *n, *p; struct strbuf *t; - p = n = str; - while (n < str + slen) { - int len; - if (max <= 0 || nr + 1 < max) - n = memchr(n, delim, slen - (n - str)); - else - n = NULL; - if (!n) - n = str + slen - 1; - len = n - p + 1; + while (slen) { + int len = slen; + if (max <= 0 || nr + 1 < max) { + const char *end = memchr(str, delim, slen); + if (end) + len = end - str + 1; + } t = xmalloc(sizeof(struct strbuf)); strbuf_init(t, len); - strbuf_add(t, p, len); + strbuf_add(t, str, len); ALLOC_GROW(ret, nr + 2, alloc); ret[nr++] = t; - p = ++n; + str += len; + slen -= len; } ALLOC_GROW(ret, nr + 1, alloc); /* In case string was empty */ ret[nr] = NULL; |