Thread (31 messages) flat view 31 messages, 2 authors, 2016-06-15

Re: [PATCH v4 11/17] trailer: add new_trailer_item() function

From: Eric Sunshine <hidden>
Date: 2016-06-15 22:59:47

On Thu, Jan 30, 2014 at 1:49 AM, Christian Couder
[off-list ref] wrote:
This is a small refactoring to prepare for the next steps.
Since this is all brand new code, wouldn't it make more sense to
structure it in this fashion in the first place when introduced in
patch 4/17? It's not clear why it should be introduced with poorer
structure and then later cleaned up.
quoted hunk ↗ jump to hunk
Signed-off-by: Christian Couder <redacted>
---
 trailer.c | 31 +++++++++++++++++++------------
 1 file changed, 19 insertions(+), 12 deletions(-)
diff --git a/trailer.c b/trailer.c
index 73a65e0..430ff39 100644
--- a/trailer.c
+++ b/trailer.c
@@ -399,11 +399,27 @@ static void parse_trailer(struct strbuf *tok, struct strbuf *val, const char *tr
        }
 }

+static struct trailer_item *new_trailer_item(struct trailer_item *conf_item,
+                                            const char* tok, const char* val)
+{
+       struct trailer_item *new = xcalloc(sizeof(struct trailer_item), 1);
+       new->value = val;
+
+       if (conf_item) {
+               new->conf = conf_item->conf;
+               new->token = xstrdup(conf_item->conf->key);
+       } else {
+               new->conf = xcalloc(sizeof(struct conf_info), 1);
+               new->token = tok;
+       }
+
+       return new;
+}
+
 static struct trailer_item *create_trailer_item(const char *string)
 {
        struct strbuf tok = STRBUF_INIT;
        struct strbuf val = STRBUF_INIT;
-       struct trailer_item *new;
        struct trailer_item *item;
        int tok_alnum_len;
@@ -415,21 +431,12 @@ static struct trailer_item *create_trailer_item(const char *string)
        for (item = first_conf_item; item; item = item->next) {
                if (!strncasecmp(tok.buf, item->conf->key, tok_alnum_len) ||
                    !strncasecmp(tok.buf, item->conf->name, tok_alnum_len)) {
-                       new = xcalloc(sizeof(struct trailer_item), 1);
-                       new->conf = item->conf;
-                       new->token = xstrdup(item->conf->key);
-                       new->value = strbuf_detach(&val, NULL);
                        strbuf_release(&tok);
-                       return new;
+                       return new_trailer_item(item, NULL, strbuf_detach(&val, NULL));
                }
        }

-       new = xcalloc(sizeof(struct trailer_item), 1);
-       new->conf = xcalloc(sizeof(struct conf_info), 1);
-       new->token = strbuf_detach(&tok, NULL);
-       new->value = strbuf_detach(&val, NULL);
-
-       return new;
+       return new_trailer_item(NULL, strbuf_detach(&tok, NULL), strbuf_detach(&val, NULL));;
 }

 static void add_trailer_item(struct trailer_item **first,
--
1.8.5.2.201.gacc5987
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help