THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

812 lines
20KB

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