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.

811 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 (skip_iprefix(src, "utf", &src) && skip_iprefix(dst, "utf", &dst)) {
  378. skip_prefix(src, "-", &src);
  379. skip_prefix(dst, "-", &dst);
  380. return !strcasecmp(src, dst);
  381. }
  382. return 0;
  383. }
  384. int is_encoding_utf8(const char *name)
  385. {
  386. if (!name)
  387. return 1;
  388. if (same_utf_encoding("utf-8", name))
  389. return 1;
  390. return 0;
  391. }
  392. int same_encoding(const char *src, const char *dst)
  393. {
  394. static const char utf8[] = "UTF-8";
  395. if (!src)
  396. src = utf8;
  397. if (!dst)
  398. dst = utf8;
  399. if (same_utf_encoding(src, dst))
  400. return 1;
  401. return !strcasecmp(src, dst);
  402. }
  403. /*
  404. * Wrapper for fprintf and returns the total number of columns required
  405. * for the printed string, assuming that the string is utf8.
  406. */
  407. int utf8_fprintf(FILE *stream, const char *format, ...)
  408. {
  409. struct strbuf buf = STRBUF_INIT;
  410. va_list arg;
  411. int columns;
  412. va_start(arg, format);
  413. strbuf_vaddf(&buf, format, arg);
  414. va_end(arg);
  415. columns = fputs(buf.buf, stream);
  416. if (0 <= columns) /* keep the error from the I/O */
  417. columns = utf8_strwidth(buf.buf);
  418. strbuf_release(&buf);
  419. return columns;
  420. }
  421. /*
  422. * Given a buffer and its encoding, return it re-encoded
  423. * with iconv. If the conversion fails, returns NULL.
  424. */
  425. #ifndef NO_ICONV
  426. #if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6))
  427. typedef const char * iconv_ibp;
  428. #else
  429. typedef char * iconv_ibp;
  430. #endif
  431. char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv,
  432. size_t bom_len, size_t *outsz_p)
  433. {
  434. size_t outsz, outalloc;
  435. char *out, *outpos;
  436. iconv_ibp cp;
  437. outsz = insz;
  438. outalloc = st_add(outsz, 1 + bom_len); /* for terminating NUL */
  439. out = xmalloc(outalloc);
  440. outpos = out + bom_len;
  441. cp = (iconv_ibp)in;
  442. while (1) {
  443. size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
  444. if (cnt == (size_t) -1) {
  445. size_t sofar;
  446. if (errno != E2BIG) {
  447. free(out);
  448. return NULL;
  449. }
  450. /* insz has remaining number of bytes.
  451. * since we started outsz the same as insz,
  452. * it is likely that insz is not enough for
  453. * converting the rest.
  454. */
  455. sofar = outpos - out;
  456. outalloc = st_add3(sofar, st_mult(insz, 2), 32);
  457. out = xrealloc(out, outalloc);
  458. outpos = out + sofar;
  459. outsz = outalloc - sofar - 1;
  460. }
  461. else {
  462. *outpos = '\0';
  463. if (outsz_p)
  464. *outsz_p = outpos - out;
  465. break;
  466. }
  467. }
  468. return out;
  469. }
  470. static const char *fallback_encoding(const char *name)
  471. {
  472. /*
  473. * Some platforms do not have the variously spelled variants of
  474. * UTF-8, so let's fall back to trying the most official
  475. * spelling. We do so only as a fallback in case the platform
  476. * does understand the user's spelling, but not our official
  477. * one.
  478. */
  479. if (is_encoding_utf8(name))
  480. return "UTF-8";
  481. /*
  482. * Even though latin-1 is still seen in e-mail
  483. * headers, some platforms only install ISO-8859-1.
  484. */
  485. if (!strcasecmp(name, "latin-1"))
  486. return "ISO-8859-1";
  487. return name;
  488. }
  489. char *reencode_string_len(const char *in, size_t insz,
  490. const char *out_encoding, const char *in_encoding,
  491. size_t *outsz)
  492. {
  493. iconv_t conv;
  494. char *out;
  495. const char *bom_str = NULL;
  496. size_t bom_len = 0;
  497. if (!in_encoding)
  498. return NULL;
  499. /* UTF-16LE-BOM is the same as UTF-16 for reading */
  500. if (same_utf_encoding("UTF-16LE-BOM", in_encoding))
  501. in_encoding = "UTF-16";
  502. /*
  503. * For writing, UTF-16 iconv typically creates "UTF-16BE-BOM"
  504. * Some users under Windows want the little endian version
  505. *
  506. * We handle UTF-16 and UTF-32 ourselves only if the platform does not
  507. * provide a BOM (which we require), since we want to match the behavior
  508. * of the system tools and libc as much as possible.
  509. */
  510. if (same_utf_encoding("UTF-16LE-BOM", out_encoding)) {
  511. bom_str = utf16_le_bom;
  512. bom_len = sizeof(utf16_le_bom);
  513. out_encoding = "UTF-16LE";
  514. } else if (same_utf_encoding("UTF-16BE-BOM", out_encoding)) {
  515. bom_str = utf16_be_bom;
  516. bom_len = sizeof(utf16_be_bom);
  517. out_encoding = "UTF-16BE";
  518. #ifdef ICONV_OMITS_BOM
  519. } else if (same_utf_encoding("UTF-16", out_encoding)) {
  520. bom_str = utf16_be_bom;
  521. bom_len = sizeof(utf16_be_bom);
  522. out_encoding = "UTF-16BE";
  523. } else if (same_utf_encoding("UTF-32", out_encoding)) {
  524. bom_str = utf32_be_bom;
  525. bom_len = sizeof(utf32_be_bom);
  526. out_encoding = "UTF-32BE";
  527. #endif
  528. }
  529. conv = iconv_open(out_encoding, in_encoding);
  530. if (conv == (iconv_t) -1) {
  531. in_encoding = fallback_encoding(in_encoding);
  532. out_encoding = fallback_encoding(out_encoding);
  533. conv = iconv_open(out_encoding, in_encoding);
  534. if (conv == (iconv_t) -1)
  535. return NULL;
  536. }
  537. out = reencode_string_iconv(in, insz, conv, bom_len, outsz);
  538. iconv_close(conv);
  539. if (out && bom_str && bom_len)
  540. memcpy(out, bom_str, bom_len);
  541. return out;
  542. }
  543. #endif
  544. static int has_bom_prefix(const char *data, size_t len,
  545. const char *bom, size_t bom_len)
  546. {
  547. return data && bom && (len >= bom_len) && !memcmp(data, bom, bom_len);
  548. }
  549. int has_prohibited_utf_bom(const char *enc, const char *data, size_t len)
  550. {
  551. return (
  552. (same_utf_encoding("UTF-16BE", enc) ||
  553. same_utf_encoding("UTF-16LE", enc)) &&
  554. (has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
  555. has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
  556. ) || (
  557. (same_utf_encoding("UTF-32BE", enc) ||
  558. same_utf_encoding("UTF-32LE", enc)) &&
  559. (has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
  560. has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
  561. );
  562. }
  563. int is_missing_required_utf_bom(const char *enc, const char *data, size_t len)
  564. {
  565. return (
  566. (same_utf_encoding(enc, "UTF-16")) &&
  567. !(has_bom_prefix(data, len, utf16_be_bom, sizeof(utf16_be_bom)) ||
  568. has_bom_prefix(data, len, utf16_le_bom, sizeof(utf16_le_bom)))
  569. ) || (
  570. (same_utf_encoding(enc, "UTF-32")) &&
  571. !(has_bom_prefix(data, len, utf32_be_bom, sizeof(utf32_be_bom)) ||
  572. has_bom_prefix(data, len, utf32_le_bom, sizeof(utf32_le_bom)))
  573. );
  574. }
  575. /*
  576. * Returns first character length in bytes for multi-byte `text` according to
  577. * `encoding`.
  578. *
  579. * - The `text` pointer is updated to point at the next character.
  580. * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes
  581. * we can consume from text, and on exit `*remainder_p` is reduced by returned
  582. * character length. Otherwise `text` is treated as limited by NUL.
  583. */
  584. int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding)
  585. {
  586. int chrlen;
  587. const char *p = *text;
  588. size_t r = (remainder_p ? *remainder_p : SIZE_MAX);
  589. if (r < 1)
  590. return 0;
  591. if (is_encoding_utf8(encoding)) {
  592. pick_one_utf8_char(&p, &r);
  593. chrlen = p ? (p - *text)
  594. : 1 /* not valid UTF-8 -> raw byte sequence */;
  595. }
  596. else {
  597. /*
  598. * TODO use iconv to decode one char and obtain its chrlen
  599. * for now, let's treat encodings != UTF-8 as one-byte
  600. */
  601. chrlen = 1;
  602. }
  603. *text += chrlen;
  604. if (remainder_p)
  605. *remainder_p -= chrlen;
  606. return chrlen;
  607. }
  608. /*
  609. * Pick the next char from the stream, ignoring codepoints an HFS+ would.
  610. * Note that this is _not_ complete by any means. It's just enough
  611. * to make is_hfs_dotgit() work, and should not be used otherwise.
  612. */
  613. static ucs_char_t next_hfs_char(const char **in)
  614. {
  615. while (1) {
  616. ucs_char_t out = pick_one_utf8_char(in, NULL);
  617. /*
  618. * check for malformed utf8. Technically this
  619. * gets converted to a percent-sequence, but
  620. * returning 0 is good enough for is_hfs_dotgit
  621. * to realize it cannot be .git
  622. */
  623. if (!*in)
  624. return 0;
  625. /* these code points are ignored completely */
  626. switch (out) {
  627. case 0x200c: /* ZERO WIDTH NON-JOINER */
  628. case 0x200d: /* ZERO WIDTH JOINER */
  629. case 0x200e: /* LEFT-TO-RIGHT MARK */
  630. case 0x200f: /* RIGHT-TO-LEFT MARK */
  631. case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
  632. case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
  633. case 0x202c: /* POP DIRECTIONAL FORMATTING */
  634. case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
  635. case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
  636. case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
  637. case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
  638. case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
  639. case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
  640. case 0x206e: /* NATIONAL DIGIT SHAPES */
  641. case 0x206f: /* NOMINAL DIGIT SHAPES */
  642. case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
  643. continue;
  644. }
  645. return out;
  646. }
  647. }
  648. static int is_hfs_dot_generic(const char *path,
  649. const char *needle, size_t needle_len)
  650. {
  651. ucs_char_t c;
  652. c = next_hfs_char(&path);
  653. if (c != '.')
  654. return 0;
  655. /*
  656. * there's a great deal of other case-folding that occurs
  657. * in HFS+, but this is enough to catch our fairly vanilla
  658. * hard-coded needles.
  659. */
  660. for (; needle_len > 0; needle++, needle_len--) {
  661. c = next_hfs_char(&path);
  662. /*
  663. * We know our needles contain only ASCII, so we clamp here to
  664. * make the results of tolower() sane.
  665. */
  666. if (c > 127)
  667. return 0;
  668. if (tolower(c) != *needle)
  669. return 0;
  670. }
  671. c = next_hfs_char(&path);
  672. if (c && !is_dir_sep(c))
  673. return 0;
  674. return 1;
  675. }
  676. /*
  677. * Inline wrapper to make sure the compiler resolves strlen() on literals at
  678. * compile time.
  679. */
  680. static inline int is_hfs_dot_str(const char *path, const char *needle)
  681. {
  682. return is_hfs_dot_generic(path, needle, strlen(needle));
  683. }
  684. int is_hfs_dotgit(const char *path)
  685. {
  686. return is_hfs_dot_str(path, "git");
  687. }
  688. int is_hfs_dotgitmodules(const char *path)
  689. {
  690. return is_hfs_dot_str(path, "gitmodules");
  691. }
  692. int is_hfs_dotgitignore(const char *path)
  693. {
  694. return is_hfs_dot_str(path, "gitignore");
  695. }
  696. int is_hfs_dotgitattributes(const char *path)
  697. {
  698. return is_hfs_dot_str(path, "gitattributes");
  699. }
  700. const char utf8_bom[] = "\357\273\277";
  701. int skip_utf8_bom(char **text, size_t len)
  702. {
  703. if (len < strlen(utf8_bom) ||
  704. memcmp(*text, utf8_bom, strlen(utf8_bom)))
  705. return 0;
  706. *text += strlen(utf8_bom);
  707. return 1;
  708. }
  709. void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
  710. const char *s)
  711. {
  712. int slen = strlen(s);
  713. int display_len = utf8_strnwidth(s, slen, 0);
  714. int utf8_compensation = slen - display_len;
  715. if (display_len >= width) {
  716. strbuf_addstr(buf, s);
  717. return;
  718. }
  719. if (position == ALIGN_LEFT)
  720. strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
  721. else if (position == ALIGN_MIDDLE) {
  722. int left = (width - display_len) / 2;
  723. strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
  724. } else if (position == ALIGN_RIGHT)
  725. strbuf_addf(buf, "%*s", width + utf8_compensation, s);
  726. }