Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Johannes Schindelin | a94410c | 2008-11-10 18:47:00 +0100 | [diff] [blame] | 2 | #include "strbuf.h" |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 3 | #include "utf8.h" |
| 4 | |
| 5 | /* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */ |
| 6 | |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 7 | static const char utf16_be_bom[] = {'\xFE', '\xFF'}; |
| 8 | static const char utf16_le_bom[] = {'\xFF', '\xFE'}; |
| 9 | static const char utf32_be_bom[] = {'\0', '\0', '\xFE', '\xFF'}; |
| 10 | static const char utf32_le_bom[] = {'\xFF', '\xFE', '\0', '\0'}; |
| 11 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 12 | struct interval { |
John Keeping | a68a67d | 2014-02-16 16:06:04 +0000 | [diff] [blame] | 13 | ucs_char_t first; |
| 14 | ucs_char_t last; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 15 | }; |
| 16 | |
Nguyễn Thái Ngọc Duy | 1640632 | 2013-04-19 09:08:52 +1000 | [diff] [blame] | 17 | size_t display_mode_esc_sequence_len(const char *s) |
Nguyễn Thái Ngọc Duy | 4247fe7 | 2013-04-19 09:08:44 +1000 | [diff] [blame] | 18 | { |
| 19 | const char *p = s; |
| 20 | if (*p++ != '\033') |
| 21 | return 0; |
| 22 | if (*p++ != '[') |
| 23 | return 0; |
| 24 | while (isdigit(*p) || *p == ';') |
| 25 | p++; |
| 26 | if (*p++ != 'm') |
| 27 | return 0; |
| 28 | return p - s; |
| 29 | } |
| 30 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 31 | /* auxiliary function for binary search in interval table */ |
Junio C Hamano | f3fa183 | 2007-11-08 15:35:32 -0800 | [diff] [blame] | 32 | static int bisearch(ucs_char_t ucs, const struct interval *table, int max) |
| 33 | { |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 34 | int min = 0; |
| 35 | int mid; |
| 36 | |
| 37 | if (ucs < table[0].first || ucs > table[max].last) |
| 38 | return 0; |
| 39 | while (max >= min) { |
Derrick Stolee | 19716b2 | 2017-10-08 14:29:37 -0400 | [diff] [blame] | 40 | mid = min + (max - min) / 2; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 41 | if (ucs > table[mid].last) |
| 42 | min = mid + 1; |
| 43 | else if (ucs < table[mid].first) |
| 44 | max = mid - 1; |
| 45 | else |
| 46 | return 1; |
| 47 | } |
| 48 | |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | /* The following two functions define the column width of an ISO 10646 |
| 53 | * character as follows: |
| 54 | * |
| 55 | * - The null character (U+0000) has a column width of 0. |
| 56 | * |
| 57 | * - Other C0/C1 control characters and DEL will lead to a return |
| 58 | * value of -1. |
| 59 | * |
| 60 | * - Non-spacing and enclosing combining characters (general |
| 61 | * category code Mn or Me in the Unicode database) have a |
| 62 | * column width of 0. |
| 63 | * |
| 64 | * - SOFT HYPHEN (U+00AD) has a column width of 1. |
| 65 | * |
| 66 | * - Other format characters (general category code Cf in the Unicode |
| 67 | * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0. |
| 68 | * |
| 69 | * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF) |
| 70 | * have a column width of 0. |
| 71 | * |
| 72 | * - Spacing characters in the East Asian Wide (W) or East Asian |
| 73 | * Full-width (F) category as defined in Unicode Technical |
| 74 | * Report #11 have a column width of 2. |
| 75 | * |
| 76 | * - All remaining characters (including all printable |
| 77 | * ISO 8859-1 and WGL4 characters, Unicode control characters, |
| 78 | * etc.) have a column width of 1. |
| 79 | * |
Ramsay Jones | 2832114 | 2007-03-03 18:28:57 +0000 | [diff] [blame] | 80 | * This implementation assumes that ucs_char_t characters are encoded |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 81 | * in ISO 10646. |
| 82 | */ |
| 83 | |
Amos Waterland | b51be13 | 2007-05-08 00:46:08 -0400 | [diff] [blame] | 84 | static int git_wcwidth(ucs_char_t ch) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 85 | { |
| 86 | /* |
| 87 | * Sorted list of non-overlapping intervals of non-spacing characters, |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 88 | */ |
Stefan Beller | e233bef | 2018-04-10 14:26:17 -0700 | [diff] [blame] | 89 | #include "unicode-width.h" |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 90 | |
| 91 | /* test for 8-bit control characters */ |
| 92 | if (ch == 0) |
| 93 | return 0; |
| 94 | if (ch < 32 || (ch >= 0x7f && ch < 0xa0)) |
| 95 | return -1; |
| 96 | |
| 97 | /* binary search in table of non-spacing characters */ |
Beat Bolli | fa364ad | 2019-10-11 20:41:23 +0200 | [diff] [blame] | 98 | if (bisearch(ch, zero_width, ARRAY_SIZE(zero_width) - 1)) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 99 | return 0; |
| 100 | |
Torsten Bögershausen | 0846034 | 2014-05-09 23:51:38 +0200 | [diff] [blame] | 101 | /* binary search in table of double width characters */ |
Beat Bolli | fa364ad | 2019-10-11 20:41:23 +0200 | [diff] [blame] | 102 | if (bisearch(ch, double_width, ARRAY_SIZE(double_width) - 1)) |
Torsten Bögershausen | 0846034 | 2014-05-09 23:51:38 +0200 | [diff] [blame] | 103 | return 2; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 104 | |
Torsten Bögershausen | 0846034 | 2014-05-09 23:51:38 +0200 | [diff] [blame] | 105 | return 1; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | /* |
Junio C Hamano | 396ccf1 | 2008-01-06 19:02:22 -0800 | [diff] [blame] | 109 | * Pick one ucs character starting from the location *start points at, |
| 110 | * and return it, while updating the *start pointer to point at the |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 111 | * end of that character. When remainder_p is not NULL, the location |
| 112 | * holds the number of bytes remaining in the string that we are allowed |
| 113 | * to pick from. Otherwise we are allowed to pick up to the NUL that |
| 114 | * would eventually appear in the string. *remainder_p is also reduced |
| 115 | * by the number of bytes we have consumed. |
Junio C Hamano | 396ccf1 | 2008-01-06 19:02:22 -0800 | [diff] [blame] | 116 | * |
| 117 | * If the string was not a valid UTF-8, *start pointer is set to NULL |
| 118 | * and the return value is undefined. |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 119 | */ |
Junio C Hamano | 5e133b8 | 2010-01-11 22:32:29 -0800 | [diff] [blame] | 120 | static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 121 | { |
| 122 | unsigned char *s = (unsigned char *)*start; |
Ramsay Jones | 2832114 | 2007-03-03 18:28:57 +0000 | [diff] [blame] | 123 | ucs_char_t ch; |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 124 | size_t remainder, incr; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 125 | |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 126 | /* |
| 127 | * A caller that assumes NUL terminated text can choose |
| 128 | * not to bother with the remainder length. We will |
| 129 | * stop at the first NUL. |
| 130 | */ |
| 131 | remainder = (remainder_p ? *remainder_p : 999); |
| 132 | |
| 133 | if (remainder < 1) { |
| 134 | goto invalid; |
| 135 | } else if (*s < 0x80) { |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 136 | /* 0xxxxxxx */ |
| 137 | ch = *s; |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 138 | incr = 1; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 139 | } else if ((s[0] & 0xe0) == 0xc0) { |
| 140 | /* 110XXXXx 10xxxxxx */ |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 141 | if (remainder < 2 || |
| 142 | (s[1] & 0xc0) != 0x80 || |
| 143 | (s[0] & 0xfe) == 0xc0) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 144 | goto invalid; |
| 145 | ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 146 | incr = 2; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 147 | } else if ((s[0] & 0xf0) == 0xe0) { |
| 148 | /* 1110XXXX 10Xxxxxx 10xxxxxx */ |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 149 | if (remainder < 3 || |
| 150 | (s[1] & 0xc0) != 0x80 || |
| 151 | (s[2] & 0xc0) != 0x80 || |
| 152 | /* overlong? */ |
| 153 | (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) || |
| 154 | /* surrogate? */ |
| 155 | (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) || |
| 156 | /* U+FFFE or U+FFFF? */ |
| 157 | (s[0] == 0xef && s[1] == 0xbf && |
| 158 | (s[2] & 0xfe) == 0xbe)) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 159 | goto invalid; |
| 160 | ch = ((s[0] & 0x0f) << 12) | |
| 161 | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f); |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 162 | incr = 3; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 163 | } else if ((s[0] & 0xf8) == 0xf0) { |
| 164 | /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */ |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 165 | if (remainder < 4 || |
| 166 | (s[1] & 0xc0) != 0x80 || |
| 167 | (s[2] & 0xc0) != 0x80 || |
| 168 | (s[3] & 0xc0) != 0x80 || |
| 169 | /* overlong? */ |
| 170 | (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) || |
| 171 | /* > U+10FFFF? */ |
| 172 | (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 173 | goto invalid; |
| 174 | ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) | |
| 175 | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f); |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 176 | incr = 4; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 177 | } else { |
| 178 | invalid: |
| 179 | *start = NULL; |
| 180 | return 0; |
| 181 | } |
| 182 | |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 183 | *start += incr; |
| 184 | if (remainder_p) |
| 185 | *remainder_p = remainder - incr; |
Junio C Hamano | 396ccf1 | 2008-01-06 19:02:22 -0800 | [diff] [blame] | 186 | return ch; |
| 187 | } |
| 188 | |
| 189 | /* |
| 190 | * This function returns the number of columns occupied by the character |
| 191 | * pointed to by the variable start. The pointer is updated to point at |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 192 | * the next character. When remainder_p is not NULL, it points at the |
| 193 | * location that stores the number of remaining bytes we can use to pick |
| 194 | * a character (see pick_one_utf8_char() above). |
Junio C Hamano | 396ccf1 | 2008-01-06 19:02:22 -0800 | [diff] [blame] | 195 | */ |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 196 | int utf8_width(const char **start, size_t *remainder_p) |
Junio C Hamano | 396ccf1 | 2008-01-06 19:02:22 -0800 | [diff] [blame] | 197 | { |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 198 | ucs_char_t ch = pick_one_utf8_char(start, remainder_p); |
Junio C Hamano | 396ccf1 | 2008-01-06 19:02:22 -0800 | [diff] [blame] | 199 | if (!*start) |
| 200 | return 0; |
Amos Waterland | b51be13 | 2007-05-08 00:46:08 -0400 | [diff] [blame] | 201 | return git_wcwidth(ch); |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 202 | } |
| 203 | |
Geoffrey Thomas | 8a9391e | 2009-01-30 04:41:28 -0500 | [diff] [blame] | 204 | /* |
| 205 | * Returns the total number of columns required by a null-terminated |
| 206 | * string, assuming that the string is utf8. Returns strlen() instead |
| 207 | * if the string does not look like a valid utf8 string. |
| 208 | */ |
Patrick Steinhardt | 522cc87 | 2022-12-01 15:46:53 +0100 | [diff] [blame] | 209 | int utf8_strnwidth(const char *string, size_t len, int skip_ansi) |
Geoffrey Thomas | 8a9391e | 2009-01-30 04:41:28 -0500 | [diff] [blame] | 210 | { |
Geoffrey Thomas | 8a9391e | 2009-01-30 04:41:28 -0500 | [diff] [blame] | 211 | const char *orig = string; |
Patrick Steinhardt | 937b71c | 2022-12-01 15:47:04 +0100 | [diff] [blame] | 212 | size_t width = 0; |
Geoffrey Thomas | 8a9391e | 2009-01-30 04:41:28 -0500 | [diff] [blame] | 213 | |
Nguyễn Thái Ngọc Duy | 2bc1e7e | 2013-04-19 09:08:45 +1000 | [diff] [blame] | 214 | while (string && string < orig + len) { |
Patrick Steinhardt | 937b71c | 2022-12-01 15:47:04 +0100 | [diff] [blame] | 215 | int glyph_width; |
| 216 | size_t skip; |
Patrick Steinhardt | 17d23e8 | 2022-12-01 15:47:00 +0100 | [diff] [blame] | 217 | |
Nguyễn Thái Ngọc Duy | 2bc1e7e | 2013-04-19 09:08:45 +1000 | [diff] [blame] | 218 | while (skip_ansi && |
| 219 | (skip = display_mode_esc_sequence_len(string)) != 0) |
| 220 | string += skip; |
Patrick Steinhardt | 17d23e8 | 2022-12-01 15:47:00 +0100 | [diff] [blame] | 221 | |
| 222 | glyph_width = utf8_width(&string, NULL); |
| 223 | if (glyph_width > 0) |
| 224 | width += glyph_width; |
Geoffrey Thomas | 8a9391e | 2009-01-30 04:41:28 -0500 | [diff] [blame] | 225 | } |
Patrick Steinhardt | 937b71c | 2022-12-01 15:47:04 +0100 | [diff] [blame] | 226 | |
| 227 | /* |
| 228 | * TODO: fix the interface of this function and `utf8_strwidth()` to |
| 229 | * return `size_t` instead of `int`. |
| 230 | */ |
| 231 | return cast_size_t_to_int(string ? width : len); |
Nguyễn Thái Ngọc Duy | 2bc1e7e | 2013-04-19 09:08:45 +1000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | int utf8_strwidth(const char *string) |
| 235 | { |
Patrick Steinhardt | 522cc87 | 2022-12-01 15:46:53 +0100 | [diff] [blame] | 236 | return utf8_strnwidth(string, strlen(string), 0); |
Geoffrey Thomas | 8a9391e | 2009-01-30 04:41:28 -0500 | [diff] [blame] | 237 | } |
| 238 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 239 | int is_utf8(const char *text) |
| 240 | { |
| 241 | while (*text) { |
| 242 | if (*text == '\n' || *text == '\t' || *text == '\r') { |
| 243 | text++; |
| 244 | continue; |
| 245 | } |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 246 | utf8_width(&text, NULL); |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 247 | if (!text) |
| 248 | return 0; |
| 249 | } |
| 250 | return 1; |
| 251 | } |
| 252 | |
René Scharfe | 37bb5d7 | 2009-11-22 17:15:29 +0100 | [diff] [blame] | 253 | static void strbuf_add_indented_text(struct strbuf *buf, const char *text, |
| 254 | int indent, int indent2) |
| 255 | { |
| 256 | if (indent < 0) |
| 257 | indent = 0; |
| 258 | while (*text) { |
| 259 | const char *eol = strchrnul(text, '\n'); |
| 260 | if (*eol == '\n') |
| 261 | eol++; |
René Scharfe | 3c0ff44 | 2010-02-19 23:15:55 +0100 | [diff] [blame] | 262 | strbuf_addchars(buf, ' ', indent); |
René Scharfe | 68ad5e1 | 2010-02-19 23:16:45 +0100 | [diff] [blame] | 263 | strbuf_add(buf, text, eol - text); |
René Scharfe | 37bb5d7 | 2009-11-22 17:15:29 +0100 | [diff] [blame] | 264 | text = eol; |
| 265 | indent = indent2; |
| 266 | } |
| 267 | } |
| 268 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 269 | /* |
| 270 | * Wrap the text, if necessary. The variable indent is the indent for the |
| 271 | * first line, indent2 is the indent for all other lines. |
Johannes Schindelin | 094e03b | 2007-02-27 16:20:31 +0100 | [diff] [blame] | 272 | * If indent is negative, assume that already -indent columns have been |
| 273 | * consumed (and no extra indent is necessary for the first line). |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 274 | */ |
Steffen Prohaska | e0db176 | 2012-12-11 06:59:22 +0100 | [diff] [blame] | 275 | void strbuf_add_wrapped_text(struct strbuf *buf, |
René Scharfe | 462749b | 2010-02-19 23:20:44 +0100 | [diff] [blame] | 276 | const char *text, int indent1, int indent2, int width) |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 277 | { |
René Scharfe | 462749b | 2010-02-19 23:20:44 +0100 | [diff] [blame] | 278 | int indent, w, assume_utf8 = 1; |
| 279 | const char *bol, *space, *start = text; |
| 280 | size_t orig_len = buf->len; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 281 | |
Junio C Hamano | 00d3947 | 2009-10-18 23:40:35 -0700 | [diff] [blame] | 282 | if (width <= 0) { |
René Scharfe | 462749b | 2010-02-19 23:20:44 +0100 | [diff] [blame] | 283 | strbuf_add_indented_text(buf, text, indent1, indent2); |
Steffen Prohaska | e0db176 | 2012-12-11 06:59:22 +0100 | [diff] [blame] | 284 | return; |
Junio C Hamano | 00d3947 | 2009-10-18 23:40:35 -0700 | [diff] [blame] | 285 | } |
| 286 | |
René Scharfe | 462749b | 2010-02-19 23:20:44 +0100 | [diff] [blame] | 287 | retry: |
| 288 | bol = text; |
| 289 | w = indent = indent1; |
| 290 | space = NULL; |
Johannes Schindelin | 094e03b | 2007-02-27 16:20:31 +0100 | [diff] [blame] | 291 | if (indent < 0) { |
| 292 | w = -indent; |
| 293 | space = text; |
| 294 | } |
| 295 | |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 296 | for (;;) { |
René Scharfe | 8a3c63e | 2009-11-23 23:40:03 +0100 | [diff] [blame] | 297 | char c; |
| 298 | size_t skip; |
| 299 | |
| 300 | while ((skip = display_mode_esc_sequence_len(text))) |
| 301 | text += skip; |
| 302 | |
| 303 | c = *text; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 304 | if (!c || isspace(c)) { |
Jan H. Schönherr | 14e1a4e | 2012-10-18 16:43:28 +0200 | [diff] [blame] | 305 | if (w <= width || !space) { |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 306 | const char *start = bol; |
Johannes Schindelin | ae0b270 | 2007-11-11 14:14:15 +0000 | [diff] [blame] | 307 | if (!c && text == start) |
Steffen Prohaska | e0db176 | 2012-12-11 06:59:22 +0100 | [diff] [blame] | 308 | return; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 309 | if (space) |
| 310 | start = space; |
| 311 | else |
René Scharfe | 3c0ff44 | 2010-02-19 23:15:55 +0100 | [diff] [blame] | 312 | strbuf_addchars(buf, ' ', indent); |
René Scharfe | 68ad5e1 | 2010-02-19 23:16:45 +0100 | [diff] [blame] | 313 | strbuf_add(buf, start, text - start); |
Johannes Schindelin | 094e03b | 2007-02-27 16:20:31 +0100 | [diff] [blame] | 314 | if (!c) |
Steffen Prohaska | e0db176 | 2012-12-11 06:59:22 +0100 | [diff] [blame] | 315 | return; |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 316 | space = text; |
Johannes Schindelin | ae0b270 | 2007-11-11 14:14:15 +0000 | [diff] [blame] | 317 | if (c == '\t') |
| 318 | w |= 0x07; |
| 319 | else if (c == '\n') { |
| 320 | space++; |
| 321 | if (*space == '\n') { |
René Scharfe | 68ad5e1 | 2010-02-19 23:16:45 +0100 | [diff] [blame] | 322 | strbuf_addch(buf, '\n'); |
Johannes Schindelin | ae0b270 | 2007-11-11 14:14:15 +0000 | [diff] [blame] | 323 | goto new_line; |
| 324 | } |
| 325 | else if (!isalnum(*space)) |
| 326 | goto new_line; |
| 327 | else |
René Scharfe | 68ad5e1 | 2010-02-19 23:16:45 +0100 | [diff] [blame] | 328 | strbuf_addch(buf, ' '); |
Johannes Schindelin | ae0b270 | 2007-11-11 14:14:15 +0000 | [diff] [blame] | 329 | } |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 330 | w++; |
| 331 | text++; |
| 332 | } |
| 333 | else { |
Johannes Schindelin | ae0b270 | 2007-11-11 14:14:15 +0000 | [diff] [blame] | 334 | new_line: |
René Scharfe | 68ad5e1 | 2010-02-19 23:16:45 +0100 | [diff] [blame] | 335 | strbuf_addch(buf, '\n'); |
Johannes Schindelin | 6227382 | 2007-03-02 15:28:00 +0100 | [diff] [blame] | 336 | text = bol = space + isspace(*space); |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 337 | space = NULL; |
| 338 | w = indent = indent2; |
| 339 | } |
| 340 | continue; |
| 341 | } |
René Scharfe | 462749b | 2010-02-19 23:20:44 +0100 | [diff] [blame] | 342 | if (assume_utf8) { |
Junio C Hamano | 44b25b8 | 2008-01-02 01:49:58 -0800 | [diff] [blame] | 343 | w += utf8_width(&text, NULL); |
René Scharfe | 462749b | 2010-02-19 23:20:44 +0100 | [diff] [blame] | 344 | if (!text) { |
| 345 | assume_utf8 = 0; |
| 346 | text = start; |
| 347 | strbuf_setlen(buf, orig_len); |
| 348 | goto retry; |
| 349 | } |
| 350 | } else { |
Johannes Schindelin | 9e83266 | 2006-12-22 22:06:08 +0100 | [diff] [blame] | 351 | w++; |
| 352 | text++; |
| 353 | } |
| 354 | } |
| 355 | } |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 356 | |
Steffen Prohaska | e0db176 | 2012-12-11 06:59:22 +0100 | [diff] [blame] | 357 | void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len, |
Jeff King | 98acc83 | 2011-02-23 04:50:19 -0500 | [diff] [blame] | 358 | int indent, int indent2, int width) |
| 359 | { |
| 360 | char *tmp = xstrndup(data, len); |
Steffen Prohaska | e0db176 | 2012-12-11 06:59:22 +0100 | [diff] [blame] | 361 | strbuf_add_wrapped_text(buf, tmp, indent, indent2, width); |
Jeff King | 98acc83 | 2011-02-23 04:50:19 -0500 | [diff] [blame] | 362 | free(tmp); |
Jeff King | 98acc83 | 2011-02-23 04:50:19 -0500 | [diff] [blame] | 363 | } |
| 364 | |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 365 | void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width, |
| 366 | const char *subst) |
| 367 | { |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 368 | const char *src = sb_src->buf, *end = sb_src->buf + sb_src->len; |
| 369 | struct strbuf dst; |
| 370 | int w = 0; |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 371 | |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 372 | strbuf_init(&dst, sb_src->len); |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 373 | |
| 374 | while (src < end) { |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 375 | const char *old; |
Patrick Steinhardt | 81c2d4c | 2022-12-01 15:47:10 +0100 | [diff] [blame] | 376 | int glyph_width; |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 377 | size_t n; |
| 378 | |
| 379 | while ((n = display_mode_esc_sequence_len(src))) { |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 380 | strbuf_add(&dst, src, n); |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 381 | src += n; |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 382 | } |
| 383 | |
Nguyễn Thái Ngọc Duy | 4308759 | 2014-08-10 14:05:21 +0700 | [diff] [blame] | 384 | if (src >= end) |
| 385 | break; |
| 386 | |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 387 | old = src; |
Patrick Steinhardt | 81c2d4c | 2022-12-01 15:47:10 +0100 | [diff] [blame] | 388 | glyph_width = utf8_width((const char**)&src, NULL); |
| 389 | if (!src) /* broken utf-8, do nothing */ |
Rene Scharfe | 9a012bf | 2017-08-30 20:20:16 +0200 | [diff] [blame] | 390 | goto out; |
Patrick Steinhardt | 81c2d4c | 2022-12-01 15:47:10 +0100 | [diff] [blame] | 391 | |
| 392 | /* |
| 393 | * In case we see a control character we copy it into the |
| 394 | * buffer, but don't add it to the width. |
| 395 | */ |
| 396 | if (glyph_width < 0) |
| 397 | glyph_width = 0; |
| 398 | |
| 399 | if (glyph_width && w >= pos && w < pos + width) { |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 400 | if (subst) { |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 401 | strbuf_addstr(&dst, subst); |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 402 | subst = NULL; |
| 403 | } |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 404 | } else { |
| 405 | strbuf_add(&dst, old, src - old); |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 406 | } |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 407 | |
Patrick Steinhardt | 81c2d4c | 2022-12-01 15:47:10 +0100 | [diff] [blame] | 408 | w += glyph_width; |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 409 | } |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 410 | |
| 411 | strbuf_swap(sb_src, &dst); |
Rene Scharfe | 9a012bf | 2017-08-30 20:20:16 +0200 | [diff] [blame] | 412 | out: |
Patrick Steinhardt | f930a23 | 2022-12-01 15:47:15 +0100 | [diff] [blame] | 413 | strbuf_release(&dst); |
Nguyễn Thái Ngọc Duy | a7f01c6 | 2013-04-19 09:08:51 +1000 | [diff] [blame] | 414 | } |
| 415 | |
Lars Schneider | 2f0c4a3 | 2018-04-15 20:16:04 +0200 | [diff] [blame] | 416 | /* |
| 417 | * Returns true (1) if the src encoding name matches the dst encoding |
| 418 | * name directly or one of its alternative names. E.g. UTF-16BE is the |
| 419 | * same as UTF16BE. |
| 420 | */ |
| 421 | static int same_utf_encoding(const char *src, const char *dst) |
| 422 | { |
René Scharfe | 89f8cab | 2019-11-08 21:25:21 +0100 | [diff] [blame] | 423 | if (skip_iprefix(src, "utf", &src) && skip_iprefix(dst, "utf", &dst)) { |
| 424 | skip_prefix(src, "-", &src); |
| 425 | skip_prefix(dst, "-", &dst); |
| 426 | return !strcasecmp(src, dst); |
Lars Schneider | 2f0c4a3 | 2018-04-15 20:16:04 +0200 | [diff] [blame] | 427 | } |
| 428 | return 0; |
| 429 | } |
| 430 | |
Junio C Hamano | 677cfed | 2006-12-30 12:20:43 -0800 | [diff] [blame] | 431 | int is_encoding_utf8(const char *name) |
| 432 | { |
| 433 | if (!name) |
| 434 | return 1; |
Lars Schneider | 2f0c4a3 | 2018-04-15 20:16:04 +0200 | [diff] [blame] | 435 | if (same_utf_encoding("utf-8", name)) |
Junio C Hamano | 677cfed | 2006-12-30 12:20:43 -0800 | [diff] [blame] | 436 | return 1; |
| 437 | return 0; |
| 438 | } |
| 439 | |
Junio C Hamano | 0e18bcd | 2012-10-18 22:41:56 -0700 | [diff] [blame] | 440 | int same_encoding(const char *src, const char *dst) |
| 441 | { |
Lars Schneider | 2f0c4a3 | 2018-04-15 20:16:04 +0200 | [diff] [blame] | 442 | static const char utf8[] = "UTF-8"; |
| 443 | |
| 444 | if (!src) |
| 445 | src = utf8; |
| 446 | if (!dst) |
| 447 | dst = utf8; |
| 448 | if (same_utf_encoding(src, dst)) |
Junio C Hamano | 0e18bcd | 2012-10-18 22:41:56 -0700 | [diff] [blame] | 449 | return 1; |
| 450 | return !strcasecmp(src, dst); |
| 451 | } |
| 452 | |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 453 | /* |
Jiang Xin | c082196 | 2013-02-09 14:31:09 +0800 | [diff] [blame] | 454 | * Wrapper for fprintf and returns the total number of columns required |
| 455 | * for the printed string, assuming that the string is utf8. |
| 456 | */ |
| 457 | int utf8_fprintf(FILE *stream, const char *format, ...) |
| 458 | { |
| 459 | struct strbuf buf = STRBUF_INIT; |
| 460 | va_list arg; |
| 461 | int columns; |
| 462 | |
| 463 | va_start(arg, format); |
| 464 | strbuf_vaddf(&buf, format, arg); |
| 465 | va_end(arg); |
| 466 | |
| 467 | columns = fputs(buf.buf, stream); |
| 468 | if (0 <= columns) /* keep the error from the I/O */ |
| 469 | columns = utf8_strwidth(buf.buf); |
| 470 | strbuf_release(&buf); |
| 471 | return columns; |
| 472 | } |
| 473 | |
| 474 | /* |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 475 | * Given a buffer and its encoding, return it re-encoded |
| 476 | * with iconv. If the conversion fails, returns NULL. |
| 477 | */ |
| 478 | #ifndef NO_ICONV |
Brandon Casey | 309dbc8 | 2009-06-05 18:36:12 -0500 | [diff] [blame] | 479 | #if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6)) |
Ramsay Jones | fd547a9 | 2007-03-03 18:29:03 +0000 | [diff] [blame] | 480 | typedef const char * iconv_ibp; |
| 481 | #else |
| 482 | typedef char * iconv_ibp; |
| 483 | #endif |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 484 | char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, |
| 485 | size_t bom_len, size_t *outsz_p) |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 486 | { |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 487 | size_t outsz, outalloc; |
Ramsay Jones | fd547a9 | 2007-03-03 18:29:03 +0000 | [diff] [blame] | 488 | char *out, *outpos; |
| 489 | iconv_ibp cp; |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 490 | |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 491 | outsz = insz; |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 492 | outalloc = st_add(outsz, 1 + bom_len); /* for terminating NUL */ |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 493 | out = xmalloc(outalloc); |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 494 | outpos = out + bom_len; |
Ramsay Jones | fd547a9 | 2007-03-03 18:29:03 +0000 | [diff] [blame] | 495 | cp = (iconv_ibp)in; |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 496 | |
| 497 | while (1) { |
| 498 | size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz); |
| 499 | |
John Keeping | df5213b | 2014-02-16 16:06:03 +0000 | [diff] [blame] | 500 | if (cnt == (size_t) -1) { |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 501 | size_t sofar; |
| 502 | if (errno != E2BIG) { |
| 503 | free(out); |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 504 | return NULL; |
| 505 | } |
| 506 | /* insz has remaining number of bytes. |
| 507 | * since we started outsz the same as insz, |
| 508 | * it is likely that insz is not enough for |
| 509 | * converting the rest. |
| 510 | */ |
| 511 | sofar = outpos - out; |
Jeff King | 77aa03d | 2018-07-24 06:50:10 -0400 | [diff] [blame] | 512 | outalloc = st_add3(sofar, st_mult(insz, 2), 32); |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 513 | out = xrealloc(out, outalloc); |
| 514 | outpos = out + sofar; |
| 515 | outsz = outalloc - sofar - 1; |
| 516 | } |
| 517 | else { |
| 518 | *outpos = '\0'; |
Nguyễn Thái Ngọc Duy | b782bba | 2013-04-19 09:08:46 +1000 | [diff] [blame] | 519 | if (outsz_p) |
| 520 | *outsz_p = outpos - out; |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | } |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 524 | return out; |
| 525 | } |
| 526 | |
Junio C Hamano | 3270741 | 2016-09-26 18:09:48 -0700 | [diff] [blame] | 527 | static const char *fallback_encoding(const char *name) |
| 528 | { |
| 529 | /* |
| 530 | * Some platforms do not have the variously spelled variants of |
| 531 | * UTF-8, so let's fall back to trying the most official |
| 532 | * spelling. We do so only as a fallback in case the platform |
| 533 | * does understand the user's spelling, but not our official |
| 534 | * one. |
| 535 | */ |
| 536 | if (is_encoding_utf8(name)) |
| 537 | return "UTF-8"; |
| 538 | |
Junio C Hamano | df37558 | 2016-09-26 18:09:48 -0700 | [diff] [blame] | 539 | /* |
| 540 | * Even though latin-1 is still seen in e-mail |
| 541 | * headers, some platforms only install ISO-8859-1. |
| 542 | */ |
| 543 | if (!strcasecmp(name, "latin-1")) |
| 544 | return "ISO-8859-1"; |
| 545 | |
Junio C Hamano | 3270741 | 2016-09-26 18:09:48 -0700 | [diff] [blame] | 546 | return name; |
| 547 | } |
| 548 | |
Jeff King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 549 | char *reencode_string_len(const char *in, size_t insz, |
Nguyễn Thái Ngọc Duy | b782bba | 2013-04-19 09:08:46 +1000 | [diff] [blame] | 550 | const char *out_encoding, const char *in_encoding, |
Jeff King | c7d017d | 2018-07-24 06:50:33 -0400 | [diff] [blame] | 551 | size_t *outsz) |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 552 | { |
| 553 | iconv_t conv; |
| 554 | char *out; |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 555 | const char *bom_str = NULL; |
| 556 | size_t bom_len = 0; |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 557 | |
| 558 | if (!in_encoding) |
| 559 | return NULL; |
Jeff King | 5c680be | 2013-02-25 15:31:00 -0500 | [diff] [blame] | 560 | |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 561 | /* UTF-16LE-BOM is the same as UTF-16 for reading */ |
| 562 | if (same_utf_encoding("UTF-16LE-BOM", in_encoding)) |
| 563 | in_encoding = "UTF-16"; |
| 564 | |
| 565 | /* |
| 566 | * For writing, UTF-16 iconv typically creates "UTF-16BE-BOM" |
| 567 | * Some users under Windows want the little endian version |
brian m. carlson | 79444c9 | 2019-02-12 00:52:06 +0000 | [diff] [blame] | 568 | * |
| 569 | * We handle UTF-16 and UTF-32 ourselves only if the platform does not |
| 570 | * provide a BOM (which we require), since we want to match the behavior |
| 571 | * of the system tools and libc as much as possible. |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 572 | */ |
| 573 | if (same_utf_encoding("UTF-16LE-BOM", out_encoding)) { |
| 574 | bom_str = utf16_le_bom; |
| 575 | bom_len = sizeof(utf16_le_bom); |
| 576 | out_encoding = "UTF-16LE"; |
| 577 | } else if (same_utf_encoding("UTF-16BE-BOM", out_encoding)) { |
| 578 | bom_str = utf16_be_bom; |
| 579 | bom_len = sizeof(utf16_be_bom); |
| 580 | out_encoding = "UTF-16BE"; |
brian m. carlson | 79444c9 | 2019-02-12 00:52:06 +0000 | [diff] [blame] | 581 | #ifdef ICONV_OMITS_BOM |
| 582 | } else if (same_utf_encoding("UTF-16", out_encoding)) { |
| 583 | bom_str = utf16_be_bom; |
| 584 | bom_len = sizeof(utf16_be_bom); |
| 585 | out_encoding = "UTF-16BE"; |
| 586 | } else if (same_utf_encoding("UTF-32", out_encoding)) { |
| 587 | bom_str = utf32_be_bom; |
| 588 | bom_len = sizeof(utf32_be_bom); |
| 589 | out_encoding = "UTF-32BE"; |
| 590 | #endif |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 591 | } |
| 592 | |
Torsten Bögershausen | 76759c7 | 2012-07-08 15:50:25 +0200 | [diff] [blame] | 593 | conv = iconv_open(out_encoding, in_encoding); |
Jeff King | 5c680be | 2013-02-25 15:31:00 -0500 | [diff] [blame] | 594 | if (conv == (iconv_t) -1) { |
Junio C Hamano | 3270741 | 2016-09-26 18:09:48 -0700 | [diff] [blame] | 595 | in_encoding = fallback_encoding(in_encoding); |
| 596 | out_encoding = fallback_encoding(out_encoding); |
| 597 | |
Jeff King | 5c680be | 2013-02-25 15:31:00 -0500 | [diff] [blame] | 598 | conv = iconv_open(out_encoding, in_encoding); |
| 599 | if (conv == (iconv_t) -1) |
| 600 | return NULL; |
| 601 | } |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 602 | out = reencode_string_iconv(in, insz, conv, bom_len, outsz); |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 603 | iconv_close(conv); |
Torsten Bögershausen | aab2a1a | 2019-01-30 16:01:52 +0100 | [diff] [blame] | 604 | if (out && bom_str && bom_len) |
| 605 | memcpy(out, bom_str, bom_len); |
Junio C Hamano | b45974a | 2006-12-23 23:36:55 -0800 | [diff] [blame] | 606 | return out; |
| 607 | } |
| 608 | #endif |
Kirill Smelkov | 6cd3c05 | 2013-03-07 14:55:07 +0400 | [diff] [blame] | 609 | |
Lars Schneider | 10ecb82 | 2018-04-15 20:16:05 +0200 | [diff] [blame] | 610 | static int has_bom_prefix(const char *data, size_t len, |
| 611 | const char *bom, size_t bom_len) |
| 612 | { |
| 613 | return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len); |
| 614 | } |
| 615 | |
Lars Schneider | 10ecb82 | 2018-04-15 20:16:05 +0200 | [diff] [blame] | 616 | int has_prohibited_utf_bom(const char *enc, const char *data, size_t len) |
| 617 | { |
| 618 | return ( |
| 619 | (same_utf_encoding("UTF-16BE", enc) || |
| 620 | same_utf_encoding("UTF-16LE", enc)) && |
| 621 | (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) || |
| 622 | has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom))) |
| 623 | ) || ( |
| 624 | (same_utf_encoding("UTF-32BE", enc) || |
| 625 | same_utf_encoding("UTF-32LE", enc)) && |
| 626 | (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) || |
| 627 | has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom))) |
| 628 | ); |
| 629 | } |
| 630 | |
Lars Schneider | c6e4865 | 2018-04-15 20:16:06 +0200 | [diff] [blame] | 631 | int is_missing_required_utf_bom(const char *enc, const char *data, size_t len) |
| 632 | { |
| 633 | return ( |
| 634 | (same_utf_encoding(enc, "UTF-16")) && |
| 635 | !(has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) || |
| 636 | has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom))) |
| 637 | ) || ( |
| 638 | (same_utf_encoding(enc, "UTF-32")) && |
| 639 | !(has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) || |
| 640 | has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom))) |
| 641 | ); |
| 642 | } |
| 643 | |
Kirill Smelkov | 6cd3c05 | 2013-03-07 14:55:07 +0400 | [diff] [blame] | 644 | /* |
| 645 | * Returns first character length in bytes for multi-byte `text` according to |
| 646 | * `encoding`. |
| 647 | * |
| 648 | * - The `text` pointer is updated to point at the next character. |
| 649 | * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes |
| 650 | * we can consume from text, and on exit `*remainder_p` is reduced by returned |
| 651 | * character length. Otherwise `text` is treated as limited by NUL. |
| 652 | */ |
| 653 | int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding) |
| 654 | { |
| 655 | int chrlen; |
| 656 | const char *p = *text; |
| 657 | size_t r = (remainder_p ? *remainder_p : SIZE_MAX); |
| 658 | |
| 659 | if (r < 1) |
| 660 | return 0; |
| 661 | |
| 662 | if (is_encoding_utf8(encoding)) { |
| 663 | pick_one_utf8_char(&p, &r); |
| 664 | |
| 665 | chrlen = p ? (p - *text) |
| 666 | : 1 /* not valid UTF-8 -> raw byte sequence */; |
| 667 | } |
| 668 | else { |
| 669 | /* |
| 670 | * TODO use iconv to decode one char and obtain its chrlen |
| 671 | * for now, let's treat encodings != UTF-8 as one-byte |
| 672 | */ |
| 673 | chrlen = 1; |
| 674 | } |
| 675 | |
| 676 | *text += chrlen; |
| 677 | if (remainder_p) |
| 678 | *remainder_p -= chrlen; |
| 679 | |
| 680 | return chrlen; |
| 681 | } |
Jeff King | 6162a1d | 2014-12-15 17:56:59 -0500 | [diff] [blame] | 682 | |
| 683 | /* |
Jeff King | 6aaf956 | 2014-12-23 03:45:36 -0500 | [diff] [blame] | 684 | * Pick the next char from the stream, ignoring codepoints an HFS+ would. |
| 685 | * Note that this is _not_ complete by any means. It's just enough |
Jeff King | 6162a1d | 2014-12-15 17:56:59 -0500 | [diff] [blame] | 686 | * to make is_hfs_dotgit() work, and should not be used otherwise. |
| 687 | */ |
| 688 | static ucs_char_t next_hfs_char(const char **in) |
| 689 | { |
| 690 | while (1) { |
| 691 | ucs_char_t out = pick_one_utf8_char(in, NULL); |
| 692 | /* |
| 693 | * check for malformed utf8. Technically this |
| 694 | * gets converted to a percent-sequence, but |
| 695 | * returning 0 is good enough for is_hfs_dotgit |
| 696 | * to realize it cannot be .git |
| 697 | */ |
| 698 | if (!*in) |
| 699 | return 0; |
| 700 | |
| 701 | /* these code points are ignored completely */ |
| 702 | switch (out) { |
| 703 | case 0x200c: /* ZERO WIDTH NON-JOINER */ |
| 704 | case 0x200d: /* ZERO WIDTH JOINER */ |
| 705 | case 0x200e: /* LEFT-TO-RIGHT MARK */ |
| 706 | case 0x200f: /* RIGHT-TO-LEFT MARK */ |
| 707 | case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */ |
| 708 | case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */ |
| 709 | case 0x202c: /* POP DIRECTIONAL FORMATTING */ |
| 710 | case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */ |
| 711 | case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */ |
| 712 | case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */ |
| 713 | case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */ |
| 714 | case 0x206c: /* INHIBIT ARABIC FORM SHAPING */ |
| 715 | case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */ |
| 716 | case 0x206e: /* NATIONAL DIGIT SHAPES */ |
| 717 | case 0x206f: /* NOMINAL DIGIT SHAPES */ |
| 718 | case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */ |
| 719 | continue; |
| 720 | } |
| 721 | |
Jeff King | 6aaf956 | 2014-12-23 03:45:36 -0500 | [diff] [blame] | 722 | return out; |
Jeff King | 6162a1d | 2014-12-15 17:56:59 -0500 | [diff] [blame] | 723 | } |
| 724 | } |
| 725 | |
Jeff King | 0fc333b | 2018-05-02 15:23:45 -0400 | [diff] [blame] | 726 | static int is_hfs_dot_generic(const char *path, |
| 727 | const char *needle, size_t needle_len) |
Jeff King | 6162a1d | 2014-12-15 17:56:59 -0500 | [diff] [blame] | 728 | { |
| 729 | ucs_char_t c; |
| 730 | |
Jeff King | 6aaf956 | 2014-12-23 03:45:36 -0500 | [diff] [blame] | 731 | c = next_hfs_char(&path); |
| 732 | if (c != '.') |
| 733 | return 0; |
Jeff King | 6aaf956 | 2014-12-23 03:45:36 -0500 | [diff] [blame] | 734 | |
| 735 | /* |
| 736 | * there's a great deal of other case-folding that occurs |
Jeff King | 0fc333b | 2018-05-02 15:23:45 -0400 | [diff] [blame] | 737 | * in HFS+, but this is enough to catch our fairly vanilla |
| 738 | * hard-coded needles. |
Jeff King | 6aaf956 | 2014-12-23 03:45:36 -0500 | [diff] [blame] | 739 | */ |
Jeff King | 0fc333b | 2018-05-02 15:23:45 -0400 | [diff] [blame] | 740 | for (; needle_len > 0; needle++, needle_len--) { |
| 741 | c = next_hfs_char(&path); |
| 742 | |
| 743 | /* |
| 744 | * We know our needles contain only ASCII, so we clamp here to |
| 745 | * make the results of tolower() sane. |
| 746 | */ |
| 747 | if (c > 127) |
| 748 | return 0; |
| 749 | if (tolower(c) != *needle) |
| 750 | return 0; |
| 751 | } |
| 752 | |
Jeff King | 6162a1d | 2014-12-15 17:56:59 -0500 | [diff] [blame] | 753 | c = next_hfs_char(&path); |
| 754 | if (c && !is_dir_sep(c)) |
| 755 | return 0; |
| 756 | |
| 757 | return 1; |
| 758 | } |
Junio C Hamano | dde843e | 2015-04-16 10:45:29 -0700 | [diff] [blame] | 759 | |
Jeff King | 0fc333b | 2018-05-02 15:23:45 -0400 | [diff] [blame] | 760 | /* |
| 761 | * Inline wrapper to make sure the compiler resolves strlen() on literals at |
| 762 | * compile time. |
| 763 | */ |
| 764 | static inline int is_hfs_dot_str(const char *path, const char *needle) |
| 765 | { |
| 766 | return is_hfs_dot_generic(path, needle, strlen(needle)); |
| 767 | } |
| 768 | |
| 769 | int is_hfs_dotgit(const char *path) |
| 770 | { |
| 771 | return is_hfs_dot_str(path, "git"); |
| 772 | } |
| 773 | |
| 774 | int is_hfs_dotgitmodules(const char *path) |
| 775 | { |
| 776 | return is_hfs_dot_str(path, "gitmodules"); |
| 777 | } |
| 778 | |
| 779 | int is_hfs_dotgitignore(const char *path) |
| 780 | { |
| 781 | return is_hfs_dot_str(path, "gitignore"); |
| 782 | } |
| 783 | |
| 784 | int is_hfs_dotgitattributes(const char *path) |
| 785 | { |
| 786 | return is_hfs_dot_str(path, "gitattributes"); |
| 787 | } |
| 788 | |
Jeff King | 801ed01 | 2021-05-03 16:43:22 -0400 | [diff] [blame] | 789 | int is_hfs_dotmailmap(const char *path) |
| 790 | { |
| 791 | return is_hfs_dot_str(path, "mailmap"); |
| 792 | } |
| 793 | |
Junio C Hamano | dde843e | 2015-04-16 10:45:29 -0700 | [diff] [blame] | 794 | const char utf8_bom[] = "\357\273\277"; |
| 795 | |
| 796 | int skip_utf8_bom(char **text, size_t len) |
| 797 | { |
| 798 | if (len < strlen(utf8_bom) || |
| 799 | memcmp(*text, utf8_bom, strlen(utf8_bom))) |
| 800 | return 0; |
| 801 | *text += strlen(utf8_bom); |
| 802 | return 1; |
| 803 | } |
Karthik Nayak | 110dcda | 2015-09-10 21:18:19 +0530 | [diff] [blame] | 804 | |
| 805 | void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width, |
| 806 | const char *s) |
| 807 | { |
Patrick Steinhardt | 522cc87 | 2022-12-01 15:46:53 +0100 | [diff] [blame] | 808 | size_t slen = strlen(s); |
Karthik Nayak | 110dcda | 2015-09-10 21:18:19 +0530 | [diff] [blame] | 809 | int display_len = utf8_strnwidth(s, slen, 0); |
| 810 | int utf8_compensation = slen - display_len; |
| 811 | |
| 812 | if (display_len >= width) { |
| 813 | strbuf_addstr(buf, s); |
| 814 | return; |
| 815 | } |
| 816 | |
| 817 | if (position == ALIGN_LEFT) |
| 818 | strbuf_addf(buf, "%-*s", width + utf8_compensation, s); |
| 819 | else if (position == ALIGN_MIDDLE) { |
| 820 | int left = (width - display_len) / 2; |
| 821 | strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s); |
| 822 | } else if (position == ALIGN_RIGHT) |
| 823 | strbuf_addf(buf, "%*s", width + utf8_compensation, s); |
| 824 | } |