about summary refs log tree commit
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2019-01-02 07:32:59 +0000
committerEric Wong <e@80x24.org>2019-04-22 02:34:30 +0000
commit17920b7b72d1c907c740a7d1f79b90c66299043c (patch)
tree97ea6422a2c2262a93d2d0d9a7b0f3da2ce18558
parentbd0293f57015ede637b630fcaf4fc11e7697d777 (diff)
downloadcgit-17920b7b72d1c907c740a7d1f79b90c66299043c.tar.gz
ui-ssdiff: get rid of strncat
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*.
-rw-r--r--ui-ssdiff.c59
1 files 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)