From: Karthik Nayak <karthik.188@gmail.com>
To: git@vger.kernel.org
Cc: gitster@pobox.com, sunshine@sunshineco.com,
Karthik Nayak <Karthik.188@gmail.com>
Subject: [PATCH v6 00/11] ref-filter: use parsing functions
Date: Wed, 17 Feb 2016 23:36:08 +0530 [thread overview]
Message-ID: <1455732379-22479-1-git-send-email-Karthik.188@gmail.com> (raw)
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
next reply other threads:[~2016-02-17 18:05 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-02-17 18:06 Karthik Nayak [this message]
2016-02-17 18:06 ` [PATCH v6 01/11] ref-filter: use string_list_split over strbuf_split Karthik Nayak
2016-02-17 22:11 ` Eric Sunshine
2016-02-17 22:14 ` Jeff King
2016-02-17 22:19 ` Junio C Hamano
2016-02-17 22:21 ` Eric Sunshine
2016-02-17 22:26 ` Jeff King
2016-02-18 14:19 ` Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 02/11] ref-filter: bump 'used_atom' and related code to the top Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 03/11] ref-filter: introduce struct used_atom Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 04/11] ref-filter: introduce parsing functions for each valid atom Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 05/11] ref-filter: introduce color_atom_parser() Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 06/11] ref-filter: introduce parse_align_position() Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 07/11] ref-filter: introduce align_atom_parser() Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 08/11] ref-filter: align: introduce long-form syntax Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 09/11] ref-filter: introduce remote_ref_atom_parser() Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 10/11] ref-filter: introduce contents_atom_parser() Karthik Nayak
2016-02-17 18:06 ` [PATCH v6 11/11] ref-filter: introduce objectname_atom_parser() Karthik Nayak
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1455732379-22479-1-git-send-email-Karthik.188@gmail.com \
--to=karthik.188@gmail.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=sunshine@sunshineco.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).