List for cgit developers and users
 help / color / Atom feed
* [PATCH] ui-ssdiff: get rid of strncat
@ 2019-01-02  7:37 e
  2019-04-22  2:39 ` [PATCH v2] " e
  0 siblings, 1 reply; 2+ messages in thread
From: e @ 2019-01-02  7:37 UTC (permalink / 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



^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH v2] ui-ssdiff: get rid of strncat
  2019-01-02  7:37 [PATCH] ui-ssdiff: get rid of strncat e
@ 2019-04-22  2:39 ` e
  0 siblings, 0 replies; 2+ messages in thread
From: e @ 2019-04-22  2:39 UTC (permalink / 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*.
---
 v2: call "strbuf_add(&sb, line, linelen);" at the end
 to ensure tab-less lines are shown.

 ui-ssdiff.c | 59 +++++++++++++++++++++++------------------------------
 1 file changed, 26 insertions(+), 33 deletions(-)

diff --git a/ui-ssdiff.c b/ui-ssdiff.c
index b6dc5b0..b27ba8b 100644
--- a/ui-ssdiff.c
+++ b/ui-ssdiff.c
@@ -109,44 +109,37 @@ 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;
+	strbuf_add(&sb, line, linelen);
+
+	return strbuf_detach(&sb, NULL);
 }
 
 static int calc_deferred_lines(struct deferred_lines *start)
-- 
EW



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-01-02  7:37 [PATCH] ui-ssdiff: get rid of strncat e
2019-04-22  2:39 ` [PATCH v2] " e

List for cgit developers and users

Archives are clonable: git clone --mirror http://inbox.vuxu.org/cgit

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.cgit


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git