Skip to content

Commit 2bc1e7e

Browse files
pcloudsgitster
authored andcommitted
utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4247fe7 commit 2bc1e7e

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

utf8.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,18 +266,26 @@ int utf8_width(const char **start, size_t *remainder_p)
266266
* string, assuming that the string is utf8. Returns strlen() instead
267267
* if the string does not look like a valid utf8 string.
268268
*/
269-
int utf8_strwidth(const char *string)
269+
int utf8_strnwidth(const char *string, int len, int skip_ansi)
270270
{
271271
int width = 0;
272272
const char *orig = string;
273273

274-
while (1) {
275-
if (!string)
276-
return strlen(orig);
277-
if (!*string)
278-
return width;
274+
if (len == -1)
275+
len = strlen(string);
276+
while (string && string < orig + len) {
277+
int skip;
278+
while (skip_ansi &&
279+
(skip = display_mode_esc_sequence_len(string)) != 0)
280+
string += skip;
279281
width += utf8_width(&string, NULL);
280282
}
283+
return string ? width : len;
284+
}
285+
286+
int utf8_strwidth(const char *string)
287+
{
288+
return utf8_strnwidth(string, -1, 0);
281289
}
282290

283291
int is_utf8(const char *text)

utf8.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
typedef unsigned int ucs_char_t; /* assuming 32bit int */
55

66
int utf8_width(const char **start, size_t *remainder_p);
7+
int utf8_strnwidth(const char *string, int len, int skip_ansi);
78
int utf8_strwidth(const char *string);
89
int is_utf8(const char *text);
910
int is_encoding_utf8(const char *name);

0 commit comments

Comments
 (0)