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

[PATCH 7/8] wrap_in_html(): use strbuf_addstr_xml_quoted()

From
Michael Haggerty <mhagger@alum.mit.edu>
Date
Nov 25, 2012, 11:08 UTC
Message-ID
<1353841721-16269-8-git-send-email-mhagger@alum.mit.edu>
In-Reply-To
<1353841721-16269-1-git-send-email-mhagger@alum.mit.edu>

Use the new function to quote characters as they are being added to buf, rather than quoting them in *p and then copying them into buf. This increases code sharing, and changes the algorithm from O(N^2) to O(N) in the number of characters in a line.

Signed-off-by: Michael Haggerty <mhagger@alum.mit.edu>
---
 imap-send.c | 23 ++++-------------------
 1 file changed, 4 insertions(+), 19 deletions(-)
diff --git a/imap-send.c b/imap-send.c
index a5e0e33..b73c913 100644
--- a/imap-send.c
+++ b/imap-send.c
@@ -1339,21 +1339,6 @@ static int imap_store_msg(struct store *gctx, struct msg_data *msg)
 	return DRV_OK;
 }
 
-static void encode_html_chars(struct strbuf *p)
-{
-	int i;
-	for (i = 0; i < p->len; i++) {
-		if (p->buf[i] == '&')
-			strbuf_splice(p, i, 1, "&amp;", 5);
-		if (p->buf[i] == '<')
-			strbuf_splice(p, i, 1, "&lt;", 4);
-		if (p->buf[i] == '>')
-			strbuf_splice(p, i, 1, "&gt;", 4);
-		if (p->buf[i] == '"')
-			strbuf_splice(p, i, 1, "&quot;", 6);
-	}
-}
-
 static void wrap_in_html(struct strbuf *msg)
 {
 	struct strbuf buf = STRBUF_INIT;
@@ -1372,12 +1357,12 @@ static void wrap_in_html(struct strbuf *msg)
 				strbuf_addbuf(&buf, *p);
 				strbuf_addstr(&buf, pre_open);
 				added_header = 1;
-				continue;
+			} else {
+				strbuf_addbuf(&buf, *p);
 			}
+		} else {
+			strbuf_addstr_xml_quoted(&buf, (*p)->buf);
 		}
-		else
-			encode_html_chars(*p);
-		strbuf_addbuf(&buf, *p);
 	}
 	strbuf_addstr(&buf, pre_close);
 	strbuf_list_free(lines);
-- 
1.8.0
Previous: Thiago FarinaNext: Junio C Hamano
Message 14 of 18 in “Add function strbuf_addstr_xml_quoted() and more”
  1. 0/8 Add function strbuf_addstr_xml_quoted() and moreMichael Haggerty, Nov 25, 2012
  2. 1/8 Add new function strbuf_add_xml_quoted()Michael Haggerty, Nov 25, 2012
  3. 2/8 xml_entities(): use function strbuf_addstr_xml_quoted()Michael Haggerty, Nov 25, 2012
  4. 3/8 lf_to_crlf(): NUL-terminate msg_data::dataMichael Haggerty, Nov 25, 2012
  5. 4/8 imap-send: store all_msgs as a strbufMichael Haggerty, Nov 25, 2012
  6. 5/8 imap-send: correctly report errors reading from stdinMichael Haggerty, Nov 25, 2012
  7. 6/8 imap-send: change msg_data from storing (char *, len) to storing strbufMichael Haggerty, Nov 25, 2012
  8. Junio C HamanoNov 29, 2012
  9. Jeff KingNov 29, 2012
  10. Michael HaggertyNov 30, 2012
  11. Junio C HamanoDec 2, 2012
  12. Michael HaggertyDec 2, 2012
  13. Thiago FarinaDec 3, 2012
  14. 7/8 wrap_in_html(): use strbuf_addstr_xml_quoted()Michael Haggerty, Nov 25, 2012
  15. Junio C HamanoNov 29, 2012
  16. Michael HaggertyNov 30, 2012
  17. Junio C HamanoDec 2, 2012
  18. Michael HaggertyDec 2, 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.