]> git.ipfire.org Git - thirdparty/git.git/blame - utf8.c
Git 2.24
[thirdparty/git.git] / utf8.c
CommitLineData
9e832665 1#include "git-compat-util.h"
a94410c8 2#include "strbuf.h"
9e832665
JS
3#include "utf8.h"
4
5/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
6
aab2a1ae
TB
7static const char utf16_be_bom[] = {'\xFE', '\xFF'};
8static const char utf16_le_bom[] = {'\xFF', '\xFE'};
9static const char utf32_be_bom[] = {'\0', '\0', '\xFE', '\xFF'};
10static const char utf32_le_bom[] = {'\xFF', '\xFE', '\0', '\0'};
11
9e832665 12struct interval {
a68a67de
JK
13 ucs_char_t first;
14 ucs_char_t last;
9e832665
JS
15};
16
1640632b 17size_t display_mode_esc_sequence_len(const char *s)
4247fe79
NTND
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
9e832665 31/* auxiliary function for binary search in interval table */
f3fa1838
JH
32static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
33{
9e832665
JS
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) {
19716b21 40 mid = min + (max - min) / 2;
9e832665
JS
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 *
28321145 80 * This implementation assumes that ucs_char_t characters are encoded
9e832665
JS
81 * in ISO 10646.
82 */
83
b51be13c 84static int git_wcwidth(ucs_char_t ch)
9e832665
JS
85{
86 /*
87 * Sorted list of non-overlapping intervals of non-spacing characters,
9e832665 88 */
e233bef4 89#include "unicode-width.h"
9e832665
JS
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 */
fa364ad7 98 if (bisearch(ch, zero_width, ARRAY_SIZE(zero_width) - 1))
9e832665
JS
99 return 0;
100
08460345 101 /* binary search in table of double width characters */
fa364ad7 102 if (bisearch(ch, double_width, ARRAY_SIZE(double_width) - 1))
08460345 103 return 2;
9e832665 104
08460345 105 return 1;
9e832665
JS
106}
107
108/*
396ccf1f
JH
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
44b25b87
JH
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.
396ccf1f
JH
116 *
117 * If the string was not a valid UTF-8, *start pointer is set to NULL
118 * and the return value is undefined.
9e832665 119 */
5e133b8c 120static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p)
9e832665
JS
121{
122 unsigned char *s = (unsigned char *)*start;
28321145 123 ucs_char_t ch;
44b25b87 124 size_t remainder, incr;
9e832665 125
44b25b87
JH
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) {
9e832665
JS
136 /* 0xxxxxxx */
137 ch = *s;
44b25b87 138 incr = 1;
9e832665
JS
139 } else if ((s[0] & 0xe0) == 0xc0) {
140 /* 110XXXXx 10xxxxxx */
44b25b87
JH
141 if (remainder < 2 ||
142 (s[1] & 0xc0) != 0x80 ||
143 (s[0] & 0xfe) == 0xc0)
9e832665
JS
144 goto invalid;
145 ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
44b25b87 146 incr = 2;
9e832665
JS
147 } else if ((s[0] & 0xf0) == 0xe0) {
148 /* 1110XXXX 10Xxxxxx 10xxxxxx */
44b25b87
JH
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))
9e832665
JS
159 goto invalid;
160 ch = ((s[0] & 0x0f) << 12) |
161 ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
44b25b87 162 incr = 3;
9e832665
JS
163 } else if ((s[0] & 0xf8) == 0xf0) {
164 /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
44b25b87
JH
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)
9e832665
JS
173 goto invalid;
174 ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
175 ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
44b25b87 176 incr = 4;
9e832665
JS
177 } else {
178invalid:
179 *start = NULL;
180 return 0;
181 }
182
44b25b87
JH
183 *start += incr;
184 if (remainder_p)
185 *remainder_p = remainder - incr;
396ccf1f
JH
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
44b25b87
JH
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).
396ccf1f 195 */
44b25b87 196int utf8_width(const char **start, size_t *remainder_p)
396ccf1f 197{
44b25b87 198 ucs_char_t ch = pick_one_utf8_char(start, remainder_p);
396ccf1f
JH
199 if (!*start)
200 return 0;
b51be13c 201 return git_wcwidth(ch);
9e832665
JS
202}
203
8a9391e9
GT
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 */
2bc1e7ec 209int utf8_strnwidth(const char *string, int len, int skip_ansi)
8a9391e9
GT
210{
211 int width = 0;
212 const char *orig = string;
213
2bc1e7ec
NTND
214 if (len == -1)
215 len = strlen(string);
216 while (string && string < orig + len) {
217 int skip;
218 while (skip_ansi &&
219 (skip = display_mode_esc_sequence_len(string)) != 0)
220 string += skip;
8a9391e9
GT
221 width += utf8_width(&string, NULL);
222 }
2bc1e7ec
NTND
223 return string ? width : len;
224}
225
226int utf8_strwidth(const char *string)
227{
228 return utf8_strnwidth(string, -1, 0);
8a9391e9
GT
229}
230
9e832665
JS
231int is_utf8(const char *text)
232{
233 while (*text) {
234 if (*text == '\n' || *text == '\t' || *text == '\r') {
235 text++;
236 continue;
237 }
44b25b87 238 utf8_width(&text, NULL);
9e832665
JS
239 if (!text)
240 return 0;
241 }
242 return 1;
243}
244
37bb5d74
RS
245static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
246 int indent, int indent2)
247{
248 if (indent < 0)
249 indent = 0;
250 while (*text) {
251 const char *eol = strchrnul(text, '\n');
252 if (*eol == '\n')
253 eol++;
3c0ff44a 254 strbuf_addchars(buf, ' ', indent);
68ad5e1e 255 strbuf_add(buf, text, eol - text);
37bb5d74
RS
256 text = eol;
257 indent = indent2;
258 }
259}
260
9e832665
JS
261/*
262 * Wrap the text, if necessary. The variable indent is the indent for the
263 * first line, indent2 is the indent for all other lines.
094e03b0
JS
264 * If indent is negative, assume that already -indent columns have been
265 * consumed (and no extra indent is necessary for the first line).
9e832665 266 */
e0db1765 267void strbuf_add_wrapped_text(struct strbuf *buf,
462749b7 268 const char *text, int indent1, int indent2, int width)
9e832665 269{
462749b7
RS
270 int indent, w, assume_utf8 = 1;
271 const char *bol, *space, *start = text;
272 size_t orig_len = buf->len;
9e832665 273
00d39473 274 if (width <= 0) {
462749b7 275 strbuf_add_indented_text(buf, text, indent1, indent2);
e0db1765 276 return;
00d39473
JH
277 }
278
462749b7
RS
279retry:
280 bol = text;
281 w = indent = indent1;
282 space = NULL;
094e03b0
JS
283 if (indent < 0) {
284 w = -indent;
285 space = text;
286 }
287
9e832665 288 for (;;) {
8a3c63e0
RS
289 char c;
290 size_t skip;
291
292 while ((skip = display_mode_esc_sequence_len(text)))
293 text += skip;
294
295 c = *text;
9e832665 296 if (!c || isspace(c)) {
14e1a4e1 297 if (w <= width || !space) {
9e832665 298 const char *start = bol;
ae0b2702 299 if (!c && text == start)
e0db1765 300 return;
9e832665
JS
301 if (space)
302 start = space;
303 else
3c0ff44a 304 strbuf_addchars(buf, ' ', indent);
68ad5e1e 305 strbuf_add(buf, start, text - start);
094e03b0 306 if (!c)
e0db1765 307 return;
9e832665 308 space = text;
ae0b2702
JS
309 if (c == '\t')
310 w |= 0x07;
311 else if (c == '\n') {
312 space++;
313 if (*space == '\n') {
68ad5e1e 314 strbuf_addch(buf, '\n');
ae0b2702
JS
315 goto new_line;
316 }
317 else if (!isalnum(*space))
318 goto new_line;
319 else
68ad5e1e 320 strbuf_addch(buf, ' ');
ae0b2702 321 }
9e832665
JS
322 w++;
323 text++;
324 }
325 else {
ae0b2702 326new_line:
68ad5e1e 327 strbuf_addch(buf, '\n');
62273826 328 text = bol = space + isspace(*space);
9e832665
JS
329 space = NULL;
330 w = indent = indent2;
331 }
332 continue;
333 }
462749b7 334 if (assume_utf8) {
44b25b87 335 w += utf8_width(&text, NULL);
462749b7
RS
336 if (!text) {
337 assume_utf8 = 0;
338 text = start;
339 strbuf_setlen(buf, orig_len);
340 goto retry;
341 }
342 } else {
9e832665
JS
343 w++;
344 text++;
345 }
346 }
347}
b45974a6 348
e0db1765 349void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
98acc837
JK
350 int indent, int indent2, int width)
351{
352 char *tmp = xstrndup(data, len);
e0db1765 353 strbuf_add_wrapped_text(buf, tmp, indent, indent2, width);
98acc837 354 free(tmp);
98acc837
JK
355}
356
a7f01c6b
NTND
357void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
358 const char *subst)
359{
360 struct strbuf sb_dst = STRBUF_INIT;
361 char *src = sb_src->buf;
362 char *end = src + sb_src->len;
363 char *dst;
364 int w = 0, subst_len = 0;
365
366 if (subst)
367 subst_len = strlen(subst);
368 strbuf_grow(&sb_dst, sb_src->len + subst_len);
369 dst = sb_dst.buf;
370
371 while (src < end) {
372 char *old;
373 size_t n;
374
375 while ((n = display_mode_esc_sequence_len(src))) {
376 memcpy(dst, src, n);
377 src += n;
378 dst += n;
379 }
380
43087596
NTND
381 if (src >= end)
382 break;
383
a7f01c6b
NTND
384 old = src;
385 n = utf8_width((const char**)&src, NULL);
386 if (!src) /* broken utf-8, do nothing */
9a012bf3 387 goto out;
a7f01c6b
NTND
388 if (n && w >= pos && w < pos + width) {
389 if (subst) {
390 memcpy(dst, subst, subst_len);
391 dst += subst_len;
392 subst = NULL;
393 }
394 w += n;
395 continue;
396 }
397 memcpy(dst, old, src - old);
398 dst += src - old;
399 w += n;
400 }
401 strbuf_setlen(&sb_dst, dst - sb_dst.buf);
980419b9 402 strbuf_swap(sb_src, &sb_dst);
9a012bf3 403out:
980419b9 404 strbuf_release(&sb_dst);
a7f01c6b
NTND
405}
406
2f0c4a36
LS
407/*
408 * Returns true (1) if the src encoding name matches the dst encoding
409 * name directly or one of its alternative names. E.g. UTF-16BE is the
410 * same as UTF16BE.
411 */
412static int same_utf_encoding(const char *src, const char *dst)
413{
414 if (istarts_with(src, "utf") && istarts_with(dst, "utf")) {
415 /* src[3] or dst[3] might be '\0' */
416 int i = (src[3] == '-' ? 4 : 3);
417 int j = (dst[3] == '-' ? 4 : 3);
418 return !strcasecmp(src+i, dst+j);
419 }
420 return 0;
421}
422
677cfed5
JH
423int is_encoding_utf8(const char *name)
424{
425 if (!name)
426 return 1;
2f0c4a36 427 if (same_utf_encoding("utf-8", name))
677cfed5
JH
428 return 1;
429 return 0;
430}
431
0e18bcd5
JH
432int same_encoding(const char *src, const char *dst)
433{
2f0c4a36
LS
434 static const char utf8[] = "UTF-8";
435
436 if (!src)
437 src = utf8;
438 if (!dst)
439 dst = utf8;
440 if (same_utf_encoding(src, dst))
0e18bcd5
JH
441 return 1;
442 return !strcasecmp(src, dst);
443}
444
c0821965
JX
445/*
446 * Wrapper for fprintf and returns the total number of columns required
447 * for the printed string, assuming that the string is utf8.
448 */
449int utf8_fprintf(FILE *stream, const char *format, ...)
450{
451 struct strbuf buf = STRBUF_INIT;
452 va_list arg;
453 int columns;
454
455 va_start(arg, format);
456 strbuf_vaddf(&buf, format, arg);
457 va_end(arg);
458
459 columns = fputs(buf.buf, stream);
460 if (0 <= columns) /* keep the error from the I/O */
461 columns = utf8_strwidth(buf.buf);
462 strbuf_release(&buf);
463 return columns;
464}
465
b45974a6
JH
466/*
467 * Given a buffer and its encoding, return it re-encoded
468 * with iconv. If the conversion fails, returns NULL.
469 */
470#ifndef NO_ICONV
309dbc82 471#if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6))
fd547a97
RJ
472 typedef const char * iconv_ibp;
473#else
474 typedef char * iconv_ibp;
475#endif
aab2a1ae
TB
476char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv,
477 size_t bom_len, size_t *outsz_p)
b45974a6 478{
76759c7d 479 size_t outsz, outalloc;
fd547a97
RJ
480 char *out, *outpos;
481 iconv_ibp cp;
b45974a6 482
b45974a6 483 outsz = insz;
aab2a1ae 484 outalloc = st_add(outsz, 1 + bom_len); /* for terminating NUL */
b45974a6 485 out = xmalloc(outalloc);
aab2a1ae 486 outpos = out + bom_len;
fd547a97 487 cp = (iconv_ibp)in;
b45974a6
JH
488
489 while (1) {
490 size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
491
df5213b7 492 if (cnt == (size_t) -1) {
b45974a6
JH
493 size_t sofar;
494 if (errno != E2BIG) {
495 free(out);
b45974a6
JH
496 return NULL;
497 }
498 /* insz has remaining number of bytes.
499 * since we started outsz the same as insz,
500 * it is likely that insz is not enough for
501 * converting the rest.
502 */
503 sofar = outpos - out;
77aa03d6 504 outalloc = st_add3(sofar, st_mult(insz, 2), 32);
b45974a6
JH
505 out = xrealloc(out, outalloc);
506 outpos = out + sofar;
507 outsz = outalloc - sofar - 1;
508 }
509 else {
510 *outpos = '\0';
b782bbab
NTND
511 if (outsz_p)
512 *outsz_p = outpos - out;
b45974a6
JH
513 break;
514 }
515 }
76759c7d
TB
516 return out;
517}
518
3270741e
JH
519static const char *fallback_encoding(const char *name)
520{
521 /*
522 * Some platforms do not have the variously spelled variants of
523 * UTF-8, so let's fall back to trying the most official
524 * spelling. We do so only as a fallback in case the platform
525 * does understand the user's spelling, but not our official
526 * one.
527 */
528 if (is_encoding_utf8(name))
529 return "UTF-8";
530
df375588
JH
531 /*
532 * Even though latin-1 is still seen in e-mail
533 * headers, some platforms only install ISO-8859-1.
534 */
535 if (!strcasecmp(name, "latin-1"))
536 return "ISO-8859-1";
537
3270741e
JH
538 return name;
539}
540
c7d017d7 541char *reencode_string_len(const char *in, size_t insz,
b782bbab 542 const char *out_encoding, const char *in_encoding,
c7d017d7 543 size_t *outsz)
76759c7d
TB
544{
545 iconv_t conv;
546 char *out;
aab2a1ae
TB
547 const char *bom_str = NULL;
548 size_t bom_len = 0;
76759c7d
TB
549
550 if (!in_encoding)
551 return NULL;
5c680be1 552
aab2a1ae
TB
553 /* UTF-16LE-BOM is the same as UTF-16 for reading */
554 if (same_utf_encoding("UTF-16LE-BOM", in_encoding))
555 in_encoding = "UTF-16";
556
557 /*
558 * For writing, UTF-16 iconv typically creates "UTF-16BE-BOM"
559 * Some users under Windows want the little endian version
79444c92 560 *
561 * We handle UTF-16 and UTF-32 ourselves only if the platform does not
562 * provide a BOM (which we require), since we want to match the behavior
563 * of the system tools and libc as much as possible.
aab2a1ae
TB
564 */
565 if (same_utf_encoding("UTF-16LE-BOM", out_encoding)) {
566 bom_str = utf16_le_bom;
567 bom_len = sizeof(utf16_le_bom);
568 out_encoding = "UTF-16LE";
569 } else if (same_utf_encoding("UTF-16BE-BOM", out_encoding)) {
570 bom_str = utf16_be_bom;
571 bom_len = sizeof(utf16_be_bom);
572 out_encoding = "UTF-16BE";
79444c92 573#ifdef ICONV_OMITS_BOM
574 } else if (same_utf_encoding("UTF-16", out_encoding)) {
575 bom_str = utf16_be_bom;
576 bom_len = sizeof(utf16_be_bom);
577 out_encoding = "UTF-16BE";
578 } else if (same_utf_encoding("UTF-32", out_encoding)) {
579 bom_str = utf32_be_bom;
580 bom_len = sizeof(utf32_be_bom);
581 out_encoding = "UTF-32BE";
582#endif
aab2a1ae
TB
583 }
584
76759c7d 585 conv = iconv_open(out_encoding, in_encoding);
5c680be1 586 if (conv == (iconv_t) -1) {
3270741e
JH
587 in_encoding = fallback_encoding(in_encoding);
588 out_encoding = fallback_encoding(out_encoding);
589
5c680be1
JK
590 conv = iconv_open(out_encoding, in_encoding);
591 if (conv == (iconv_t) -1)
592 return NULL;
593 }
aab2a1ae 594 out = reencode_string_iconv(in, insz, conv, bom_len, outsz);
b45974a6 595 iconv_close(conv);
aab2a1ae
TB
596 if (out && bom_str && bom_len)
597 memcpy(out, bom_str, bom_len);
b45974a6
JH
598 return out;
599}
600#endif
6cd3c053 601
10ecb82e
LS
602static int has_bom_prefix(const char *data, size_t len,
603 const char *bom, size_t bom_len)
604{
605 return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len);
606}
607
10ecb82e
LS
608int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)
609{
610 return (
611 (same_utf_encoding("UTF-16BE", enc) ||
612 same_utf_encoding("UTF-16LE", enc)) &&
613 (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
614 has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
615 ) || (
616 (same_utf_encoding("UTF-32BE", enc) ||
617 same_utf_encoding("UTF-32LE", enc)) &&
618 (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
619 has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
620 );
621}
622
c6e48652
LS
623int is_missing_required_utf_bom(const char *enc, const char *data, size_t len)
624{
625 return (
626 (same_utf_encoding(enc, "UTF-16")) &&
627 !(has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
628 has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
629 ) || (
630 (same_utf_encoding(enc, "UTF-32")) &&
631 !(has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
632 has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
633 );
634}
635
6cd3c053
KS
636/*
637 * Returns first character length in bytes for multi-byte `text` according to
638 * `encoding`.
639 *
640 * - The `text` pointer is updated to point at the next character.
641 * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes
642 * we can consume from text, and on exit `*remainder_p` is reduced by returned
643 * character length. Otherwise `text` is treated as limited by NUL.
644 */
645int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding)
646{
647 int chrlen;
648 const char *p = *text;
649 size_t r = (remainder_p ? *remainder_p : SIZE_MAX);
650
651 if (r < 1)
652 return 0;
653
654 if (is_encoding_utf8(encoding)) {
655 pick_one_utf8_char(&p, &r);
656
657 chrlen = p ? (p - *text)
658 : 1 /* not valid UTF-8 -> raw byte sequence */;
659 }
660 else {
661 /*
662 * TODO use iconv to decode one char and obtain its chrlen
663 * for now, let's treat encodings != UTF-8 as one-byte
664 */
665 chrlen = 1;
666 }
667
668 *text += chrlen;
669 if (remainder_p)
670 *remainder_p -= chrlen;
671
672 return chrlen;
673}
6162a1d3
JK
674
675/*
6aaf956b
JK
676 * Pick the next char from the stream, ignoring codepoints an HFS+ would.
677 * Note that this is _not_ complete by any means. It's just enough
6162a1d3
JK
678 * to make is_hfs_dotgit() work, and should not be used otherwise.
679 */
680static ucs_char_t next_hfs_char(const char **in)
681{
682 while (1) {
683 ucs_char_t out = pick_one_utf8_char(in, NULL);
684 /*
685 * check for malformed utf8. Technically this
686 * gets converted to a percent-sequence, but
687 * returning 0 is good enough for is_hfs_dotgit
688 * to realize it cannot be .git
689 */
690 if (!*in)
691 return 0;
692
693 /* these code points are ignored completely */
694 switch (out) {
695 case 0x200c: /* ZERO WIDTH NON-JOINER */
696 case 0x200d: /* ZERO WIDTH JOINER */
697 case 0x200e: /* LEFT-TO-RIGHT MARK */
698 case 0x200f: /* RIGHT-TO-LEFT MARK */
699 case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
700 case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
701 case 0x202c: /* POP DIRECTIONAL FORMATTING */
702 case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
703 case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
704 case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
705 case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
706 case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
707 case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
708 case 0x206e: /* NATIONAL DIGIT SHAPES */
709 case 0x206f: /* NOMINAL DIGIT SHAPES */
710 case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
711 continue;
712 }
713
6aaf956b 714 return out;
6162a1d3
JK
715 }
716}
717
0fc333ba
JK
718static int is_hfs_dot_generic(const char *path,
719 const char *needle, size_t needle_len)
6162a1d3
JK
720{
721 ucs_char_t c;
722
6aaf956b
JK
723 c = next_hfs_char(&path);
724 if (c != '.')
725 return 0;
6aaf956b
JK
726
727 /*
728 * there's a great deal of other case-folding that occurs
0fc333ba
JK
729 * in HFS+, but this is enough to catch our fairly vanilla
730 * hard-coded needles.
6aaf956b 731 */
0fc333ba
JK
732 for (; needle_len > 0; needle++, needle_len--) {
733 c = next_hfs_char(&path);
734
735 /*
736 * We know our needles contain only ASCII, so we clamp here to
737 * make the results of tolower() sane.
738 */
739 if (c > 127)
740 return 0;
741 if (tolower(c) != *needle)
742 return 0;
743 }
744
6162a1d3
JK
745 c = next_hfs_char(&path);
746 if (c && !is_dir_sep(c))
747 return 0;
748
749 return 1;
750}
dde843e7 751
0fc333ba
JK
752/*
753 * Inline wrapper to make sure the compiler resolves strlen() on literals at
754 * compile time.
755 */
756static inline int is_hfs_dot_str(const char *path, const char *needle)
757{
758 return is_hfs_dot_generic(path, needle, strlen(needle));
759}
760
761int is_hfs_dotgit(const char *path)
762{
763 return is_hfs_dot_str(path, "git");
764}
765
766int is_hfs_dotgitmodules(const char *path)
767{
768 return is_hfs_dot_str(path, "gitmodules");
769}
770
771int is_hfs_dotgitignore(const char *path)
772{
773 return is_hfs_dot_str(path, "gitignore");
774}
775
776int is_hfs_dotgitattributes(const char *path)
777{
778 return is_hfs_dot_str(path, "gitattributes");
779}
780
dde843e7
JH
781const char utf8_bom[] = "\357\273\277";
782
783int skip_utf8_bom(char **text, size_t len)
784{
785 if (len < strlen(utf8_bom) ||
786 memcmp(*text, utf8_bom, strlen(utf8_bom)))
787 return 0;
788 *text += strlen(utf8_bom);
789 return 1;
790}
110dcda5
KN
791
792void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
793 const char *s)
794{
795 int slen = strlen(s);
796 int display_len = utf8_strnwidth(s, slen, 0);
797 int utf8_compensation = slen - display_len;
798
799 if (display_len >= width) {
800 strbuf_addstr(buf, s);
801 return;
802 }
803
804 if (position == ALIGN_LEFT)
805 strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
806 else if (position == ALIGN_MIDDLE) {
807 int left = (width - display_len) / 2;
808 strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
809 } else if (position == ALIGN_RIGHT)
810 strbuf_addf(buf, "%*s", width + utf8_compensation, s);
811}