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
- min
) / 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_add_indented_text(struct strbuf
*buf
, const char *text
,
243 int indent
, int indent2
)
248 const char *eol
= strchrnul(text
, '\n');
251 strbuf_addchars(buf
, ' ', indent
);
252 strbuf_add(buf
, text
, eol
- text
);
259 * Wrap the text, if necessary. The variable indent is the indent for the
260 * first line, indent2 is the indent for all other lines.
261 * If indent is negative, assume that already -indent columns have been
262 * consumed (and no extra indent is necessary for the first line).
264 void strbuf_add_wrapped_text(struct strbuf
*buf
,
265 const char *text
, int indent1
, int indent2
, int width
)
267 int indent
, w
, assume_utf8
= 1;
268 const char *bol
, *space
, *start
= text
;
269 size_t orig_len
= buf
->len
;
272 strbuf_add_indented_text(buf
, text
, indent1
, indent2
);
278 w
= indent
= indent1
;
289 while ((skip
= display_mode_esc_sequence_len(text
)))
293 if (!c
|| isspace(c
)) {
294 if (w
<= width
|| !space
) {
295 const char *start
= bol
;
296 if (!c
&& text
== start
)
301 strbuf_addchars(buf
, ' ', indent
);
302 strbuf_add(buf
, start
, text
- start
);
308 else if (c
== '\n') {
310 if (*space
== '\n') {
311 strbuf_addch(buf
, '\n');
314 else if (!isalnum(*space
))
317 strbuf_addch(buf
, ' ');
324 strbuf_addch(buf
, '\n');
325 text
= bol
= space
+ isspace(*space
);
327 w
= indent
= indent2
;
332 w
+= utf8_width(&text
, NULL
);
336 strbuf_setlen(buf
, orig_len
);
346 void strbuf_add_wrapped_bytes(struct strbuf
*buf
, const char *data
, int len
,
347 int indent
, int indent2
, int width
)
349 char *tmp
= xstrndup(data
, len
);
350 strbuf_add_wrapped_text(buf
, tmp
, indent
, indent2
, width
);
354 void strbuf_utf8_replace(struct strbuf
*sb_src
, int pos
, int width
,
357 struct strbuf sb_dst
= STRBUF_INIT
;
358 char *src
= sb_src
->buf
;
359 char *end
= src
+ sb_src
->len
;
361 int w
= 0, subst_len
= 0;
364 subst_len
= strlen(subst
);
365 strbuf_grow(&sb_dst
, sb_src
->len
+ subst_len
);
372 while ((n
= display_mode_esc_sequence_len(src
))) {
382 n
= utf8_width((const char**)&src
, NULL
);
383 if (!src
) /* broken utf-8, do nothing */
385 if (n
&& w
>= pos
&& w
< pos
+ width
) {
387 memcpy(dst
, subst
, subst_len
);
394 memcpy(dst
, old
, src
- old
);
398 strbuf_setlen(&sb_dst
, dst
- sb_dst
.buf
);
399 strbuf_swap(sb_src
, &sb_dst
);
401 strbuf_release(&sb_dst
);
405 * Returns true (1) if the src encoding name matches the dst encoding
406 * name directly or one of its alternative names. E.g. UTF-16BE is the
409 static int same_utf_encoding(const char *src
, const char *dst
)
411 if (istarts_with(src
, "utf") && istarts_with(dst
, "utf")) {
412 /* src[3] or dst[3] might be '\0' */
413 int i
= (src
[3] == '-' ? 4 : 3);
414 int j
= (dst
[3] == '-' ? 4 : 3);
415 return !strcasecmp(src
+i
, dst
+j
);
420 int is_encoding_utf8(const char *name
)
424 if (same_utf_encoding("utf-8", name
))
429 int same_encoding(const char *src
, const char *dst
)
431 static const char utf8
[] = "UTF-8";
437 if (same_utf_encoding(src
, dst
))
439 return !strcasecmp(src
, dst
);
443 * Wrapper for fprintf and returns the total number of columns required
444 * for the printed string, assuming that the string is utf8.
446 int utf8_fprintf(FILE *stream
, const char *format
, ...)
448 struct strbuf buf
= STRBUF_INIT
;
452 va_start(arg
, format
);
453 strbuf_vaddf(&buf
, format
, arg
);
456 columns
= fputs(buf
.buf
, stream
);
457 if (0 <= columns
) /* keep the error from the I/O */
458 columns
= utf8_strwidth(buf
.buf
);
459 strbuf_release(&buf
);
464 * Given a buffer and its encoding, return it re-encoded
465 * with iconv. If the conversion fails, returns NULL.
468 #if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6))
469 typedef const char * iconv_ibp
;
471 typedef char * iconv_ibp
;
473 char *reencode_string_iconv(const char *in
, size_t insz
, iconv_t conv
, size_t *outsz_p
)
475 size_t outsz
, outalloc
;
480 outalloc
= st_add(outsz
, 1); /* for terminating NUL */
481 out
= xmalloc(outalloc
);
486 size_t cnt
= iconv(conv
, &cp
, &insz
, &outpos
, &outsz
);
488 if (cnt
== (size_t) -1) {
490 if (errno
!= E2BIG
) {
494 /* insz has remaining number of bytes.
495 * since we started outsz the same as insz,
496 * it is likely that insz is not enough for
497 * converting the rest.
499 sofar
= outpos
- out
;
500 outalloc
= st_add3(sofar
, st_mult(insz
, 2), 32);
501 out
= xrealloc(out
, outalloc
);
502 outpos
= out
+ sofar
;
503 outsz
= outalloc
- sofar
- 1;
508 *outsz_p
= outpos
- out
;
515 static const char *fallback_encoding(const char *name
)
518 * Some platforms do not have the variously spelled variants of
519 * UTF-8, so let's fall back to trying the most official
520 * spelling. We do so only as a fallback in case the platform
521 * does understand the user's spelling, but not our official
524 if (is_encoding_utf8(name
))
528 * Even though latin-1 is still seen in e-mail
529 * headers, some platforms only install ISO-8859-1.
531 if (!strcasecmp(name
, "latin-1"))
537 char *reencode_string_len(const char *in
, size_t insz
,
538 const char *out_encoding
, const char *in_encoding
,
547 conv
= iconv_open(out_encoding
, in_encoding
);
548 if (conv
== (iconv_t
) -1) {
549 in_encoding
= fallback_encoding(in_encoding
);
550 out_encoding
= fallback_encoding(out_encoding
);
552 conv
= iconv_open(out_encoding
, in_encoding
);
553 if (conv
== (iconv_t
) -1)
557 out
= reencode_string_iconv(in
, insz
, conv
, outsz
);
563 static int has_bom_prefix(const char *data
, size_t len
,
564 const char *bom
, size_t bom_len
)
566 return data
&& bom
&& (len
>= bom_len
) && !memcmp(data
, bom
, bom_len
);
569 static const char utf16_be_bom
[] = {0xFE, 0xFF};
570 static const char utf16_le_bom
[] = {0xFF, 0xFE};
571 static const char utf32_be_bom
[] = {0x00, 0x00, 0xFE, 0xFF};
572 static const char utf32_le_bom
[] = {0xFF, 0xFE, 0x00, 0x00};
574 int has_prohibited_utf_bom(const char *enc
, const char *data
, size_t len
)
577 (same_utf_encoding("UTF-16BE", enc
) ||
578 same_utf_encoding("UTF-16LE", enc
)) &&
579 (has_bom_prefix(data
, len
, utf16_be_bom
, sizeof(utf16_be_bom
)) ||
580 has_bom_prefix(data
, len
, utf16_le_bom
, sizeof(utf16_le_bom
)))
582 (same_utf_encoding("UTF-32BE", enc
) ||
583 same_utf_encoding("UTF-32LE", enc
)) &&
584 (has_bom_prefix(data
, len
, utf32_be_bom
, sizeof(utf32_be_bom
)) ||
585 has_bom_prefix(data
, len
, utf32_le_bom
, sizeof(utf32_le_bom
)))
589 int is_missing_required_utf_bom(const char *enc
, const char *data
, size_t len
)
592 (same_utf_encoding(enc
, "UTF-16")) &&
593 !(has_bom_prefix(data
, len
, utf16_be_bom
, sizeof(utf16_be_bom
)) ||
594 has_bom_prefix(data
, len
, utf16_le_bom
, sizeof(utf16_le_bom
)))
596 (same_utf_encoding(enc
, "UTF-32")) &&
597 !(has_bom_prefix(data
, len
, utf32_be_bom
, sizeof(utf32_be_bom
)) ||
598 has_bom_prefix(data
, len
, utf32_le_bom
, sizeof(utf32_le_bom
)))
603 * Returns first character length in bytes for multi-byte `text` according to
606 * - The `text` pointer is updated to point at the next character.
607 * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes
608 * we can consume from text, and on exit `*remainder_p` is reduced by returned
609 * character length. Otherwise `text` is treated as limited by NUL.
611 int mbs_chrlen(const char **text
, size_t *remainder_p
, const char *encoding
)
614 const char *p
= *text
;
615 size_t r
= (remainder_p
? *remainder_p
: SIZE_MAX
);
620 if (is_encoding_utf8(encoding
)) {
621 pick_one_utf8_char(&p
, &r
);
623 chrlen
= p
? (p
- *text
)
624 : 1 /* not valid UTF-8 -> raw byte sequence */;
628 * TODO use iconv to decode one char and obtain its chrlen
629 * for now, let's treat encodings != UTF-8 as one-byte
636 *remainder_p
-= chrlen
;
642 * Pick the next char from the stream, ignoring codepoints an HFS+ would.
643 * Note that this is _not_ complete by any means. It's just enough
644 * to make is_hfs_dotgit() work, and should not be used otherwise.
646 static ucs_char_t
next_hfs_char(const char **in
)
649 ucs_char_t out
= pick_one_utf8_char(in
, NULL
);
651 * check for malformed utf8. Technically this
652 * gets converted to a percent-sequence, but
653 * returning 0 is good enough for is_hfs_dotgit
654 * to realize it cannot be .git
659 /* these code points are ignored completely */
661 case 0x200c: /* ZERO WIDTH NON-JOINER */
662 case 0x200d: /* ZERO WIDTH JOINER */
663 case 0x200e: /* LEFT-TO-RIGHT MARK */
664 case 0x200f: /* RIGHT-TO-LEFT MARK */
665 case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
666 case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
667 case 0x202c: /* POP DIRECTIONAL FORMATTING */
668 case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
669 case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
670 case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
671 case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
672 case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
673 case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
674 case 0x206e: /* NATIONAL DIGIT SHAPES */
675 case 0x206f: /* NOMINAL DIGIT SHAPES */
676 case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
684 static int is_hfs_dot_generic(const char *path
,
685 const char *needle
, size_t needle_len
)
689 c
= next_hfs_char(&path
);
694 * there's a great deal of other case-folding that occurs
695 * in HFS+, but this is enough to catch our fairly vanilla
696 * hard-coded needles.
698 for (; needle_len
> 0; needle
++, needle_len
--) {
699 c
= next_hfs_char(&path
);
702 * We know our needles contain only ASCII, so we clamp here to
703 * make the results of tolower() sane.
707 if (tolower(c
) != *needle
)
711 c
= next_hfs_char(&path
);
712 if (c
&& !is_dir_sep(c
))
719 * Inline wrapper to make sure the compiler resolves strlen() on literals at
722 static inline int is_hfs_dot_str(const char *path
, const char *needle
)
724 return is_hfs_dot_generic(path
, needle
, strlen(needle
));
727 int is_hfs_dotgit(const char *path
)
729 return is_hfs_dot_str(path
, "git");
732 int is_hfs_dotgitmodules(const char *path
)
734 return is_hfs_dot_str(path
, "gitmodules");
737 int is_hfs_dotgitignore(const char *path
)
739 return is_hfs_dot_str(path
, "gitignore");
742 int is_hfs_dotgitattributes(const char *path
)
744 return is_hfs_dot_str(path
, "gitattributes");
747 const char utf8_bom
[] = "\357\273\277";
749 int skip_utf8_bom(char **text
, size_t len
)
751 if (len
< strlen(utf8_bom
) ||
752 memcmp(*text
, utf8_bom
, strlen(utf8_bom
)))
754 *text
+= strlen(utf8_bom
);
758 void strbuf_utf8_align(struct strbuf
*buf
, align_type position
, unsigned int width
,
761 int slen
= strlen(s
);
762 int display_len
= utf8_strnwidth(s
, slen
, 0);
763 int utf8_compensation
= slen
- display_len
;
765 if (display_len
>= width
) {
766 strbuf_addstr(buf
, s
);
770 if (position
== ALIGN_LEFT
)
771 strbuf_addf(buf
, "%-*s", width
+ utf8_compensation
, s
);
772 else if (position
== ALIGN_MIDDLE
) {
773 int left
= (width
- display_len
) / 2;
774 strbuf_addf(buf
, "%*s%-*s", left
, "", width
- left
+ utf8_compensation
, s
);
775 } else if (position
== ALIGN_RIGHT
)
776 strbuf_addf(buf
, "%*s", width
+ utf8_compensation
, s
);