List for cgit developers and users
 help / color / mirror / Atom feed
From: e at 80x24.org (Eric Wong)
Subject: [PATCH] ui-ssdiff: get rid of strncat
Date: Wed,  2 Jan 2019 07:37:10 +0000	[thread overview]
Message-ID: <20190102073710.580-1-e@80x24.org> (raw)

strncat is slow and error-prone, use the git strbuf API instead.
We can steal much of our logic from git/pretty.c::strbuf_add_tabexpand,
but maybe keep the invalid characters for now *shrug*.
---
 ui-ssdiff.c | 58 +++++++++++++++++++++++------------------------------
 1 file changed, 25 insertions(+), 33 deletions(-)

diff --git a/ui-ssdiff.c b/ui-ssdiff.c
index c456033..a6b3861 100644
--- a/ui-ssdiff.c
+++ b/ui-ssdiff.c
@@ -109,44 +109,36 @@ static int line_from_hunk(char *line, char type)
 	return res;
 }
 
-static char *replace_tabs(char *line)
+static char *replace_tabs(const char *line)
 {
-	char *prev_buf = line;
-	char *cur_buf;
+	const size_t tabwidth = 8;
 	size_t linelen = strlen(line);
-	int n_tabs = 0;
-	int i;
-	char *result;
-	int result_len;
-
-	if (linelen == 0) {
-		result = xmalloc(1);
-		result[0] = '\0';
-		return result;
+	struct strbuf sb;
+	const char *tab;
+	size_t tab_extra = 0;
+
+	while ((tab = memchr(line, '\t', linelen)) != NULL) {
+		tab_extra += tabwidth - 1;
+		linelen -= tab + 1 - line;
+		line = tab + 1;
 	}
 
-	for (i = 0; i < linelen; i++) {
-		if (line[i] == '\t')
-			n_tabs += 1;
-	}
-	result_len = linelen + n_tabs * 8;
-	result = xmalloc(result_len + 1);
-	result[0] = '\0';
-
-	for (;;) {
-		cur_buf = strchr(prev_buf, '\t');
-		if (!cur_buf) {
-			strncat(result, prev_buf, result_len);
-			break;
-		} else {
-			strncat(result, prev_buf, cur_buf - prev_buf);
-			linelen = strlen(result);
-			memset(&result[linelen], ' ', 8 - (linelen % 8));
-			result[linelen + 8 - (linelen % 8)] = '\0';
-		}
-		prev_buf = cur_buf + 1;
+	strbuf_init(&sb, linelen + tab_extra + 1);
+
+	/* based on git/pretty.c::strbuf_add_tabexpand: */
+	while ((tab = memchr(line, '\t', linelen)) != NULL) {
+		/* Output the data .. */
+		strbuf_add(&sb, line, tab - line);
+
+		/* .. and the de-tabified tab */
+		strbuf_addchars(&sb, ' ', tabwidth);
+
+		/* Skip over the printed part .. */
+		linelen -= tab + 1 - line;
+		line = tab + 1;
 	}
-	return result;
+
+	return strbuf_detach(&sb, NULL);
 }
 
 static int calc_deferred_lines(struct deferred_lines *start)
-- 
EW



             reply	other threads:[~2019-01-02  7:37 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-02  7:37 e [this message]
2019-04-22  2:39 ` [PATCH v2] " e

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

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

  git send-email \
    --in-reply-to=20190102073710.580-1-e@80x24.org \
    --to=cgit@lists.zx2c4.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.
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 NNTP newsgroup(s).