@matheusbsbs / matheus-bagliano / commits / a91cc7fad0
Fork 0

strbuf: add and use strbuf_insertstr()

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>

René Scharfe committed Feb 9, 2020 at 14:44 UTC a91cc7fad0d48984135abe2fb70c41db61b500c5
11 files changed +27 -15
builtin/checkout.c
+1 -1
@@ -863,7 +863,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
863 strbuf_addf(&msg, "checkout: moving from %s to %s",
864 old_desc ? old_desc : "(invalid)", new_branch_info->name);
865 else
866 - strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg));
866 + strbuf_insertstr(&msg, 0, reflog_msg);
867
868 if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) {
869 /* Nothing to do. */
builtin/notes.c
+2 -2
@@ -622,7 +622,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
622
623 strbuf_grow(&d.buf, size + 1);
624 if (d.buf.len && prev_buf && size)
625 - strbuf_insert(&d.buf, 0, "\n", 1);
625 + strbuf_insertstr(&d.buf, 0, "\n");
626 if (prev_buf && size)
627 strbuf_insert(&d.buf, 0, prev_buf, size);
628 free(prev_buf);
@@ -745,7 +745,7 @@ static int merge_commit(struct notes_merge_options *o)
745 memset(&pretty_ctx, 0, sizeof(pretty_ctx));
746 format_commit_message(partial, "%s", &msg, &pretty_ctx);
747 strbuf_trim(&msg);
748 - strbuf_insert(&msg, 0, "notes: ", 7);
748 + strbuf_insertstr(&msg, 0, "notes: ");
749 update_ref(msg.buf, o->local_ref, &oid,
750 is_null_oid(&parent_oid) ? NULL : &parent_oid,
751 0, UPDATE_REFS_DIE_ON_ERR);
builtin/sparse-checkout.c
+1 -1
@@ -373,7 +373,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
373 return;
374
375 if (line->buf[0] != '/')
376 - strbuf_insert(line, 0, "/", 1);
376 + strbuf_insertstr(line, 0, "/");
377
378 insert_recursive_pattern(pl, line);
379 }
commit.c
+1 -1
@@ -993,7 +993,7 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
993 strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
994 inspos += gpg_sig_header_len;
995 }
996 - strbuf_insert(buf, inspos++, " ", 1);
996 + strbuf_insertstr(buf, inspos++, " ");
997 strbuf_insert(buf, inspos, bol, len);
998 inspos += len;
999 copypos += len;
config.c
+1 -1
@@ -204,7 +204,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
204 strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
205 prefix = slash - path.buf + 1 /* slash */;
206 } else if (!is_absolute_path(pat->buf))
207 - strbuf_insert(pat, 0, "**/", 3);
207 + strbuf_insertstr(pat, 0, "**/");
208
209 add_trailing_starstar_for_dir(pat);
210
http.c
+2 -2
@@ -680,8 +680,8 @@ static void curl_dump_header(const char *text, unsigned char *ptr, size_t size,
680 for (header = headers; *header; header++) {
681 if (hide_sensitive_header)
682 redact_sensitive_header(*header);
683 - strbuf_insert((*header), 0, text, strlen(text));
684 - strbuf_insert((*header), strlen(text), ": ", 2);
683 + strbuf_insertstr((*header), 0, text);
684 + strbuf_insertstr((*header), strlen(text), ": ");
685 strbuf_rtrim((*header));
686 strbuf_addch((*header), '\n');
687 trace_strbuf(&trace_curl, (*header));
mailinfo.c
+2 -2
@@ -254,7 +254,7 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
254 mi->delsp = has_attr_value(line->buf, "delsp=", "yes");
255
256 if (slurp_attr(line->buf, "boundary=", boundary)) {
257 - strbuf_insert(boundary, 0, "--", 2);
257 + strbuf_insertstr(boundary, 0, "--");
258 if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
259 error("Too many boundaries to handle");
260 mi->input_error = -1;
@@ -570,7 +570,7 @@ static int check_header(struct mailinfo *mi,
570 len = strlen("Content-Type: ");
571 strbuf_add(&sb, line->buf + len, line->len - len);
572 decode_header(mi, &sb);
573 - strbuf_insert(&sb, 0, "Content-Type: ", len);
573 + strbuf_insertstr(&sb, 0, "Content-Type: ");
574 handle_content_type(mi, &sb);
575 ret = 1;
576 goto check_header_out;
notes-utils.c
+1 -1
@@ -52,7 +52,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
52 strbuf_complete_line(&buf);
53
54 create_notes_commit(r, t, NULL, buf.buf, buf.len, &commit_oid);
55 - strbuf_insert(&buf, 0, "notes: ", 7); /* commit message starts at index 7 */
55 + strbuf_insertstr(&buf, 0, "notes: ");
56 update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
57 UPDATE_REFS_DIE_ON_ERR);
58
notes.c
+2 -2
@@ -1332,9 +1332,9 @@ void expand_notes_ref(struct strbuf *sb)
1332 if (starts_with(sb->buf, "refs/notes/"))
1333 return; /* we're happy */
1334 else if (starts_with(sb->buf, "notes/"))
1335 - strbuf_insert(sb, 0, "refs/", 5);
1335 + strbuf_insertstr(sb, 0, "refs/");
1336 else
1337 - strbuf_insert(sb, 0, "refs/notes/", 11);
1337 + strbuf_insertstr(sb, 0, "refs/notes/");
1338 }
1339
1340 void expand_loose_notes_ref(struct strbuf *sb)
pretty.c
+2 -2
@@ -1581,9 +1581,9 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
1581 strbuf_setlen(sb, sb->len - 1);
1582 } else if (orig_len != sb->len) {
1583 if (magic == ADD_LF_BEFORE_NON_EMPTY)
1584 - strbuf_insert(sb, orig_len, "\n", 1);
1584 + strbuf_insertstr(sb, orig_len, "\n");
1585 else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1586 - strbuf_insert(sb, orig_len, " ", 1);
1586 + strbuf_insertstr(sb, orig_len, " ");
1587 }
1588 return consumed + 1;
1589 }
strbuf.h
+12
@@ -244,6 +244,18 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);
244 */
245 void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
246
247 +/**
248 + * Insert a NUL-terminated string to the given position of the buffer.
249 + * The remaining contents will be shifted, not overwritten. It's an
250 + * inline function to allow the compiler to resolve strlen() calls on
251 + * constants at compile time.
252 + */
253 +static inline void strbuf_insertstr(struct strbuf *sb, size_t pos,
254 + const char *s)
255 +{
256 + strbuf_insert(sb, pos, s, strlen(s));
257 +}
258 +
259 /**
260 * Insert data to the given position of the buffer giving a printf format
261 * string. The contents will be shifted, not overwritten.