Skip to content

Commit

Permalink
utf8: add utf8_strwidth()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Geoffrey Thomas authored and Junio C Hamano committed Feb 5, 2009
1 parent b296e8f commit 8a9391e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
int utf8_strwidth(const char *string);
int is_utf8(const char *text);
int is_encoding_utf8(const char *name);

Expand Down

0 comments on commit 8a9391e

Please sign in to comment.