Skip to content

Commit d9291ec

Browse files
committed
Merge branch 'rs/pp-user-info-without-extra-allocation'
* rs/pp-user-info-without-extra-allocation: pretty: remove intermediate strbufs from pp_user_info() pretty: simplify output line length calculation in pp_user_info() pretty: simplify input line length calculation in pp_user_info()
2 parents c259a1a + a0511b3 commit d9291ec

File tree

1 file changed

+14
-35
lines changed

1 file changed

+14
-35
lines changed

pretty.c

Lines changed: 14 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -410,10 +410,7 @@ void pp_user_info(const struct pretty_print_context *pp,
410410
const char *what, struct strbuf *sb,
411411
const char *line, const char *encoding)
412412
{
413-
struct strbuf name;
414-
struct strbuf mail;
415413
struct ident_split ident;
416-
int linelen;
417414
char *line_end;
418415
const char *mailbuf, *namebuf;
419416
size_t namelen, maillen;
@@ -422,18 +419,10 @@ void pp_user_info(const struct pretty_print_context *pp,
422419
if (pp->fmt == CMIT_FMT_ONELINE)
423420
return;
424421

425-
line_end = strchr(line, '\n');
426-
if (!line_end) {
427-
line_end = strchr(line, '\0');
428-
if (!line_end)
429-
return;
430-
}
431-
432-
linelen = ++line_end - line;
433-
if (split_ident_line(&ident, line, linelen))
422+
line_end = strchrnul(line, '\n');
423+
if (split_ident_line(&ident, line, line_end - line))
434424
return;
435425

436-
437426
mailbuf = ident.mail_begin;
438427
maillen = ident.mail_end - ident.mail_begin;
439428
namebuf = ident.name_begin;
@@ -442,43 +431,33 @@ void pp_user_info(const struct pretty_print_context *pp,
442431
if (pp->mailmap)
443432
map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
444433

445-
strbuf_init(&mail, 0);
446-
strbuf_init(&name, 0);
447-
448-
strbuf_add(&mail, mailbuf, maillen);
449-
strbuf_add(&name, namebuf, namelen);
450-
451-
namelen = name.len + mail.len + 3; /* ' ' + '<' + '>' */
452-
453434
if (pp->fmt == CMIT_FMT_EMAIL) {
454435
strbuf_addstr(sb, "From: ");
455-
if (needs_rfc2047_encoding(name.buf, name.len, RFC2047_ADDRESS)) {
456-
add_rfc2047(sb, name.buf, name.len,
436+
if (needs_rfc2047_encoding(namebuf, namelen, RFC2047_ADDRESS)) {
437+
add_rfc2047(sb, namebuf, namelen,
457438
encoding, RFC2047_ADDRESS);
458439
max_length = 76; /* per rfc2047 */
459-
} else if (needs_rfc822_quoting(name.buf, name.len)) {
440+
} else if (needs_rfc822_quoting(namebuf, namelen)) {
460441
struct strbuf quoted = STRBUF_INIT;
461-
add_rfc822_quoted(&quoted, name.buf, name.len);
442+
add_rfc822_quoted(&quoted, namebuf, namelen);
462443
strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
463444
-6, 1, max_length);
464445
strbuf_release(&quoted);
465446
} else {
466-
strbuf_add_wrapped_bytes(sb, name.buf, name.len,
447+
strbuf_add_wrapped_bytes(sb, namebuf, namelen,
467448
-6, 1, max_length);
468449
}
469-
if (namelen - name.len + last_line_length(sb) > max_length)
470-
strbuf_addch(sb, '\n');
471450

472-
strbuf_addf(sb, " <%s>\n", mail.buf);
451+
if (max_length <
452+
last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
453+
strbuf_addch(sb, '\n');
454+
strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
473455
} else {
474-
strbuf_addf(sb, "%s: %.*s%s <%s>\n", what,
475-
(pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
476-
" ", name.buf, mail.buf);
456+
strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
457+
(pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, " ",
458+
(int)namelen, namebuf, (int)maillen, mailbuf);
477459
}
478460

479-
strbuf_release(&mail);
480-
strbuf_release(&name);
481-
482461
switch (pp->fmt) {
483462
case CMIT_FMT_MEDIUM:
484463
strbuf_addf(sb, "Date: %s\n",

0 commit comments

Comments
 (0)