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

[PATCH] utf8: add utf8_strwidth()

From
Geoffrey Thomas <geofft@mit.edu>
Date
Jan 30, 2009, 09:41 UTC
Message-ID
<1233308489-2656-1-git-send-email-geofft@mit.edu>
From: Geoffrey Thomas <geofft@mit.edu>
I'm about to use this pattern more than once, so make it a common function.
Signed-off-by: Geoffrey Thomas <geofft@mit.edu>
---
 utf8.c |   12 ++++++++++++
 utf8.h |    1 +
 2 files changed, 13 insertions(+), 0 deletions(-)
diff --git a/utf8.c b/utf8.c
index dc37353..a2d888d 100644
--- a/utf8.c
+++ b/utf8.c
@@ -246,6 +246,18 @@ int utf8_width(const char **start, size_t *remainder_p)
 	return git_wcwidth(ch);
 }
 
+/*
+ * Returns the total number of columns required by a null-terminated
+ * string.
+ */
+size_t utf8_strwidth(const char *string)
+{
+	size_t width = 0;
+	while (string && *string)
+		width += utf8_width(&string, NULL);
+	return width;
+}
+
 int is_utf8(const char *text)
 {
 	while (*text) {
diff --git a/utf8.h b/utf8.h
index 98cce1b..1ae3450 100644
--- a/utf8.h
+++ b/utf8.h
@@ -5,6 +5,7 @@ typedef unsigned int ucs_char_t;  /* assuming 32bit int */
 
 ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p);
 int utf8_width(const char **start, size_t *remainder_p);
+size_t utf8_strwidth(const char *string);
 int is_utf8(const char *text);
 int is_encoding_utf8(const char *name);
 
-- 
1.5.6.5
Next: Geoffrey Thomas
Message 1 of 13 in “utf8: add utf8_strwidth()”
  1. utf8: add utf8_strwidth()Geoffrey Thomas, Jan 30, 2009
  2. builtin-blame.c: Use utf8_strwidth for author's namesGeoffrey Thomas, Jan 30, 2009
  3. Johannes SchindelinJan 30, 2009
  4. Geoffrey ThomasJan 30, 2009
  5. Jeff KingJan 31, 2009
  6. Johannes SchindelinFeb 1, 2009
  7. Junio C HamanoFeb 2, 2009
  8. Johannes SchindelinFeb 2, 2009
  9. Junio C HamanoFeb 3, 2009
  10. Jeff KingFeb 2, 2009
  11. Jeff KingJan 31, 2009
  12. Geoffrey ThomasJan 31, 2009
  13. Jeff KingJan 31, 2009

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.