Skip to content

Commit a91cc7f

Browse files
rscharfegitster
authored andcommitted
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 <[email protected]> Helped-by: Eric Sunshine <[email protected]> Signed-off-by: René Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0654dc commit a91cc7f

11 files changed

+27
-15
lines changed

builtin/checkout.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -863,7 +863,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
863863
strbuf_addf(&msg, "checkout: moving from %s to %s",
864864
old_desc ? old_desc : "(invalid)", new_branch_info->name);
865865
else
866-
strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg));
866+
strbuf_insertstr(&msg, 0, reflog_msg);
867867

868868
if (!strcmp(new_branch_info->name, "HEAD") && !new_branch_info->path && !opts->force_detach) {
869869
/* Nothing to do. */

builtin/notes.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -622,7 +622,7 @@ static int append_edit(int argc, const char **argv, const char *prefix)
622622

623623
strbuf_grow(&d.buf, size + 1);
624624
if (d.buf.len && prev_buf && size)
625-
strbuf_insert(&d.buf, 0, "\n", 1);
625+
strbuf_insertstr(&d.buf, 0, "\n");
626626
if (prev_buf && size)
627627
strbuf_insert(&d.buf, 0, prev_buf, size);
628628
free(prev_buf);
@@ -745,7 +745,7 @@ static int merge_commit(struct notes_merge_options *o)
745745
memset(&pretty_ctx, 0, sizeof(pretty_ctx));
746746
format_commit_message(partial, "%s", &msg, &pretty_ctx);
747747
strbuf_trim(&msg);
748-
strbuf_insert(&msg, 0, "notes: ", 7);
748+
strbuf_insertstr(&msg, 0, "notes: ");
749749
update_ref(msg.buf, o->local_ref, &oid,
750750
is_null_oid(&parent_oid) ? NULL : &parent_oid,
751751
0, UPDATE_REFS_DIE_ON_ERR);

builtin/sparse-checkout.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
373373
return;
374374

375375
if (line->buf[0] != '/')
376-
strbuf_insert(line, 0, "/", 1);
376+
strbuf_insertstr(line, 0, "/");
377377

378378
insert_recursive_pattern(pl, line);
379379
}

commit.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -993,7 +993,7 @@ static int do_sign_commit(struct strbuf *buf, const char *keyid)
993993
strbuf_insert(buf, inspos, gpg_sig_header, gpg_sig_header_len);
994994
inspos += gpg_sig_header_len;
995995
}
996-
strbuf_insert(buf, inspos++, " ", 1);
996+
strbuf_insertstr(buf, inspos++, " ");
997997
strbuf_insert(buf, inspos, bol, len);
998998
inspos += len;
999999
copypos += len;

config.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ static int prepare_include_condition_pattern(struct strbuf *pat)
204204
strbuf_splice(pat, 0, 1, path.buf, slash - path.buf);
205205
prefix = slash - path.buf + 1 /* slash */;
206206
} else if (!is_absolute_path(pat->buf))
207-
strbuf_insert(pat, 0, "**/", 3);
207+
strbuf_insertstr(pat, 0, "**/");
208208

209209
add_trailing_starstar_for_dir(pat);
210210

http.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,8 @@ static void curl_dump_header(const char *text, unsigned char *ptr, size_t size,
680680
for (header = headers; *header; header++) {
681681
if (hide_sensitive_header)
682682
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), ": ");
685685
strbuf_rtrim((*header));
686686
strbuf_addch((*header), '\n');
687687
trace_strbuf(&trace_curl, (*header));

mailinfo.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ static void handle_content_type(struct mailinfo *mi, struct strbuf *line)
254254
mi->delsp = has_attr_value(line->buf, "delsp=", "yes");
255255

256256
if (slurp_attr(line->buf, "boundary=", boundary)) {
257-
strbuf_insert(boundary, 0, "--", 2);
257+
strbuf_insertstr(boundary, 0, "--");
258258
if (++mi->content_top >= &mi->content[MAX_BOUNDARIES]) {
259259
error("Too many boundaries to handle");
260260
mi->input_error = -1;
@@ -570,7 +570,7 @@ static int check_header(struct mailinfo *mi,
570570
len = strlen("Content-Type: ");
571571
strbuf_add(&sb, line->buf + len, line->len - len);
572572
decode_header(mi, &sb);
573-
strbuf_insert(&sb, 0, "Content-Type: ", len);
573+
strbuf_insertstr(&sb, 0, "Content-Type: ");
574574
handle_content_type(mi, &sb);
575575
ret = 1;
576576
goto check_header_out;

notes-utils.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void commit_notes(struct repository *r, struct notes_tree *t, const char *msg)
5252
strbuf_complete_line(&buf);
5353

5454
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: ");
5656
update_ref(buf.buf, t->update_ref, &commit_oid, NULL, 0,
5757
UPDATE_REFS_DIE_ON_ERR);
5858

notes.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1332,9 +1332,9 @@ void expand_notes_ref(struct strbuf *sb)
13321332
if (starts_with(sb->buf, "refs/notes/"))
13331333
return; /* we're happy */
13341334
else if (starts_with(sb->buf, "notes/"))
1335-
strbuf_insert(sb, 0, "refs/", 5);
1335+
strbuf_insertstr(sb, 0, "refs/");
13361336
else
1337-
strbuf_insert(sb, 0, "refs/notes/", 11);
1337+
strbuf_insertstr(sb, 0, "refs/notes/");
13381338
}
13391339

13401340
void expand_loose_notes_ref(struct strbuf *sb)

pretty.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1581,9 +1581,9 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
15811581
strbuf_setlen(sb, sb->len - 1);
15821582
} else if (orig_len != sb->len) {
15831583
if (magic == ADD_LF_BEFORE_NON_EMPTY)
1584-
strbuf_insert(sb, orig_len, "\n", 1);
1584+
strbuf_insertstr(sb, orig_len, "\n");
15851585
else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1586-
strbuf_insert(sb, orig_len, " ", 1);
1586+
strbuf_insertstr(sb, orig_len, " ");
15871587
}
15881588
return consumed + 1;
15891589
}

strbuf.h

+12
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,18 @@ void strbuf_addchars(struct strbuf *sb, int c, size_t n);
244244
*/
245245
void strbuf_insert(struct strbuf *sb, size_t pos, const void *, size_t);
246246

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+
247259
/**
248260
* Insert data to the given position of the buffer giving a printf format
249261
* string. The contents will be shifted, not overwritten.

0 commit comments

Comments
 (0)