threads / patch / 33609

patch, 3 partspretty: simplify input line length calculation in pp_user_info()

Subject: [PATCH 1/3] pretty: simplify input line length calculation in pp_user_info()

## tl;dr

3 messages between Apr 25, 2013 and Apr 25, 2013. Diffs are folded; open one to read it.

replies: 2people: 1as markdown or json

René Scharfe· Apr 25, 2013, 19:40 UTC · lore

Instead of searching for LF and NUL with two strchr() calls use a single strchrnul() call. We don't need to check if the returned pointer is NULL because either we'll find the NUL at the end of line, or the caller forgot to NUL-terminate the string and we'll overrun the buffer in any case. Also we don't need to pass LF or NUL to split_ident_line() as it ignores it anyway.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 pretty.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
Show changes to pretty.c +2 −11
diff --git a/pretty.c b/pretty.c
index ba31481..e51c993 100644
--- a/pretty.c
+++ b/pretty.c
@@ -413,7 +413,6 @@ void pp_user_info(const struct pretty_print_context *pp,
 	struct strbuf name;
 	struct strbuf mail;
 	struct ident_split ident;
-	int linelen;
 	char *line_end;
 	const char *mailbuf, *namebuf;
 	size_t namelen, maillen;
@@ -422,18 +421,10 @@ void pp_user_info(const struct pretty_print_context *pp,
 	if (pp->fmt == CMIT_FMT_ONELINE)
 		return;
 
-	line_end = strchr(line, '\n');
-	if (!line_end) {
-		line_end = strchr(line, '\0');
-		if (!line_end)
-			return;
-	}
-
-	linelen = ++line_end - line;
-	if (split_ident_line(&ident, line, linelen))
+	line_end = strchrnul(line, '\n');
+	if (split_ident_line(&ident, line, line_end - line))
 		return;
 
-
 	mailbuf = ident.mail_begin;
 	maillen = ident.mail_end - ident.mail_begin;
 	namebuf = ident.name_begin;
-- 
1.8.2.1
René Scharfe· Apr 25, 2013, 19:41 UTC · re: René Scharfe · lore

[PATCH 2/3] pretty: simplify output line length calculation in pp_user_info()

Keep namelen unchanged and don't use it to hold a value that we're not interested in anyway -- we can use maillen and the constant part directly instead. This simplifies the code slightly and prepares for the next patch that makes use of the original value of namelen.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 pretty.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
Show changes to pretty.c +3 −4
diff --git a/pretty.c b/pretty.c
index e51c993..6861997 100644
--- a/pretty.c
+++ b/pretty.c
@@ -439,8 +439,6 @@ void pp_user_info(const struct pretty_print_context *pp,
 	strbuf_add(&mail, mailbuf, maillen);
 	strbuf_add(&name, namebuf, namelen);
 
-	namelen = name.len + mail.len + 3; /* ' ' + '<' + '>' */
-
 	if (pp->fmt == CMIT_FMT_EMAIL) {
 		strbuf_addstr(sb, "From: ");
 		if (needs_rfc2047_encoding(name.buf, name.len, RFC2047_ADDRESS)) {
@@ -457,9 +455,10 @@ void pp_user_info(const struct pretty_print_context *pp,
 			strbuf_add_wrapped_bytes(sb, name.buf, name.len,
 						 -6, 1, max_length);
 		}
-		if (namelen - name.len + last_line_length(sb) > max_length)
-			strbuf_addch(sb, '\n');
 
+		if (max_length <
+		    last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
+			strbuf_addch(sb, '\n');
 		strbuf_addf(sb, " <%s>\n", mail.buf);
 	} else {
 		strbuf_addf(sb, "%s: %.*s%s <%s>\n", what,
-- 
1.8.2.1
René Scharfe· Apr 25, 2013, 19:43 UTC · re: René Scharfe · lore

[PATCH 3/3] pretty: remove intermediate strbufs from pp_user_info()

Use namebuf/namelen and mailbuf/maillen directly instead of copying their contents into strbufs first.

Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>
---
 pretty.c | 29 +++++++++--------------------
 1 file changed, 9 insertions(+), 20 deletions(-)
Show changes to pretty.c +9 −20
diff --git a/pretty.c b/pretty.c
index 6861997..9e43154 100644
--- a/pretty.c
+++ b/pretty.c
@@ -410,8 +410,6 @@ void pp_user_info(const struct pretty_print_context *pp,
 		  const char *what, struct strbuf *sb,
 		  const char *line, const char *encoding)
 {
-	struct strbuf name;
-	struct strbuf mail;
 	struct ident_split ident;
 	char *line_end;
 	const char *mailbuf, *namebuf;
@@ -433,42 +431,33 @@ void pp_user_info(const struct pretty_print_context *pp,
 	if (pp->mailmap)
 		map_user(pp->mailmap, &mailbuf, &maillen, &namebuf, &namelen);
 
-	strbuf_init(&mail, 0);
-	strbuf_init(&name, 0);
-
-	strbuf_add(&mail, mailbuf, maillen);
-	strbuf_add(&name, namebuf, namelen);
-
 	if (pp->fmt == CMIT_FMT_EMAIL) {
 		strbuf_addstr(sb, "From: ");
-		if (needs_rfc2047_encoding(name.buf, name.len, RFC2047_ADDRESS)) {
-			add_rfc2047(sb, name.buf, name.len,
+		if (needs_rfc2047_encoding(namebuf, namelen, RFC2047_ADDRESS)) {
+			add_rfc2047(sb, namebuf, namelen,
 				    encoding, RFC2047_ADDRESS);
 			max_length = 76; /* per rfc2047 */
-		} else if (needs_rfc822_quoting(name.buf, name.len)) {
+		} else if (needs_rfc822_quoting(namebuf, namelen)) {
 			struct strbuf quoted = STRBUF_INIT;
-			add_rfc822_quoted(&quoted, name.buf, name.len);
+			add_rfc822_quoted(&quoted, namebuf, namelen);
 			strbuf_add_wrapped_bytes(sb, quoted.buf, quoted.len,
 							-6, 1, max_length);
 			strbuf_release(&quoted);
 		} else {
-			strbuf_add_wrapped_bytes(sb, name.buf, name.len,
+			strbuf_add_wrapped_bytes(sb, namebuf, namelen,
 						 -6, 1, max_length);
 		}
 
 		if (max_length <
 		    last_line_length(sb) + strlen(" <") + maillen + strlen(">"))
 			strbuf_addch(sb, '\n');
-		strbuf_addf(sb, " <%s>\n", mail.buf);
+		strbuf_addf(sb, " <%.*s>\n", (int)maillen, mailbuf);
 	} else {
-		strbuf_addf(sb, "%s: %.*s%s <%s>\n", what,
-			      (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0,
-			      "    ", name.buf, mail.buf);
+		strbuf_addf(sb, "%s: %.*s%.*s <%.*s>\n", what,
+			    (pp->fmt == CMIT_FMT_FULLER) ? 4 : 0, "    ",
+			    (int)namelen, namebuf, (int)maillen, mailbuf);
 	}
 
-	strbuf_release(&mail);
-	strbuf_release(&name);
-
 	switch (pp->fmt) {
 	case CMIT_FMT_MEDIUM:
 		strbuf_addf(sb, "Date:   %s\n",
-- 
1.8.2.1

← back to recent threads