]> git.ipfire.org Git - thirdparty/git.git/commitdiff
strbuf: add and use strbuf_insertstr()
authorRené Scharfe <l.s.r@web.de>
Sun, 9 Feb 2020 13:44:23 +0000 (14:44 +0100)
committerJunio C Hamano <gitster@pobox.com>
Mon, 10 Feb 2020 17:04:45 +0000 (09:04 -0800)
Add a function for inserting a C string into a strbuf.  Use it
throughout the source to get rid of magic string length constants and
explicit strlen() calls.

Like strbuf_addstr(), implement it as an inline function to avoid the
implicit strlen() calls to cause runtime overhead.

Helped-by: Taylor Blau <me@ttaylorr.com>
Helped-by: Eric Sunshine <sunshine@sunshineco.com>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/checkout.c
builtin/notes.c
builtin/sparse-checkout.c
commit.c
config.c
http.c
mailinfo.c
notes-utils.c
notes.c
pretty.c
strbuf.h

index b52c490c8f5404e24c05db289fc22539faf276e3..2cdc6cdec9768754a83cb9e22a3a63f14c4ea685 100644 (file)
@@ -863,7 +863,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
                strbuf_addf(&msg, "checkout: moving from %s to %s",
                        old_desc ? old_desc : "(invalid)", new_branch_info->name);
        else
-               strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg));
+               strbuf_insertstr(&msg, 0, reflog_msg);
 
        if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) {
                /* Nothing to do. */
index 95456f316549c9a5890a4539446c643640429eb0..35e468ea2d2fb24e9985e0311f9beea31b5eb7a2 100644 (file)
@@ -622,7 +622,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
 
                strbuf_grow(&d.buf, size + 1);
                if (d.buf.len && prev_buf && size)
-                       strbuf_insert(&d.buf, 0, "\n", 1);
+                       strbuf_insertstr(&d.buf, 0, "\n");
                if (prev_buf && size)
                        strbuf_insert(&d.buf, 0, prev_buf, size);
                free(prev_buf);
@@ -745,7 +745,7 @@ static int merge_commit(struct notes_merge_options *o)
        memset(&pretty_ctx, 0, sizeof(pretty_ctx));
        format_commit_message(partial, "%s", &msg, &pretty_ctx);
        strbuf_trim(&msg);
-       strbuf_insert(&msg, 0, "notes: ", 7);
+       strbuf_insertstr(&msg, 0, "notes: ");
        update_ref(msg.buf, o->local_ref, &oid,
                   is_null_oid(&parent_oid) ? NULL : &parent_oid,
                   0, UPDATE_REFS_DIE_ON_ERR);
index b3bed891cb15e9a97a587cf2a6dab49f309578d2..38d0d944b38a53738b67ae057b04de7c567f9c4d 100644 (file)
@@ -373,7 +373,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
                return;
 
        if (line->buf[0] != '/')
-               strbuf_insert(line, 0, "/", 1);
+               strbuf_insertstr(line, 0, "/");
 
        insert_recursive_pattern(pl, line);
 }
index 434ec030d6b2a0074c46c376c6322c92fd5c8a39..83e1190ad91712a3171b25e57703e4ab88e5e13d 100644 (file)
--- a/commit.c
+++ b/commit.c
@@ -993,7 +993,7 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
                        strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
                        inspos += gpg_sig_header_len;
                }
-               strbuf_insert(buf, inspos++, " ", 1);
+               strbuf_insertstr(buf, inspos++, " ");
                strbuf_insert(buf, inspos, bol, len);
                inspos += len;
                copypos += len;
index d75f88ca0ce31f1a9b6b3a8a2198c4f94a4196b5..b386c0c6287739d11446873426f7c483f2537745 100644 (file)
--- a/config.c
+++ b/config.c
@@ -204,7 +204,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
                strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
                prefix = slash - path.buf + 1 /* slash */;
        } else if (!is_absolute_path(pat->buf))
-               strbuf_insert(pat, 0, "**/", 3);
+               strbuf_insertstr(pat, 0, "**/");
 
        add_trailing_starstar_for_dir(pat);
 
diff --git a/http.c b/http.c
index 5f348169c3cdcafbfdc561a468996f7118ce3a56..00a0e507633b3a464e0ef3c7bdd6c04e044f441a 100644 (file)
--- a/http.c
+++ b/http.c
@@ -680,8 +680,8 @@ static void curl_dump_header(const char *text, unsigned char *ptr, size_t size,
        for (header = headers; *header; header++) {
                if (hide_sensitive_header)
                        redact_sensitive_header(*header);
-               strbuf_insert((*header), 0, text, strlen(text));
-               strbuf_insert((*header), strlen(text), ": ", 2);
+               strbuf_insertstr((*header), 0, text);
+               strbuf_insertstr((*header), strlen(text), ": ");
                strbuf_rtrim((*header));
                strbuf_addch((*header), '\n');
                trace_strbuf(&trace_curl, (*header));
index b395adbdf2a405567bc40c254125265f30b12bbd..543962d40cdd028493674d92791bdead37342069 100644 (file)
@@ -254,7 +254,7 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
        mi->delsp = has_attr_value(line->buf, "delsp=", "yes");
 
        if (slurp_attr(line->buf, "boundary=", boundary)) {
-               strbuf_insert(boundary, 0, "--", 2);
+               strbuf_insertstr(boundary, 0, "--");
                if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
                        error("Too many boundaries to handle");
                        mi->input_error = -1;
@@ -570,7 +570,7 @@ static int check_header(struct mailinfo *mi,
                len = strlen("Content-Type: ");
                strbuf_add(&sb, line->buf + len, line->len - len);
                decode_header(mi, &sb);
-               strbuf_insert(&sb, 0, "Content-Type: ", len);
+               strbuf_insertstr(&sb, 0, "Content-Type: ");
                handle_content_type(mi, &sb);
                ret = 1;
                goto check_header_out;
index a8194106987388a590b10e246327f49fd550f448..4bf4888d8c1f2c1b6a08ae75dd6dcd10a8814d1d 100644 (file)
@@ -52,7 +52,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
        strbuf_complete_line(&buf);
 
        create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
-       strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
+       strbuf_insertstr(&buf, 0, "notes: ");
        update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
                   UPDATE_REFS_DIE_ON_ERR);
 
diff --git a/notes.c b/notes.c
index 0c79964c26a71d6843c43c94fe260131334749f2..a24af53de6939ec9f743bb2ab46a56fce152d55f 100644 (file)
--- a/notes.c
+++ b/notes.c
@@ -1332,9 +1332,9 @@ void expand_notes_ref(struct strbuf *sb)
        if (starts_with(sb->buf, "refs/notes/"))
                return; /* we're happy */
        else if (starts_with(sb->buf, "notes/"))
-               strbuf_insert(sb, 0, "refs/", 5);
+               strbuf_insertstr(sb, 0, "refs/");
        else
-               strbuf_insert(sb, 0, "refs/notes/", 11);
+               strbuf_insertstr(sb, 0, "refs/notes/");
 }
 
 void expand_loose_notes_ref(struct strbuf *sb)
index 305e903192a7ae4fb125090436130f434bd46e7c..1cd6e93e47eea577525f6cb9a15ee7554ec153f2 100644 (file)
--- a/pretty.c
+++ b/pretty.c
@@ -1581,9 +1581,9 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
                        strbuf_setlen(sb, sb->len - 1);
        } else if (orig_len != sb->len) {
                if (magic == ADD_LF_BEFORE_NON_EMPTY)
-                       strbuf_insert(sb, orig_len, "\n", 1);
+                       strbuf_insertstr(sb, orig_len, "\n");
                else if (magic == ADD_SP_BEFORE_NON_EMPTY)
-                       strbuf_insert(sb, orig_len, " ", 1);
+                       strbuf_insertstr(sb, orig_len, " ");
        }
        return consumed + 1;
 }
index bfa66569a4bffd063578477e68010a5ccc14f5a3..aae7ac3a82da1adbe4df82e915d47bc14f4bd033 100644 (file)
--- a/strbuf.h
+++ b/strbuf.h
@@ -244,6 +244,18 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);
  */
 void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
 
+/**
+ * Insert a NUL-terminated string to the given position of the buffer.
+ * The remaining contents will be shifted, not overwritten.  It's an
+ * inline function to allow the compiler to resolve strlen() calls on
+ * constants at compile time.
+ */
+static inline void strbuf_insertstr(struct strbuf *sb, size_t pos,
+                                   const char *s)
+{
+       strbuf_insert(sb, pos, s, strlen(s));
+}
+
 /**
  * Insert data to the given position of the buffer giving a printf format
  * string. The contents will be shifted, not overwritten.