Skip to content

Commit 9f3f387

Browse files
committed
Merge branch 'rs/strbuf-insertstr'
Code clean-up. * rs/strbuf-insertstr: mailinfo: don't insert header prefix for handle_content_type() strbuf: add and use strbuf_insertstr()
2 parents cbecc16 + 517b605 commit 9f3f387

11 files changed

+26
-15
lines changed

builtin/checkout.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,7 @@ static void update_refs_for_switch(const struct checkout_opts *opts,
865865
strbuf_addf(&msg, "checkout: moving from %s to %s",
866866
old_desc ? old_desc : "(invalid)", new_branch_info->name);
867867
else
868-
strbuf_insert(&msg, 0, reflog_msg, strlen(reflog_msg));
868+
strbuf_insertstr(&msg, 0, reflog_msg);
869869

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

builtin/notes.c

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ static void strbuf_to_cone_pattern(struct strbuf *line, struct pattern_list *pl)
398398
return;
399399

400400
if (line->buf[0] != '/')
401-
strbuf_insert(line, 0, "/", 1);
401+
strbuf_insertstr(line, 0, "/");
402402

403403
insert_recursive_pattern(pl, line);
404404
}

commit.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 2 deletions
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,6 @@ 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);
574573
handle_content_type(mi, &sb);
575574
ret = 1;
576575
goto check_header_out;

notes-utils.c

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,9 +1334,9 @@ void expand_notes_ref(struct strbuf *sb)
13341334
if (starts_with(sb->buf, "refs/notes/"))
13351335
return; /* we're happy */
13361336
else if (starts_with(sb->buf, "notes/"))
1337-
strbuf_insert(sb, 0, "refs/", 5);
1337+
strbuf_insertstr(sb, 0, "refs/");
13381338
else
1339-
strbuf_insert(sb, 0, "refs/notes/", 11);
1339+
strbuf_insertstr(sb, 0, "refs/notes/");
13401340
}
13411341

13421342
void expand_loose_notes_ref(struct strbuf *sb)

pretty.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,9 +1609,9 @@ static size_t format_commit_item(struct strbuf *sb, /* in UTF-8 */
16091609
strbuf_setlen(sb, sb->len - 1);
16101610
} else if (orig_len != sb->len) {
16111611
if (magic == ADD_LF_BEFORE_NON_EMPTY)
1612-
strbuf_insert(sb, orig_len, "\n", 1);
1612+
strbuf_insertstr(sb, orig_len, "\n");
16131613
else if (magic == ADD_SP_BEFORE_NON_EMPTY)
1614-
strbuf_insert(sb, orig_len, " ", 1);
1614+
strbuf_insertstr(sb, orig_len, " ");
16151615
}
16161616
return consumed + 1;
16171617
}

strbuf.h

Lines changed: 12 additions & 0 deletions
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)