Skip to content

Commit f9b7204

Browse files
Jan H. Schönherrgitster
authored andcommitted
format-patch: introduce helper function last_line_length()
Currently, an open-coded loop to calculate the length of the last line of a string buffer is used in multiple places. Move that code into a function of its own. Signed-off-by: Jan H. Schönherr <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 94f6cdf commit f9b7204

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

pretty.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,17 @@ static int has_rfc822_specials(const char *s, int len)
240240
return 0;
241241
}
242242

243+
static int last_line_length(struct strbuf *sb)
244+
{
245+
int i;
246+
247+
/* How many bytes are already used on the last line? */
248+
for (i = sb->len - 1; i >= 0; i--)
249+
if (sb->buf[i] == '\n')
250+
break;
251+
return sb->len - (i + 1);
252+
}
253+
243254
static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
244255
{
245256
int i;
@@ -275,13 +286,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
275286
static const int max_length = 78; /* per rfc2822 */
276287
static const int max_encoded_length = 76; /* per rfc2047 */
277288
int i;
278-
int line_len;
279-
280-
/* How many bytes are already used on the current line? */
281-
for (i = sb->len - 1; i >= 0; i--)
282-
if (sb->buf[i] == '\n')
283-
break;
284-
line_len = sb->len - (i+1);
289+
int line_len = last_line_length(sb);
285290

286291
for (i = 0; i < len; i++) {
287292
int ch = line[i];
@@ -346,7 +351,6 @@ void pp_user_info(const struct pretty_print_context *pp,
346351
if (pp->fmt == CMIT_FMT_EMAIL) {
347352
char *name_tail = strchr(line, '<');
348353
int display_name_length;
349-
int final_line;
350354
if (!name_tail)
351355
return;
352356
while (line < name_tail && isspace(name_tail[-1]))
@@ -361,10 +365,7 @@ void pp_user_info(const struct pretty_print_context *pp,
361365
add_rfc2047(sb, quoted.buf, quoted.len, encoding);
362366
strbuf_release(&quoted);
363367
}
364-
for (final_line = 0; final_line < sb->len; final_line++)
365-
if (sb->buf[sb->len - final_line - 1] == '\n')
366-
break;
367-
if (namelen - display_name_length + final_line > 78) {
368+
if (namelen - display_name_length + last_line_length(sb) > 78) {
368369
strbuf_addch(sb, '\n');
369370
if (!isspace(name_tail[0]))
370371
strbuf_addch(sb, ' ');

0 commit comments

Comments
 (0)