summaryrefslogtreecommitdiff
path: root/utf8.c
diff options
context:
space:
mode:
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>2013-04-18 23:08:45 (GMT)
committerJunio C Hamano <gitster@pobox.com>2013-04-18 23:28:28 (GMT)
commit2bc1e7ecba1fcd73112d5e6703bdc28fb4da530a (patch)
treeb092b084056c7605ca66ee03f482a7d5252a3350 /utf8.c
parent4247fe79567fed2c9f896e2cc20246d71d8a33a2 (diff)
downloadgit-2bc1e7ecba1fcd73112d5e6703bdc28fb4da530a.zip
git-2bc1e7ecba1fcd73112d5e6703bdc28fb4da530a.tar.gz
git-2bc1e7ecba1fcd73112d5e6703bdc28fb4da530a.tar.bz2
utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'utf8.c')
-rw-r--r--utf8.c20
1 files changed, 14 insertions, 6 deletions
diff --git a/utf8.c b/utf8.c
index 6ed93c3..e7ba33c 100644
--- a/utf8.c
+++ b/utf8.c
@@ -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)