This series cleans up populate_value() in ref-filter, by moving out the parsing part of atoms to separate parsing functions. This ensures that parsing is only done once and also improves the modularity of the code. v1: http://thread.gmane.org/gmane.comp.version-control.git/281180 v2: http://thread.gmane.org/gmane.comp.version-control.git/282563 v3: http://thread.gmane.org/gmane.comp.version-control.git/283350 v4: http://thread.gmane.org/gmane.comp.version-control.git/285158 v5: http://thread.gmane.org/gmane.comp.version-control.git/286414 Changes: * As per Jeff's suggestion ($gmane/286425) we drop creation and usage of strbuf_split_str_omit_term() and use string_list_split() instead. Jeff King (1): ref-filter: use string_list_split over strbuf_split Karthik Nayak (10): ref-filter: bump 'used_atom' and related code to the top ref-filter: introduce struct used_atom ref-filter: introduce parsing functions for each valid atom ref-filter: introduce color_atom_parser() ref-filter: introduce parse_align_position() ref-filter: introduce align_atom_parser() ref-filter: align: introduce long-form syntax ref-filter: introduce remote_ref_atom_parser() ref-filter: introduce contents_atom_parser() ref-filter: introduce objectname_atom_parser() Documentation/git-for-each-ref.txt | 20 +- ref-filter.c | 434 +++++++++++++++++++++---------------- t/t6302-for-each-ref-filter.sh | 42 ++++ 3 files changed, 304 insertions(+), 192 deletions(-) Interdiff: diff --git a/ref-filter.c b/ref-filter.c index 21f4937..d13d002 100644 --- a/ref-filter.c +++ b/ref-filter.c @@ -127,18 +127,19 @@ static align_type parse_align_position(const char *s) static void align_atom_parser(struct used_atom *atom, const char *arg) { struct align *align = &atom->u.align; - struct strbuf **v, **to_free; + struct string_list params = STRING_LIST_INIT_DUP; + int i; unsigned int width = ~0U; if (!arg) die(_("expected format: %%(align:<width>,<position>)")); - v = to_free = strbuf_split_str_omit_term(arg, ',', 0); align->position = ALIGN_LEFT; - while (*v) { + string_list_split(¶ms, arg, ',', -1); + for (i = 0; i < params.nr; i++) { + const char *s = params.items[i].string; int position; - const char *s = v[0]->buf; if (skip_prefix(s, "position=", &s)) { position = parse_align_position(s); @@ -154,13 +155,12 @@ static void align_atom_parser(struct used_atom *atom, const char *arg) align->position = position; else die(_("unrecognized %%(align) argument: %s"), s); - v++; } if (width == ~0U) die(_("positive width expected with the %%(align) atom")); align->width = width; - strbuf_list_free(to_free); + string_list_clear(¶ms, 0); } static struct { diff --git a/strbuf.c b/strbuf.c index 4a93e2a..bab316d 100644 --- a/strbuf.c +++ b/strbuf.c @@ -115,7 +115,7 @@ void strbuf_tolower(struct strbuf *sb) } struct strbuf **strbuf_split_buf(const char *str, size_t slen, - int terminator, int max, int omit_term) + int terminator, int max) { struct strbuf **ret = NULL; size_t nr = 0, alloc = 0; @@ -123,18 +123,14 @@ struct strbuf **strbuf_split_buf(const char *str, size_t slen, while (slen) { int len = slen; - int copylen = len; - const char *end = NULL; if (max <= 0 || nr + 1 < max) { - end = memchr(str, terminator, slen); - if (end) { + const char *end = memchr(str, terminator, slen); + if (end) len = end - str + 1; - copylen = len - !!omit_term; - } } t = xmalloc(sizeof(struct strbuf)); - strbuf_init(t, copylen); - strbuf_add(t, str, copylen); + strbuf_init(t, len); + strbuf_add(t, str, len); ALLOC_GROW(ret, nr + 2, alloc); ret[nr++] = t; str += len; diff --git a/strbuf.h b/strbuf.h index 6115e72..f72fd14 100644 --- a/strbuf.h +++ b/strbuf.h @@ -466,12 +466,11 @@ static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) /** * Split str (of length slen) at the specified terminator character. * Return a null-terminated array of pointers to strbuf objects - * holding the substrings. If omit_term is true, the terminator will - * be stripped from all substrings. Otherwise, substrings will include - * the terminator, except for the final substring, if the original - * string lacked a terminator. If max is positive, then split the - * string into at most max substrings (with the last substring - * containing everything following the (max-1)th terminator + * holding the substrings. The substrings include the terminator, + * except for the last substring, which might be unterminated if the + * original string did not end with a terminator. If max is positive, + * then split the string into at most max substrings (with the last + * substring containing everything following the (max-1)th terminator * character). * * The most generic form is `strbuf_split_buf`, which takes an arbitrary @@ -482,25 +481,19 @@ static inline int strbuf_strip_suffix(struct strbuf *sb, const char *suffix) * For lighter-weight alternatives, see string_list_split() and * string_list_split_in_place(). */ -extern struct strbuf **strbuf_split_buf(const char *str, size_t slen, - int terminator, int max, int omit_term); - -static inline struct strbuf **strbuf_split_str_omit_term(const char *str, - int terminator, int max) -{ - return strbuf_split_buf(str, strlen(str), terminator, max, 1); -} +extern struct strbuf **strbuf_split_buf(const char *, size_t, + int terminator, int max); static inline struct strbuf **strbuf_split_str(const char *str, int terminator, int max) { - return strbuf_split_buf(str, strlen(str), terminator, max, 0); + return strbuf_split_buf(str, strlen(str), terminator, max); } static inline struct strbuf **strbuf_split_max(const struct strbuf *sb, int terminator, int max) { - return strbuf_split_buf(sb->buf, sb->len, terminator, max, 0); + return strbuf_split_buf(sb->buf, sb->len, terminator, max); } static inline struct strbuf **strbuf_split(const struct strbuf *sb, -- 2.7.1 -- To unsubscribe from this list: send the line "unsubscribe git" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html