git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Michael Haggerty <mhagger@alum.mit.edu>
To: Junio C Hamano <gitster@pobox.com>
Cc: git@vger.kernel.org, Michael Haggerty <mhagger@alum.mit.edu>
Subject: [PATCH v2 14/14] imap-send.c: simplify logic in lf_to_crlf()
Date: Tue, 15 Jan 2013 09:06:32 +0100	[thread overview]
Message-ID: <1358237193-8887-15-git-send-email-mhagger@alum.mit.edu> (raw)
In-Reply-To: <1358237193-8887-1-git-send-email-mhagger@alum.mit.edu>

* The first character in the string used to be special-cased to get
  around the fact that msg->buf[i - 1] is not defined for i == 0.
  Instead, keep track of the previous character in a separate
  variable, "lastc", initialized in such a way to let the loop handle
  i == 0 correctly.

* Make the two loops over the string look as similar as possible to
  make it more obvious that the count computed in the first pass
  agrees with the true length of the new string written in the second
  pass.  As a side effect, this makes it possible to use the "j"
  counter in place of lfnum and new_len.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 imap-send.c | 52 +++++++++++++++++++++++-----------------------------
 1 file changed, 23 insertions(+), 29 deletions(-)

diff --git a/imap-send.c b/imap-send.c
index f2933e9..1d40207 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1081,42 +1081,36 @@ bail:
 	return NULL;
 }
 
+/*
+ * Insert CR characters as necessary in *msg to ensure that every LF
+ * character in *msg is preceded by a CR.
+ */
 static void lf_to_crlf(struct strbuf *msg)
 {
-	size_t new_len;
 	char *new;
-	int i, j, lfnum = 0;
-
-	if (msg->buf[0] == '\n')
-		lfnum++;
-	for (i = 1; i < msg->len; i++) {
-		if (msg->buf[i - 1] != '\r' && msg->buf[i] == '\n')
-			lfnum++;
+	size_t i, j;
+	char lastc;
+
+	/* First pass: tally, in j, the size of the new string: */
+	for (i = j = 0, lastc = '\0'; i < msg->len; i++) {
+		if (msg->buf[i] == '\n' && lastc != '\r')
+			j++; /* a CR will need to be added here */
+		lastc = msg->buf[i];
+		j++;
 	}
 
-	new_len = msg->len + lfnum;
-	new = xmalloc(new_len + 1);
-	if (msg->buf[0] == '\n') {
-		new[0] = '\r';
-		new[1] = '\n';
-		i = 1;
-		j = 2;
-	} else {
-		new[0] = msg->buf[0];
-		i = 1;
-		j = 1;
-	}
-	for ( ; i < msg->len; i++) {
-		if (msg->buf[i] != '\n') {
-			new[j++] = msg->buf[i];
-			continue;
-		}
-		if (msg->buf[i - 1] != '\r')
+	new = xmalloc(j + 1);
+
+	/*
+	 * Second pass: write the new string.  Note that this loop is
+	 * otherwise identical to the first pass.
+	 */
+	for (i = j = 0, lastc = '\0'; i < msg->len; i++) {
+		if (msg->buf[i] == '\n' && lastc != '\r')
 			new[j++] = '\r';
-		/* otherwise it already had CR before */
-		new[j++] = '\n';
+		lastc = new[j++] = msg->buf[i];
 	}
-	strbuf_attach(msg, new, new_len, new_len + 1);
+	strbuf_attach(msg, new, j, j + 1);
 }
 
 /*
-- 
1.8.0.3

  parent reply	other threads:[~2013-01-15  8:15 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-01-15  8:06 [PATCH v2 00/14] Remove unused code from imap-send.c Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 01/14] imap-send.c: remove msg_data::flags, which was always zero Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 02/14] imap-send.c: remove struct msg_data Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 03/14] iamp-send.c: remove unused struct imap_store_conf Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 04/14] imap-send.c: remove struct store_conf Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 05/14] imap-send.c: remove struct message Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 06/14] imap-send.c: remove some unused fields from struct store Michael Haggerty
2013-01-15 20:32   ` Jonathan Nieder
2013-01-15 22:30     ` Junio C Hamano
2013-01-15 22:59       ` Jonathan Nieder
2013-01-16  8:23     ` Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 07/14] imap-send.c: inline imap_parse_list() in imap_list() Michael Haggerty
2013-01-15 18:51   ` Matt Kraai
2013-01-16  8:26     ` Michael Haggerty
2013-01-16 15:34       ` Junio C Hamano
2013-01-17  4:43         ` Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 08/14] imap-send.c: remove struct imap argument to parse_imap_list_l() Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 09/14] imap-send.c: remove namespace fields from struct imap Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 10/14] imap-send.c: remove unused field imap_store::trashnc Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 11/14] imap-send.c: use struct imap_store instead of struct store Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 12/14] imap-send.c: remove unused field imap_store::uidvalidity Michael Haggerty
2013-01-15  8:06 ` [PATCH v2 13/14] imap-send.c: fold struct store into struct imap_store Michael Haggerty
2013-01-15  8:06 ` Michael Haggerty [this message]
2013-01-15 14:42 ` [PATCH v2 00/14] Remove unused code from imap-send.c Jeff King
2013-01-15 20:49 ` Jonathan Nieder

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1358237193-8887-15-git-send-email-mhagger@alum.mit.edu \
    --to=mhagger@alum.mit.edu \
    --cc=git@vger.kernel.org \
    --cc=gitster@pobox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).