git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 4/7] format-patch: introduce helper function last_line_length()

From
JSJan H. Schönherr <schnhrr@cs.tu-berlin.de>
Date
Oct 18, 2012, 14:43 UTC
Message-ID
<1350571414-17907-5-git-send-email-schnhrr@cs.tu-berlin.de>
In-Reply-To
<1350571414-17907-1-git-send-email-schnhrr@cs.tu-berlin.de>
From: Jan H. Schönherr <schnhrr@cs.tu-berlin.de>

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 <schnhrr@cs.tu-berlin.de>
---
 pretty.c | 25 +++++++++++++------------
 1 Datei geändert, 13 Zeilen hinzugefügt(+), 12 Zeilen entfernt(-)
diff --git a/pretty.c b/pretty.c
index da75879..482402d 100644
--- a/pretty.c
+++ b/pretty.c
@@ -240,6 +240,17 @@ static int has_rfc822_specials(const char *s, int len)
 	return 0;
 }
 
+static int last_line_length(struct strbuf *sb)
+{
+	int i;
+
+	/* How many bytes are already used on the last line? */
+	for (i = sb->len - 1; i >= 0; i--)
+		if (sb->buf[i] == '\n')
+			break;
+	return sb->len - (i + 1);
+}
+
 static void add_rfc822_quoted(struct strbuf *out, const char *s, int len)
 {
 	int i;
@@ -275,13 +286,7 @@ static void add_rfc2047(struct strbuf *sb, const char *line, int len,
 	static const int max_length = 78; /* per rfc2822 */
 	static const int max_encoded_length = 76; /* per rfc2047 */
 	int i;
-	int line_len;
-
-	/* How many bytes are already used on the current line? */
-	for (i = sb->len - 1; i >= 0; i--)
-		if (sb->buf[i] == '\n')
-			break;
-	line_len = sb->len - (i+1);
+	int line_len = last_line_length(sb);
 
 	for (i = 0; i < len; i++) {
 		int ch = line[i];
@@ -346,7 +351,6 @@ void pp_user_info(const struct pretty_print_context *pp,
 	if (pp->fmt == CMIT_FMT_EMAIL) {
 		char *name_tail = strchr(line, '<');
 		int display_name_length;
-		int final_line;
 		if (!name_tail)
 			return;
 		while (line < name_tail && isspace(name_tail[-1]))
@@ -361,10 +365,7 @@ void pp_user_info(const struct pretty_print_context *pp,
 			add_rfc2047(sb, quoted.buf, quoted.len, encoding);
 			strbuf_release(&quoted);
 		}
-		for (final_line = 0; final_line < sb->len; final_line++)
-			if (sb->buf[sb->len - final_line - 1] == '\n')
-				break;
-		if (namelen - display_name_length + final_line > 78) {
+		if (namelen - display_name_length + last_line_length(sb) > 78) {
 			strbuf_addch(sb, '\n');
 			if (!isspace(name_tail[0]))
 				strbuf_addch(sb, ' ');
-- 
1.7.12
Previous: Jan H. SchönherrNext: Jan H. Schönherr
Message 5 of 8 in “Cure some format-patch wrapping and encoding issues”
  1. 0/7 Cure some format-patch wrapping and encoding issuesJan H. Schönherr, Oct 18, 2012
  2. 1/7 utf8: fix off-by-one wrapping of textJan H. Schönherr, Oct 18, 2012
  3. 2/7 format-patch: do not wrap non-rfc2047 headers too earlyJan H. Schönherr, Oct 18, 2012
  4. 3/7 format-patch: do not wrap rfc2047 encoded headers too lateJan H. Schönherr, Oct 18, 2012
  5. 4/7 format-patch: introduce helper function last_line_length()Jan H. Schönherr, Oct 18, 2012
  6. 5/7 format-patch: make rfc2047 encoding more strictJan H. Schönherr, Oct 18, 2012
  7. 6/7 format-patch: fix rfc2047 address encoding with respect to rfc822 specialsJan H. Schönherr, Oct 18, 2012
  8. 7/7 format-patch tests: check quoting/encoding in To: and Cc: headersJan H. Schönherr, Oct 18, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.