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

[PATCH RFC 1/2] strbuf: move strbuf_add_tabexpand into strbuf.c

From
Jacob Keller <jacob.e.keller@intel.com>
Date
Mar 28, 2017, 12:22 UTC
Message-ID
<20170328122209.4861-1-jacob.e.keller@intel.com>
From: Jacob Keller <jacob.keller@gmail.com>

In commit 7cc13c717b52 ("pretty: expand tabs in indented logs to make things line up properly", 2016-03-16) a new function was added to insert a line into a strbuf while expanding the tabs into spaces. This functionality was used to help show the log message correctly when it has been indented, so as to properly display the expected output.

This functionality will be useful in a future patch that adds similar functionality into git diff, so lets move it into strbuf.c and make it a public function.

While we're doing this, rename a few of the variables to fix the surrounding strbuf code.

Signed-off-by: Jacob Keller <jacob.keller@gmail.com>
---
 pretty.c | 50 --------------------------------------------------
 strbuf.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
 strbuf.h |  6 ++++++
 3 files changed, 56 insertions(+), 50 deletions(-)
diff --git a/pretty.c b/pretty.c
index d0f86f5d85ca..70368509ffea 100644
--- a/pretty.c
+++ b/pretty.c
@@ -1658,56 +1658,6 @@ void pp_title_line(struct pretty_print_context *pp,
 	strbuf_release(&title);
 }
 
-static int pp_utf8_width(const char *start, const char *end)
-{
-	int width = 0;
-	size_t remain = end - start;
-
-	while (remain) {
-		int n = utf8_width(&start, &remain);
-		if (n < 0 || !start)
-			return -1;
-		width += n;
-	}
-	return width;
-}
-
-static void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
-				 const char *line, int linelen)
-{
-	const char *tab;
-
-	while ((tab = memchr(line, '\t', linelen)) != NULL) {
-		int width = pp_utf8_width(line, tab);
-
-		/*
-		 * If it wasn't well-formed utf8, or it
-		 * had characters with badly defined
-		 * width (control characters etc), just
-		 * give up on trying to align things.
-		 */
-		if (width < 0)
-			break;
-
-		/* Output the data .. */
-		strbuf_add(sb, line, tab - line);
-
-		/* .. and the de-tabified tab */
-		strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
-
-		/* Skip over the printed part .. */
-		linelen -= tab + 1 - line;
-		line = tab + 1;
-	}
-
-	/*
-	 * Print out everything after the last tab without
-	 * worrying about width - there's nothing more to
-	 * align.
-	 */
-	strbuf_add(sb, line, linelen);
-}
-
 /*
  * pp_handle_indent() prints out the intendation, and
  * the whole line (without the final newline), after
diff --git a/strbuf.c b/strbuf.c
index 00457940cfc1..6cecfcadb05b 100644
--- a/strbuf.c
+++ b/strbuf.c
@@ -275,6 +275,56 @@ void strbuf_commented_addf(struct strbuf *sb, const char *fmt, ...)
 	strbuf_release(&buf);
 }
 
+static int find_utf8_width(const char *start, const char *end)
+{
+	int width = 0;
+	size_t remain = end - start;
+
+	while (remain) {
+		int n = utf8_width(&start, &remain);
+		if (n < 0 || !start)
+			return -1;
+		width += n;
+	}
+	return width;
+}
+
+void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
+			  const char *buf, size_t size)
+{
+	const char *tab;
+
+	while ((tab = memchr(buf, '\t', size)) != NULL) {
+		int width = find_utf8_width(buf, tab);
+
+		/*
+		 * If it wasn't well-formed utf8, or it
+		 * had characters with badly defined
+		 * width (control characters etc), just
+		 * give up on trying to align things.
+		 */
+		if (width < 0)
+			break;
+
+		/* Output the data .. */
+		strbuf_add(sb, buf, tab - buf);
+
+		/* .. and the de-tabified tab */
+		strbuf_addchars(sb, ' ', tabwidth - (width % tabwidth));
+
+		/* Skip over the printed part .. */
+		size -= tab + 1 - buf;
+		buf = tab + 1;
+	}
+
+	/*
+	 * Print out everything after the last tab without
+	 * worrying about width - there's nothing more to
+	 * align.
+	 */
+	strbuf_add(sb, buf, size);
+}
+
 void strbuf_vaddf(struct strbuf *sb, const char *fmt, va_list ap)
 {
 	int len;
diff --git a/strbuf.h b/strbuf.h
index 80047b1bb7b8..17e04911833e 100644
--- a/strbuf.h
+++ b/strbuf.h
@@ -238,6 +238,12 @@ extern void strbuf_splice(struct strbuf *, size_t pos, size_t len,
  */
 extern void strbuf_add_commented_lines(struct strbuf *out, const char *buf, size_t size);
 
+/**
+ * Add a NUL-terminated string to the buffer. Tabs will be expanded using the
+ * provided tabwidth.
+ */
+extern void strbuf_add_tabexpand(struct strbuf *sb, int tabwidth,
+				 const char *buf, size_t size);
 
 /**
  * Add data of given length to the buffer.
-- 
2.12.2.650.ga248b8c51283
Next: Jacob Keller
Message 1 of 6 in “strbuf: move strbuf_add_tabexpand into strbuf.c”
  1. 1/2 strbuf: move strbuf_add_tabexpand into strbuf.cJacob Keller, Mar 28, 2017
  2. 2/2 diff: teach diff to expand tabs in outputJacob Keller, Mar 28, 2017
  3. Junio C HamanoMar 28, 2017
  4. Jacob KellerMar 28, 2017
  5. Jeff KingMar 28, 2017
  6. Junio C HamanoMar 28, 2017

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.