1 #include "git-compat-util.h"
5 /* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
12 size_t display_mode_esc_sequence_len(const char *s)
19 while (isdigit(*p) || *p == ';')
26 /* auxiliary function for binary search in interval table */
27 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
32 if (ucs < table[0].first || ucs > table[max].last)
35 mid = (min + max) / 2;
36 if (ucs > table[mid].last)
38 else if (ucs < table[mid].first)
47 /* The following two functions define the column width of an ISO 10646
48 * character as follows:
50 * - The null character (U+0000) has a column width of 0.
52 * - Other C0/C1 control characters and DEL will lead to a return
55 * - Non-spacing and enclosing combining characters (general
56 * category code Mn or Me in the Unicode database) have a
59 * - SOFT HYPHEN (U+00AD) has a column width of 1.
61 * - Other format characters (general category code Cf in the Unicode
62 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
64 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
65 * have a column width of 0.
67 * - Spacing characters in the East Asian Wide (W) or East Asian
68 * Full-width (F) category as defined in Unicode Technical
69 * Report #11 have a column width of 2.
71 * - All remaining characters (including all printable
72 * ISO 8859-1 and WGL4 characters, Unicode control characters,
73 * etc.) have a column width of 1.
75 * This implementation assumes that ucs_char_t characters are encoded
79 static int git_wcwidth(ucs_char_t ch)
82 * Sorted list of non-overlapping intervals of non-spacing characters,
84 #include "unicode_width.h"
86 /* test for 8-bit control characters */
89 if (ch < 32 || (ch >= 0x7f && ch < 0xa0))
92 /* binary search in table of non-spacing characters */
93 if (bisearch(ch, zero_width, sizeof(zero_width)
94 / sizeof(struct interval) - 1))
97 /* binary search in table of double width characters */
98 if (bisearch(ch, double_width, sizeof(double_width)
99 / sizeof(struct interval) - 1))
106 * Pick one ucs character starting from the location *start points at,
107 * and return it, while updating the *start pointer to point at the
108 * end of that character. When remainder_p is not NULL, the location
109 * holds the number of bytes remaining in the string that we are allowed
110 * to pick from. Otherwise we are allowed to pick up to the NUL that
111 * would eventually appear in the string. *remainder_p is also reduced
112 * by the number of bytes we have consumed.
114 * If the string was not a valid UTF-8, *start pointer is set to NULL
115 * and the return value is undefined.
117 static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p)
119 unsigned char *s = (unsigned char *)*start;
121 size_t remainder, incr;
124 * A caller that assumes NUL terminated text can choose
125 * not to bother with the remainder length. We will
126 * stop at the first NUL.
128 remainder = (remainder_p ? *remainder_p : 999);
132 } else if (*s < 0x80) {
136 } else if ((s[0] & 0xe0) == 0xc0) {
137 /* 110XXXXx 10xxxxxx */
139 (s[1] & 0xc0) != 0x80 ||
140 (s[0] & 0xfe) == 0xc0)
142 ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
144 } else if ((s[0] & 0xf0) == 0xe0) {
145 /* 1110XXXX 10Xxxxxx 10xxxxxx */
147 (s[1] & 0xc0) != 0x80 ||
148 (s[2] & 0xc0) != 0x80 ||
150 (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) ||
152 (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) ||
153 /* U+FFFE or U+FFFF? */
154 (s[0] == 0xef && s[1] == 0xbf &&
155 (s[2] & 0xfe) == 0xbe))
157 ch = ((s[0] & 0x0f) << 12) |
158 ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
160 } else if ((s[0] & 0xf8) == 0xf0) {
161 /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
163 (s[1] & 0xc0) != 0x80 ||
164 (s[2] & 0xc0) != 0x80 ||
165 (s[3] & 0xc0) != 0x80 ||
167 (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) ||
169 (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4)
171 ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
172 ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
182 *remainder_p = remainder - incr;
187 * This function returns the number of columns occupied by the character
188 * pointed to by the variable start. The pointer is updated to point at
189 * the next character. When remainder_p is not NULL, it points at the
190 * location that stores the number of remaining bytes we can use to pick
191 * a character (see pick_one_utf8_char() above).
193 int utf8_width(const char **start, size_t *remainder_p)
195 ucs_char_t ch = pick_one_utf8_char(start, remainder_p);
198 return git_wcwidth(ch);
202 * Returns the total number of columns required by a null-terminated
203 * string, assuming that the string is utf8. Returns strlen() instead
204 * if the string does not look like a valid utf8 string.
206 int utf8_strnwidth(const char *string, int len, int skip_ansi)
209 const char *orig = string;
212 len = strlen(string);
213 while (string && string < orig + len) {
216 (skip = display_mode_esc_sequence_len(string)) != 0)
218 width += utf8_width(&string, NULL);
220 return string ? width : len;
223 int utf8_strwidth(const char *string)
225 return utf8_strnwidth(string, -1, 0);
228 int is_utf8(const char *text)
231 if (*text == '\n' || *text == '\t' || *text == '\r') {
235 utf8_width(&text, NULL);
242 static void strbuf_addchars(struct strbuf *sb, int c, size_t n)
245 memset(sb->buf + sb->len, c, n);
246 strbuf_setlen(sb, sb->len + n);
249 static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
250 int indent, int indent2)
255 const char *eol = strchrnul(text, '\n');
258 strbuf_addchars(buf, ' ', indent);
259 strbuf_add(buf, text, eol - text);
266 * Wrap the text, if necessary. The variable indent is the indent for the
267 * first line, indent2 is the indent for all other lines.
268 * If indent is negative, assume that already -indent columns have been
269 * consumed (and no extra indent is necessary for the first line).
271 void strbuf_add_wrapped_text(struct strbuf *buf,
272 const char *text, int indent1, int indent2, int width)
274 int indent, w, assume_utf8 = 1;
275 const char *bol, *space, *start = text;
276 size_t orig_len = buf->len;
279 strbuf_add_indented_text(buf, text, indent1, indent2);
285 w = indent = indent1;
296 while ((skip = display_mode_esc_sequence_len(text)))
300 if (!c || isspace(c)) {
301 if (w <= width || !space) {
302 const char *start = bol;
303 if (!c && text == start)
308 strbuf_addchars(buf, ' ', indent);
309 strbuf_add(buf, start, text - start);
315 else if (c == '\n') {
317 if (*space == '\n') {
318 strbuf_addch(buf, '\n');
321 else if (!isalnum(*space))
324 strbuf_addch(buf, ' ');
331 strbuf_addch(buf, '\n');
332 text = bol = space + isspace(*space);
334 w = indent = indent2;
339 w += utf8_width(&text, NULL);
343 strbuf_setlen(buf, orig_len);
353 void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
354 int indent, int indent2, int width)
356 char *tmp = xstrndup(data, len);
357 strbuf_add_wrapped_text(buf, tmp, indent, indent2, width);
361 void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
364 struct strbuf sb_dst = STRBUF_INIT;
365 char *src = sb_src->buf;
366 char *end = src + sb_src->len;
368 int w = 0, subst_len = 0;
371 subst_len = strlen(subst);
372 strbuf_grow(&sb_dst, sb_src->len + subst_len);
379 while ((n = display_mode_esc_sequence_len(src))) {
389 n = utf8_width((const char**)&src, NULL);
390 if (!src) /* broken utf-8, do nothing */
392 if (n && w >= pos && w < pos + width) {
394 memcpy(dst, subst, subst_len);
401 memcpy(dst, old, src - old);
405 strbuf_setlen(&sb_dst, dst - sb_dst.buf);
406 strbuf_swap(sb_src, &sb_dst);
407 strbuf_release(&sb_dst);
410 int is_encoding_utf8(const char *name)
414 if (!strcasecmp(name, "utf-8") || !strcasecmp(name, "utf8"))
419 int same_encoding(const char *src, const char *dst)
421 if (is_encoding_utf8(src) && is_encoding_utf8(dst))
423 return !strcasecmp(src, dst);
427 * Wrapper for fprintf and returns the total number of columns required
428 * for the printed string, assuming that the string is utf8.
430 int utf8_fprintf(FILE *stream, const char *format, ...)
432 struct strbuf buf = STRBUF_INIT;
436 va_start(arg, format);
437 strbuf_vaddf(&buf, format, arg);
440 columns = fputs(buf.buf, stream);
441 if (0 <= columns) /* keep the error from the I/O */
442 columns = utf8_strwidth(buf.buf);
443 strbuf_release(&buf);
448 * Given a buffer and its encoding, return it re-encoded
449 * with iconv. If the conversion fails, returns NULL.
452 #if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6))
453 typedef const char * iconv_ibp;
455 typedef char * iconv_ibp;
457 char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, int *outsz_p)
459 size_t outsz, outalloc;
464 outalloc = outsz + 1; /* for terminating NUL */
465 out = xmalloc(outalloc);
470 size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
472 if (cnt == (size_t) -1) {
474 if (errno != E2BIG) {
478 /* insz has remaining number of bytes.
479 * since we started outsz the same as insz,
480 * it is likely that insz is not enough for
481 * converting the rest.
483 sofar = outpos - out;
484 outalloc = sofar + insz * 2 + 32;
485 out = xrealloc(out, outalloc);
486 outpos = out + sofar;
487 outsz = outalloc - sofar - 1;
492 *outsz_p = outpos - out;
499 char *reencode_string_len(const char *in, int insz,
500 const char *out_encoding, const char *in_encoding,
509 conv = iconv_open(out_encoding, in_encoding);
510 if (conv == (iconv_t) -1) {
512 * Some platforms do not have the variously spelled variants of
513 * UTF-8, so let's fall back to trying the most official
514 * spelling. We do so only as a fallback in case the platform
515 * does understand the user's spelling, but not our official
518 if (is_encoding_utf8(in_encoding))
519 in_encoding = "UTF-8";
520 if (is_encoding_utf8(out_encoding))
521 out_encoding = "UTF-8";
522 conv = iconv_open(out_encoding, in_encoding);
523 if (conv == (iconv_t) -1)
527 out = reencode_string_iconv(in, insz, conv, outsz);
534 * Returns first character length in bytes for multi-byte `text` according to
537 * - The `text` pointer is updated to point at the next character.
538 * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes
539 * we can consume from text, and on exit `*remainder_p` is reduced by returned
540 * character length. Otherwise `text` is treated as limited by NUL.
542 int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding)
545 const char *p = *text;
546 size_t r = (remainder_p ? *remainder_p : SIZE_MAX);
551 if (is_encoding_utf8(encoding)) {
552 pick_one_utf8_char(&p, &r);
554 chrlen = p ? (p - *text)
555 : 1 /* not valid UTF-8 -> raw byte sequence */;
559 * TODO use iconv to decode one char and obtain its chrlen
560 * for now, let's treat encodings != UTF-8 as one-byte
567 *remainder_p -= chrlen;