Merge branch 'maint'
[git/peff.git] / utf8.c
blob9efcdb9c09970127ebe37923879274b95efd526c
1 #include "git-compat-util.h"
2 #include "utf8.h"
4 /* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
6 typedef unsigned int ucs_char_t; /* assuming 32bit int */
8 struct interval {
9 int first;
10 int last;
13 /* auxiliary function for binary search in interval table */
14 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
16 int min = 0;
17 int mid;
19 if (ucs < table[0].first || ucs > table[max].last)
20 return 0;
21 while (max >= min) {
22 mid = (min + max) / 2;
23 if (ucs > table[mid].last)
24 min = mid + 1;
25 else if (ucs < table[mid].first)
26 max = mid - 1;
27 else
28 return 1;
31 return 0;
34 /* The following two functions define the column width of an ISO 10646
35 * character as follows:
37 * - The null character (U+0000) has a column width of 0.
39 * - Other C0/C1 control characters and DEL will lead to a return
40 * value of -1.
42 * - Non-spacing and enclosing combining characters (general
43 * category code Mn or Me in the Unicode database) have a
44 * column width of 0.
46 * - SOFT HYPHEN (U+00AD) has a column width of 1.
48 * - Other format characters (general category code Cf in the Unicode
49 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
51 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
52 * have a column width of 0.
54 * - Spacing characters in the East Asian Wide (W) or East Asian
55 * Full-width (F) category as defined in Unicode Technical
56 * Report #11 have a column width of 2.
58 * - All remaining characters (including all printable
59 * ISO 8859-1 and WGL4 characters, Unicode control characters,
60 * etc.) have a column width of 1.
62 * This implementation assumes that ucs_char_t characters are encoded
63 * in ISO 10646.
66 static int git_wcwidth(ucs_char_t ch)
69 * Sorted list of non-overlapping intervals of non-spacing characters,
70 * generated by
71 * "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c".
73 static const struct interval combining[] = {
74 { 0x0300, 0x0357 }, { 0x035D, 0x036F }, { 0x0483, 0x0486 },
75 { 0x0488, 0x0489 }, { 0x0591, 0x05A1 }, { 0x05A3, 0x05B9 },
76 { 0x05BB, 0x05BD }, { 0x05BF, 0x05BF }, { 0x05C1, 0x05C2 },
77 { 0x05C4, 0x05C4 }, { 0x0600, 0x0603 }, { 0x0610, 0x0615 },
78 { 0x064B, 0x0658 }, { 0x0670, 0x0670 }, { 0x06D6, 0x06E4 },
79 { 0x06E7, 0x06E8 }, { 0x06EA, 0x06ED }, { 0x070F, 0x070F },
80 { 0x0711, 0x0711 }, { 0x0730, 0x074A }, { 0x07A6, 0x07B0 },
81 { 0x0901, 0x0902 }, { 0x093C, 0x093C }, { 0x0941, 0x0948 },
82 { 0x094D, 0x094D }, { 0x0951, 0x0954 }, { 0x0962, 0x0963 },
83 { 0x0981, 0x0981 }, { 0x09BC, 0x09BC }, { 0x09C1, 0x09C4 },
84 { 0x09CD, 0x09CD }, { 0x09E2, 0x09E3 }, { 0x0A01, 0x0A02 },
85 { 0x0A3C, 0x0A3C }, { 0x0A41, 0x0A42 }, { 0x0A47, 0x0A48 },
86 { 0x0A4B, 0x0A4D }, { 0x0A70, 0x0A71 }, { 0x0A81, 0x0A82 },
87 { 0x0ABC, 0x0ABC }, { 0x0AC1, 0x0AC5 }, { 0x0AC7, 0x0AC8 },
88 { 0x0ACD, 0x0ACD }, { 0x0AE2, 0x0AE3 }, { 0x0B01, 0x0B01 },
89 { 0x0B3C, 0x0B3C }, { 0x0B3F, 0x0B3F }, { 0x0B41, 0x0B43 },
90 { 0x0B4D, 0x0B4D }, { 0x0B56, 0x0B56 }, { 0x0B82, 0x0B82 },
91 { 0x0BC0, 0x0BC0 }, { 0x0BCD, 0x0BCD }, { 0x0C3E, 0x0C40 },
92 { 0x0C46, 0x0C48 }, { 0x0C4A, 0x0C4D }, { 0x0C55, 0x0C56 },
93 { 0x0CBC, 0x0CBC }, { 0x0CBF, 0x0CBF }, { 0x0CC6, 0x0CC6 },
94 { 0x0CCC, 0x0CCD }, { 0x0D41, 0x0D43 }, { 0x0D4D, 0x0D4D },
95 { 0x0DCA, 0x0DCA }, { 0x0DD2, 0x0DD4 }, { 0x0DD6, 0x0DD6 },
96 { 0x0E31, 0x0E31 }, { 0x0E34, 0x0E3A }, { 0x0E47, 0x0E4E },
97 { 0x0EB1, 0x0EB1 }, { 0x0EB4, 0x0EB9 }, { 0x0EBB, 0x0EBC },
98 { 0x0EC8, 0x0ECD }, { 0x0F18, 0x0F19 }, { 0x0F35, 0x0F35 },
99 { 0x0F37, 0x0F37 }, { 0x0F39, 0x0F39 }, { 0x0F71, 0x0F7E },
100 { 0x0F80, 0x0F84 }, { 0x0F86, 0x0F87 }, { 0x0F90, 0x0F97 },
101 { 0x0F99, 0x0FBC }, { 0x0FC6, 0x0FC6 }, { 0x102D, 0x1030 },
102 { 0x1032, 0x1032 }, { 0x1036, 0x1037 }, { 0x1039, 0x1039 },
103 { 0x1058, 0x1059 }, { 0x1160, 0x11FF }, { 0x1712, 0x1714 },
104 { 0x1732, 0x1734 }, { 0x1752, 0x1753 }, { 0x1772, 0x1773 },
105 { 0x17B4, 0x17B5 }, { 0x17B7, 0x17BD }, { 0x17C6, 0x17C6 },
106 { 0x17C9, 0x17D3 }, { 0x17DD, 0x17DD }, { 0x180B, 0x180D },
107 { 0x18A9, 0x18A9 }, { 0x1920, 0x1922 }, { 0x1927, 0x1928 },
108 { 0x1932, 0x1932 }, { 0x1939, 0x193B }, { 0x200B, 0x200F },
109 { 0x202A, 0x202E }, { 0x2060, 0x2063 }, { 0x206A, 0x206F },
110 { 0x20D0, 0x20EA }, { 0x302A, 0x302F }, { 0x3099, 0x309A },
111 { 0xFB1E, 0xFB1E }, { 0xFE00, 0xFE0F }, { 0xFE20, 0xFE23 },
112 { 0xFEFF, 0xFEFF }, { 0xFFF9, 0xFFFB }, { 0x1D167, 0x1D169 },
113 { 0x1D173, 0x1D182 }, { 0x1D185, 0x1D18B },
114 { 0x1D1AA, 0x1D1AD }, { 0xE0001, 0xE0001 },
115 { 0xE0020, 0xE007F }, { 0xE0100, 0xE01EF }
118 /* test for 8-bit control characters */
119 if (ch == 0)
120 return 0;
121 if (ch < 32 || (ch >= 0x7f && ch < 0xa0))
122 return -1;
124 /* binary search in table of non-spacing characters */
125 if (bisearch(ch, combining, sizeof(combining)
126 / sizeof(struct interval) - 1))
127 return 0;
130 * If we arrive here, ch is neither a combining nor a C0/C1
131 * control character.
134 return 1 +
135 (ch >= 0x1100 &&
136 /* Hangul Jamo init. consonants */
137 (ch <= 0x115f ||
138 ch == 0x2329 || ch == 0x232a ||
139 /* CJK ... Yi */
140 (ch >= 0x2e80 && ch <= 0xa4cf &&
141 ch != 0x303f) ||
142 /* Hangul Syllables */
143 (ch >= 0xac00 && ch <= 0xd7a3) ||
144 /* CJK Compatibility Ideographs */
145 (ch >= 0xf900 && ch <= 0xfaff) ||
146 /* CJK Compatibility Forms */
147 (ch >= 0xfe30 && ch <= 0xfe6f) ||
148 /* Fullwidth Forms */
149 (ch >= 0xff00 && ch <= 0xff60) ||
150 (ch >= 0xffe0 && ch <= 0xffe6) ||
151 (ch >= 0x20000 && ch <= 0x2fffd) ||
152 (ch >= 0x30000 && ch <= 0x3fffd)));
156 * This function returns the number of columns occupied by the character
157 * pointed to by the variable start. The pointer is updated to point at
158 * the next character. If it was not valid UTF-8, the pointer is set to NULL.
160 int utf8_width(const char **start)
162 unsigned char *s = (unsigned char *)*start;
163 ucs_char_t ch;
165 if (*s < 0x80) {
166 /* 0xxxxxxx */
167 ch = *s;
168 *start += 1;
169 } else if ((s[0] & 0xe0) == 0xc0) {
170 /* 110XXXXx 10xxxxxx */
171 if ((s[1] & 0xc0) != 0x80 ||
172 /* overlong? */
173 (s[0] & 0xfe) == 0xc0)
174 goto invalid;
175 ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
176 *start += 2;
177 } else if ((s[0] & 0xf0) == 0xe0) {
178 /* 1110XXXX 10Xxxxxx 10xxxxxx */
179 if ((s[1] & 0xc0) != 0x80 ||
180 (s[2] & 0xc0) != 0x80 ||
181 /* overlong? */
182 (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) ||
183 /* surrogate? */
184 (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) ||
185 /* U+FFFE or U+FFFF? */
186 (s[0] == 0xef && s[1] == 0xbf &&
187 (s[2] & 0xfe) == 0xbe))
188 goto invalid;
189 ch = ((s[0] & 0x0f) << 12) |
190 ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
191 *start += 3;
192 } else if ((s[0] & 0xf8) == 0xf0) {
193 /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
194 if ((s[1] & 0xc0) != 0x80 ||
195 (s[2] & 0xc0) != 0x80 ||
196 (s[3] & 0xc0) != 0x80 ||
197 /* overlong? */
198 (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) ||
199 /* > U+10FFFF? */
200 (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4)
201 goto invalid;
202 ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
203 ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
204 *start += 4;
205 } else {
206 invalid:
207 *start = NULL;
208 return 0;
211 return git_wcwidth(ch);
214 int is_utf8(const char *text)
216 while (*text) {
217 if (*text == '\n' || *text == '\t' || *text == '\r') {
218 text++;
219 continue;
221 utf8_width(&text);
222 if (!text)
223 return 0;
225 return 1;
228 static void print_spaces(int count)
230 static const char s[] = " ";
231 while (count >= sizeof(s)) {
232 fwrite(s, sizeof(s) - 1, 1, stdout);
233 count -= sizeof(s) - 1;
235 fwrite(s, count, 1, stdout);
239 * Wrap the text, if necessary. The variable indent is the indent for the
240 * first line, indent2 is the indent for all other lines.
241 * If indent is negative, assume that already -indent columns have been
242 * consumed (and no extra indent is necessary for the first line).
244 int print_wrapped_text(const char *text, int indent, int indent2, int width)
246 int w = indent, assume_utf8 = is_utf8(text);
247 const char *bol = text, *space = NULL;
249 if (indent < 0) {
250 w = -indent;
251 space = text;
254 for (;;) {
255 char c = *text;
256 if (!c || isspace(c)) {
257 if (w < width || !space) {
258 const char *start = bol;
259 if (space)
260 start = space;
261 else
262 print_spaces(indent);
263 fwrite(start, text - start, 1, stdout);
264 if (!c)
265 return w;
266 else if (c == '\t')
267 w |= 0x07;
268 space = text;
269 w++;
270 text++;
272 else {
273 putchar('\n');
274 text = bol = space + isspace(*space);
275 space = NULL;
276 w = indent = indent2;
278 continue;
280 if (assume_utf8)
281 w += utf8_width(&text);
282 else {
283 w++;
284 text++;
289 int is_encoding_utf8(const char *name)
291 if (!name)
292 return 1;
293 if (!strcasecmp(name, "utf-8") || !strcasecmp(name, "utf8"))
294 return 1;
295 return 0;
299 * Given a buffer and its encoding, return it re-encoded
300 * with iconv. If the conversion fails, returns NULL.
302 #ifndef NO_ICONV
303 #ifdef OLD_ICONV
304 typedef const char * iconv_ibp;
305 #else
306 typedef char * iconv_ibp;
307 #endif
308 char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)
310 iconv_t conv;
311 size_t insz, outsz, outalloc;
312 char *out, *outpos;
313 iconv_ibp cp;
315 if (!in_encoding)
316 return NULL;
317 conv = iconv_open(out_encoding, in_encoding);
318 if (conv == (iconv_t) -1)
319 return NULL;
320 insz = strlen(in);
321 outsz = insz;
322 outalloc = outsz + 1; /* for terminating NUL */
323 out = xmalloc(outalloc);
324 outpos = out;
325 cp = (iconv_ibp)in;
327 while (1) {
328 size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
330 if (cnt == -1) {
331 size_t sofar;
332 if (errno != E2BIG) {
333 free(out);
334 iconv_close(conv);
335 return NULL;
337 /* insz has remaining number of bytes.
338 * since we started outsz the same as insz,
339 * it is likely that insz is not enough for
340 * converting the rest.
342 sofar = outpos - out;
343 outalloc = sofar + insz * 2 + 32;
344 out = xrealloc(out, outalloc);
345 outpos = out + sofar;
346 outsz = outalloc - sofar - 1;
348 else {
349 *outpos = '\0';
350 break;
353 iconv_close(conv);
354 return out;
356 #endif