From 8a9391e9440028c03345afa9f21459237a529592 Mon Sep 17 00:00:00 2001 From: Geoffrey Thomas Date: Fri, 30 Jan 2009 04:41:28 -0500 Subject: 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 Signed-off-by: Junio C Hamano --- utf8.c | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'utf8.c') diff --git a/utf8.c b/utf8.c index dc3735364..ddfdc5e2b 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) { -- cgit v1.2.1