X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=utf8.c;h=ddfdc5e2b88d346886f64e39a93ced85cc10a78e;hb=9996983c9c35353f352ef7c1abd9b3d2fbb21114;hp=dc3735364f85273c2a119b994ddb405c09dc395c;hpb=b4955fb611ce407ef0e522862c60ae0472dbfe5e;p=git.git 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) {