utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Thu, 18 Apr 2013 23:08:45 +0000 (09:08 +1000)
committerJunio C Hamano <gitster@pobox.com>
Thu, 18 Apr 2013 23:28:28 +0000 (16:28 -0700)
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
utf8.c
utf8.h
diff --git a/utf8.c b/utf8.c
index 6ed93c317d3b5290d6d43c98292d98538d948c0b..e7ba33c2355d1f4a3ab7b679a3e98dec25e0707d 100644 (file)
--- 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)
diff --git a/utf8.h b/utf8.h
index 1f8ecad1e88451c622de08ea14414021da64f402..d3da96f23443df001fd47413882bad62a05b6b2a 100644 (file)
--- a/utf8.h
+++ b/utf8.h
@@ -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);