Skip to content

Commit

Permalink
utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
  • Loading branch information
Nguyễn Thái Ngọc Duy authored and Junio C Hamano committed Apr 18, 2013
1 parent 4247fe7 commit 2bc1e7e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
20 changes: 14 additions & 6 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,18 +266,26 @@ int utf8_width(const char **start, size_t *remainder_p)
* 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 utf8_strnwidth(const char *string, int len, int skip_ansi)
{
int width = 0;
const char *orig = string;

while (1) {
if (!string)
return strlen(orig);
if (!*string)
return width;
if (len == -1)
len = strlen(string);
while (string && string < orig + len) {
int skip;
while (skip_ansi &&
(skip = display_mode_esc_sequence_len(string)) != 0)
string += skip;
width += utf8_width(&string, NULL);
}
return string ? width : len;
}

int utf8_strwidth(const char *string)
{
return utf8_strnwidth(string, -1, 0);
}

int is_utf8(const char *text)
Expand Down
1 change: 1 addition & 0 deletions utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
typedef unsigned int ucs_char_t; /* assuming 32bit int */

int utf8_width(const char **start, size_t *remainder_p);
int utf8_strnwidth(const char *string, int len, int skip_ansi);
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 2bc1e7e

Please sign in to comment.