summaryrefslogtreecommitdiff
path: root/utf8.c
diff options
context:
space:
mode:
authorGeoffrey Thomas <geofft@mit.edu>2009-01-30 04:41:28 -0500
committerJunio C Hamano <gitster@pobox.com>2009-02-04 16:30:43 -0800
commit8a9391e9440028c03345afa9f21459237a529592 (patch)
tree2165cc99e28a1f0a9a828a692a5479d8caeecc5c /utf8.c
parentb296e8fce6de6a40a41b5168dfbe735d11255256 (diff)
downloadgit-8a9391e9440028c03345afa9f21459237a529592.tar.gz
utf8: add utf8_strwidth()
I'm about to use this pattern more than once, so make it a common function. Signed-off-by: Geoffrey Thomas <geofft@mit.edu> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/utf8.c b/utf8.c
index dc3735364f..ddfdc5e2b8 100644
--- a/utf8.c
+++ b/utf8.c
@@ -246,6 +246,25 @@ 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, assuming that the string is utf8. Returns strlen() instead
+ * if the string does not look like a valid utf8 string.
+ */
+int utf8_strwidth(const char *string)
+{
+ int width = 0;
+ const char *orig = string;
+
+ while (1) {
+ if (!string)
+ return strlen(orig);
+ if (!*string)
+ return width;
+ width += utf8_width(&string, NULL);
+ }
+}
+
int is_utf8(const char *text)
{
while (*text) {