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

[PATCH 11/14] imap-send.c: simplify logic in lf_to_crlf()

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Jan 14, 2013, 05:32 UTC
Message-ID
<1358141566-26081-12-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1358141566-26081-1-git-send-email-mhagger@alum.mit.edu>
Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 imap-send.c | 31 ++++++++-----------------------
 1 file changed, 8 insertions(+), 23 deletions(-)
diff --git a/imap-send.c b/imap-send.c
index 70abe9b..3167dcc 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1089,36 +1089,21 @@ static void lf_to_crlf(struct strbuf *msg)
 {
 	size_t new_len;
 	char *new;
-	int i, j, lfnum = 0;
+	int i, j = 0, lfnum = 0;
+	char lastc;
 
-	if (msg->buf[0] == '\n')
-		lfnum++;
-	for (i = 1; i < msg->len; i++) {
-		if (msg->buf[i - 1] != '\r' && msg->buf[i] == '\n')
+	for (i = 0, lastc = '\0'; i < msg->len; i++) {
+		if (msg->buf[i] == '\n' && lastc != '\r')
 			lfnum++;
+		lastc = msg->buf[i];
 	}
 
 	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')
+	for (i = 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);
 }
-- 
1.8.0.3
Previous: Michael HaggertyNext: Jonathan Nieder
Message 18 of 27 in “Remove unused code from imap-send.c”
  1. 00/14 Remove unused code from imap-send.cMichael Haggerty, Jan 14, 2013
  2. 01/14 imap-send.c: remove msg_data::flags, which was always zeroMichael Haggerty, Jan 14, 2013
  3. Jonathan NiederJan 14, 2013
  4. Michael HaggertyJan 14, 2013
  5. 02/14 imap-send.c: remove struct msg_dataMichael Haggerty, Jan 14, 2013
  6. 03/14 iamp-send.c: remove unused struct imap_store_confMichael Haggerty, Jan 14, 2013
  7. 04/14 imap-send.c: remove struct store_confMichael Haggerty, Jan 14, 2013
  8. 05/14 imap-send.c: remove struct messageMichael Haggerty, Jan 14, 2013
  9. 06/14 imap-send.c: remove some unused fields from struct storeMichael Haggerty, Jan 14, 2013
  10. Jonathan NiederJan 14, 2013
  11. Michael HaggertyJan 14, 2013
  12. 07/14 imap-send.c: inline imap_parse_list() in imap_list()Michael Haggerty, Jan 14, 2013
  13. 08/14 imap-send.c: remove struct imap argument to parse_imap_list_l()Michael Haggerty, Jan 14, 2013
  14. 09/14 imap-send.c: remove namespace fields from struct imapMichael Haggerty, Jan 14, 2013
  15. Jonathan NiederJan 14, 2013
  16. Michael HaggertyJan 14, 2013
  17. 10/14 imap-send.c: remove unused field imap_store::trashncMichael Haggerty, Jan 14, 2013
  18. 11/14 imap-send.c: simplify logic in lf_to_crlf()Michael Haggerty, Jan 14, 2013
  19. Jonathan NiederJan 14, 2013
  20. 12/14 imap-send.c: use struct imap_store instead of struct storeMichael Haggerty, Jan 14, 2013
  21. Jonathan NiederJan 14, 2013
  22. 13/14 imap-send.c: remove unused field imap_store::uidvalidityMichael Haggerty, Jan 14, 2013
  23. 14/14 imap-send.c: fold struct store into struct imap_storeMichael Haggerty, Jan 14, 2013
  24. Jeff KingJan 14, 2013
  25. Jonathan NiederJan 14, 2013
  26. Michael HaggertyJan 14, 2013
  27. Junio C HamanoJan 14, 2013

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.