@matheusbsbs / matheus-bagliano / commits / 8a9391e944
Fork 0

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>

Geoffrey Thomas committed Jan 30, 2009 at 04:41 UTC 8a9391e9440028c03345afa9f21459237a529592
2 files changed +20
utf8.c
+19
@@ -246,6 +246,25 @@ int utf8_width(const char **start, size_t *remainder_p)
246 return git_wcwidth(ch);
247 }
248
249 +/*
250 + * Returns the total number of columns required by a null-terminated
251 + * string, assuming that the string is utf8. Returns strlen() instead
252 + * if the string does not look like a valid utf8 string.
253 + */
254 +int utf8_strwidth(const char *string)
255 +{
256 + int width = 0;
257 + const char *orig = string;
258 +
259 + while (1) {
260 + if (!string)
261 + return strlen(orig);
262 + if (!*string)
263 + return width;
264 + width += utf8_width(&string, NULL);
265 + }
266 +}
267 +
268 int is_utf8(const char *text)
269 {
270 while (*text) {
utf8.h
+1
@@ -5,6 +5,7 @@ typedef unsigned int ucs_char_t; /* assuming 32bit int */
5
6 ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p);
7 int utf8_width(const char **start, size_t *remainder_p);
8 +int utf8_strwidth(const char *string);
9 int is_utf8(const char *text);
10 int is_encoding_utf8(const char *name);
11