Skip to content
Newer
Older
100644 668 lines (601 sloc) 15.9 KB
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
1 #include "git-compat-util.h"
a94410c @dscho Add strbuf_add_wrapped_text() to utf8.[ch]
dscho authored
2 #include "strbuf.h"
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
3 #include "utf8.h"
4
5 /* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */
6
7 struct interval {
a68a67d @johnkeeping utf8: use correct type for values in interval table
johnkeeping authored
8 ucs_char_t first;
9 ucs_char_t last;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
10 };
11
1640632 @pclouds pretty: support %>> that steal trailing spaces
pclouds authored
12 size_t display_mode_esc_sequence_len(const char *s)
4247fe7 @pclouds utf8.c: move display_mode_esc_sequence_len() for use by other functions
pclouds authored
13 {
14 const char *p = s;
15 if (*p++ != '\033')
16 return 0;
17 if (*p++ != '[')
18 return 0;
19 while (isdigit(*p) || *p == ';')
20 p++;
21 if (*p++ != 'm')
22 return 0;
23 return p - s;
24 }
25
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
26 /* auxiliary function for binary search in interval table */
f3fa183 @gitster Style: place opening brace of a function definition at column 1
gitster authored
27 static int bisearch(ucs_char_t ucs, const struct interval *table, int max)
28 {
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
29 int min = 0;
30 int mid;
31
32 if (ucs < table[0].first || ucs > table[max].last)
33 return 0;
34 while (max >= min) {
35 mid = (min + max) / 2;
36 if (ucs > table[mid].last)
37 min = mid + 1;
38 else if (ucs < table[mid].first)
39 max = mid - 1;
40 else
41 return 1;
42 }
43
44 return 0;
45 }
46
47 /* The following two functions define the column width of an ISO 10646
48 * character as follows:
49 *
50 * - The null character (U+0000) has a column width of 0.
51 *
52 * - Other C0/C1 control characters and DEL will lead to a return
53 * value of -1.
54 *
55 * - Non-spacing and enclosing combining characters (general
56 * category code Mn or Me in the Unicode database) have a
57 * column width of 0.
58 *
59 * - SOFT HYPHEN (U+00AD) has a column width of 1.
60 *
61 * - Other format characters (general category code Cf in the Unicode
62 * database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.
63 *
64 * - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)
65 * have a column width of 0.
66 *
67 * - Spacing characters in the East Asian Wide (W) or East Asian
68 * Full-width (F) category as defined in Unicode Technical
69 * Report #11 have a column width of 2.
70 *
71 * - All remaining characters (including all printable
72 * ISO 8859-1 and WGL4 characters, Unicode control characters,
73 * etc.) have a column width of 1.
74 *
2832114 Fix some "comparison is always true/false" warnings.
Ramsay Jones authored
75 * This implementation assumes that ucs_char_t characters are encoded
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
76 * in ISO 10646.
77 */
78
b51be13 wcwidth redeclaration
Amos Waterland authored
79 static int git_wcwidth(ucs_char_t ch)
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
80 {
81 /*
82 * Sorted list of non-overlapping intervals of non-spacing characters,
83 */
9c94389 @tboegi utf8: make it easier to auto-update git_wcwidth()
tboegi authored
84 #include "unicode_width.h"
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
85
86 /* test for 8-bit control characters */
87 if (ch == 0)
88 return 0;
89 if (ch < 32 || (ch >= 0x7f && ch < 0xa0))
90 return -1;
91
92 /* binary search in table of non-spacing characters */
9c94389 @tboegi utf8: make it easier to auto-update git_wcwidth()
tboegi authored
93 if (bisearch(ch, zero_width, sizeof(zero_width)
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
94 / sizeof(struct interval) - 1))
95 return 0;
96
0846034 @tboegi utf8.c: use a table for double_width
tboegi authored
97 /* binary search in table of double width characters */
98 if (bisearch(ch, double_width, sizeof(double_width)
99 / sizeof(struct interval) - 1))
100 return 2;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
101
0846034 @tboegi utf8.c: use a table for double_width
tboegi authored
102 return 1;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
103 }
104
105 /*
396ccf1 @gitster utf8: pick_one_utf8_char()
gitster authored
106 * Pick one ucs character starting from the location *start points at,
107 * and return it, while updating the *start pointer to point at the
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
108 * end of that character. When remainder_p is not NULL, the location
109 * holds the number of bytes remaining in the string that we are allowed
110 * to pick from. Otherwise we are allowed to pick up to the NUL that
111 * would eventually appear in the string. *remainder_p is also reduced
112 * by the number of bytes we have consumed.
396ccf1 @gitster utf8: pick_one_utf8_char()
gitster authored
113 *
114 * If the string was not a valid UTF-8, *start pointer is set to NULL
115 * and the return value is undefined.
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
116 */
5e133b8 @gitster utf8.c: mark file-local function static
gitster authored
117 static ucs_char_t pick_one_utf8_char(const char **start, size_t *remainder_p)
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
118 {
119 unsigned char *s = (unsigned char *)*start;
2832114 Fix some "comparison is always true/false" warnings.
Ramsay Jones authored
120 ucs_char_t ch;
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
121 size_t remainder, incr;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
122
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
123 /*
124 * A caller that assumes NUL terminated text can choose
125 * not to bother with the remainder length. We will
126 * stop at the first NUL.
127 */
128 remainder = (remainder_p ? *remainder_p : 999);
129
130 if (remainder < 1) {
131 goto invalid;
132 } else if (*s < 0x80) {
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
133 /* 0xxxxxxx */
134 ch = *s;
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
135 incr = 1;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
136 } else if ((s[0] & 0xe0) == 0xc0) {
137 /* 110XXXXx 10xxxxxx */
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
138 if (remainder < 2 ||
139 (s[1] & 0xc0) != 0x80 ||
140 (s[0] & 0xfe) == 0xc0)
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
141 goto invalid;
142 ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
143 incr = 2;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
144 } else if ((s[0] & 0xf0) == 0xe0) {
145 /* 1110XXXX 10Xxxxxx 10xxxxxx */
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
146 if (remainder < 3 ||
147 (s[1] & 0xc0) != 0x80 ||
148 (s[2] & 0xc0) != 0x80 ||
149 /* overlong? */
150 (s[0] == 0xe0 && (s[1] & 0xe0) == 0x80) ||
151 /* surrogate? */
152 (s[0] == 0xed && (s[1] & 0xe0) == 0xa0) ||
153 /* U+FFFE or U+FFFF? */
154 (s[0] == 0xef && s[1] == 0xbf &&
155 (s[2] & 0xfe) == 0xbe))
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
156 goto invalid;
157 ch = ((s[0] & 0x0f) << 12) |
158 ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
159 incr = 3;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
160 } else if ((s[0] & 0xf8) == 0xf0) {
161 /* 11110XXX 10XXxxxx 10xxxxxx 10xxxxxx */
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
162 if (remainder < 4 ||
163 (s[1] & 0xc0) != 0x80 ||
164 (s[2] & 0xc0) != 0x80 ||
165 (s[3] & 0xc0) != 0x80 ||
166 /* overlong? */
167 (s[0] == 0xf0 && (s[1] & 0xf0) == 0x80) ||
168 /* > U+10FFFF? */
169 (s[0] == 0xf4 && s[1] > 0x8f) || s[0] > 0xf4)
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
170 goto invalid;
171 ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |
172 ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
173 incr = 4;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
174 } else {
175 invalid:
176 *start = NULL;
177 return 0;
178 }
179
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
180 *start += incr;
181 if (remainder_p)
182 *remainder_p = remainder - incr;
396ccf1 @gitster utf8: pick_one_utf8_char()
gitster authored
183 return ch;
184 }
185
186 /*
187 * This function returns the number of columns occupied by the character
188 * pointed to by the variable start. The pointer is updated to point at
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
189 * the next character. When remainder_p is not NULL, it points at the
190 * location that stores the number of remaining bytes we can use to pick
191 * a character (see pick_one_utf8_char() above).
396ccf1 @gitster utf8: pick_one_utf8_char()
gitster authored
192 */
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
193 int utf8_width(const char **start, size_t *remainder_p)
396ccf1 @gitster utf8: pick_one_utf8_char()
gitster authored
194 {
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
195 ucs_char_t ch = pick_one_utf8_char(start, remainder_p);
396ccf1 @gitster utf8: pick_one_utf8_char()
gitster authored
196 if (!*start)
197 return 0;
b51be13 wcwidth redeclaration
Amos Waterland authored
198 return git_wcwidth(ch);
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
199 }
200
8a9391e @geofft utf8: add utf8_strwidth()
geofft authored
201 /*
202 * Returns the total number of columns required by a null-terminated
203 * string, assuming that the string is utf8. Returns strlen() instead
204 * if the string does not look like a valid utf8 string.
205 */
2bc1e7e @pclouds utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
pclouds authored
206 int utf8_strnwidth(const char *string, int len, int skip_ansi)
8a9391e @geofft utf8: add utf8_strwidth()
geofft authored
207 {
208 int width = 0;
209 const char *orig = string;
210
2bc1e7e @pclouds utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
pclouds authored
211 if (len == -1)
212 len = strlen(string);
213 while (string && string < orig + len) {
214 int skip;
215 while (skip_ansi &&
216 (skip = display_mode_esc_sequence_len(string)) != 0)
217 string += skip;
8a9391e @geofft utf8: add utf8_strwidth()
geofft authored
218 width += utf8_width(&string, NULL);
219 }
2bc1e7e @pclouds utf8.c: add utf8_strnwidth() with the ability to skip ansi sequences
pclouds authored
220 return string ? width : len;
221 }
222
223 int utf8_strwidth(const char *string)
224 {
225 return utf8_strnwidth(string, -1, 0);
8a9391e @geofft utf8: add utf8_strwidth()
geofft authored
226 }
227
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
228 int is_utf8(const char *text)
229 {
230 while (*text) {
231 if (*text == '\n' || *text == '\t' || *text == '\r') {
232 text++;
233 continue;
234 }
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
235 utf8_width(&text, NULL);
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
236 if (!text)
237 return 0;
238 }
239 return 1;
240 }
241
37bb5d7 strbuf_add_wrapped_text(): factor out strbuf_add_indented_text()
René Scharfe authored
242 static void strbuf_add_indented_text(struct strbuf *buf, const char *text,
243 int indent, int indent2)
244 {
245 if (indent < 0)
246 indent = 0;
247 while (*text) {
248 const char *eol = strchrnul(text, '\n');
249 if (*eol == '\n')
250 eol++;
3c0ff44 utf8.c: remove print_spaces()
René Scharfe authored
251 strbuf_addchars(buf, ' ', indent);
68ad5e1 utf8.c: remove strbuf_write()
René Scharfe authored
252 strbuf_add(buf, text, eol - text);
37bb5d7 strbuf_add_wrapped_text(): factor out strbuf_add_indented_text()
René Scharfe authored
253 text = eol;
254 indent = indent2;
255 }
256 }
257
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
258 /*
259 * Wrap the text, if necessary. The variable indent is the indent for the
260 * first line, indent2 is the indent for all other lines.
094e03b @dscho Actually make print_wrapped_text() useful
dscho authored
261 * If indent is negative, assume that already -indent columns have been
262 * consumed (and no extra indent is necessary for the first line).
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
263 */
e0db176 @sprohaska strbuf_add_wrapped*(): Remove unused return value
sprohaska authored
264 void strbuf_add_wrapped_text(struct strbuf *buf,
462749b utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
René Scharfe authored
265 const char *text, int indent1, int indent2, int width)
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
266 {
462749b utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
René Scharfe authored
267 int indent, w, assume_utf8 = 1;
268 const char *bol, *space, *start = text;
269 size_t orig_len = buf->len;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
270
00d3947 @gitster Teach --wrap to only indent without wrapping
gitster authored
271 if (width <= 0) {
462749b utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
René Scharfe authored
272 strbuf_add_indented_text(buf, text, indent1, indent2);
e0db176 @sprohaska strbuf_add_wrapped*(): Remove unused return value
sprohaska authored
273 return;
00d3947 @gitster Teach --wrap to only indent without wrapping
gitster authored
274 }
275
462749b utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
René Scharfe authored
276 retry:
277 bol = text;
278 w = indent = indent1;
279 space = NULL;
094e03b @dscho Actually make print_wrapped_text() useful
dscho authored
280 if (indent < 0) {
281 w = -indent;
282 space = text;
283 }
284
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
285 for (;;) {
8a3c63e strbuf_add_wrapped_text(): skip over colour codes
René Scharfe authored
286 char c;
287 size_t skip;
288
289 while ((skip = display_mode_esc_sequence_len(text)))
290 text += skip;
291
292 c = *text;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
293 if (!c || isspace(c)) {
14e1a4e utf8: fix off-by-one wrapping of text
Jan H. Schönherr authored
294 if (w <= width || !space) {
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
295 const char *start = bol;
ae0b270 @dscho print_wrapped_text(): allow hard newlines
dscho authored
296 if (!c && text == start)
e0db176 @sprohaska strbuf_add_wrapped*(): Remove unused return value
sprohaska authored
297 return;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
298 if (space)
299 start = space;
300 else
3c0ff44 utf8.c: remove print_spaces()
René Scharfe authored
301 strbuf_addchars(buf, ' ', indent);
68ad5e1 utf8.c: remove strbuf_write()
René Scharfe authored
302 strbuf_add(buf, start, text - start);
094e03b @dscho Actually make print_wrapped_text() useful
dscho authored
303 if (!c)
e0db176 @sprohaska strbuf_add_wrapped*(): Remove unused return value
sprohaska authored
304 return;
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
305 space = text;
ae0b270 @dscho print_wrapped_text(): allow hard newlines
dscho authored
306 if (c == '\t')
307 w |= 0x07;
308 else if (c == '\n') {
309 space++;
310 if (*space == '\n') {
68ad5e1 utf8.c: remove strbuf_write()
René Scharfe authored
311 strbuf_addch(buf, '\n');
ae0b270 @dscho print_wrapped_text(): allow hard newlines
dscho authored
312 goto new_line;
313 }
314 else if (!isalnum(*space))
315 goto new_line;
316 else
68ad5e1 utf8.c: remove strbuf_write()
René Scharfe authored
317 strbuf_addch(buf, ' ');
ae0b270 @dscho print_wrapped_text(): allow hard newlines
dscho authored
318 }
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
319 w++;
320 text++;
321 }
322 else {
ae0b270 @dscho print_wrapped_text(): allow hard newlines
dscho authored
323 new_line:
68ad5e1 utf8.c: remove strbuf_write()
René Scharfe authored
324 strbuf_addch(buf, '\n');
6227382 @dscho print_wrapped_text: fix output for negative indent
dscho authored
325 text = bol = space + isspace(*space);
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
326 space = NULL;
327 w = indent = indent2;
328 }
329 continue;
330 }
462749b utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
René Scharfe authored
331 if (assume_utf8) {
44b25b8 @gitster utf8_width(): allow non NUL-terminated input
gitster authored
332 w += utf8_width(&text, NULL);
462749b utf8.c: speculatively assume utf-8 in strbuf_add_wrapped_text()
René Scharfe authored
333 if (!text) {
334 assume_utf8 = 0;
335 text = start;
336 strbuf_setlen(buf, orig_len);
337 goto retry;
338 }
339 } else {
9e83266 @dscho commit-tree: encourage UTF-8 commit messages.
dscho authored
340 w++;
341 text++;
342 }
343 }
344 }
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
345
e0db176 @sprohaska strbuf_add_wrapped*(): Remove unused return value
sprohaska authored
346 void strbuf_add_wrapped_bytes(struct strbuf *buf, const char *data, int len,
98acc83 @peff strbuf: add fixed-length version of add_wrapped_text
peff authored
347 int indent, int indent2, int width)
348 {
349 char *tmp = xstrndup(data, len);
e0db176 @sprohaska strbuf_add_wrapped*(): Remove unused return value
sprohaska authored
350 strbuf_add_wrapped_text(buf, tmp, indent, indent2, width);
98acc83 @peff strbuf: add fixed-length version of add_wrapped_text
peff authored
351 free(tmp);
352 }
353
a7f01c6 @pclouds pretty: support truncating in %>, %< and %><
pclouds authored
354 void strbuf_utf8_replace(struct strbuf *sb_src, int pos, int width,
355 const char *subst)
356 {
357 struct strbuf sb_dst = STRBUF_INIT;
358 char *src = sb_src->buf;
359 char *end = src + sb_src->len;
360 char *dst;
361 int w = 0, subst_len = 0;
362
363 if (subst)
364 subst_len = strlen(subst);
365 strbuf_grow(&sb_dst, sb_src->len + subst_len);
366 dst = sb_dst.buf;
367
368 while (src < end) {
369 char *old;
370 size_t n;
371
372 while ((n = display_mode_esc_sequence_len(src))) {
373 memcpy(dst, src, n);
374 src += n;
375 dst += n;
376 }
377
4308759 @pclouds utf8.c: fix strbuf_utf8_replace() consuming data beyond input string
pclouds authored
378 if (src >= end)
379 break;
380
a7f01c6 @pclouds pretty: support truncating in %>, %< and %><
pclouds authored
381 old = src;
382 n = utf8_width((const char**)&src, NULL);
383 if (!src) /* broken utf-8, do nothing */
384 return;
385 if (n && w >= pos && w < pos + width) {
386 if (subst) {
387 memcpy(dst, subst, subst_len);
388 dst += subst_len;
389 subst = NULL;
390 }
391 w += n;
392 continue;
393 }
394 memcpy(dst, old, src - old);
395 dst += src - old;
396 w += n;
397 }
398 strbuf_setlen(&sb_dst, dst - sb_dst.buf);
980419b pretty: Fix bug in truncation support for %>, %< and %><
Ramsay Jones authored
399 strbuf_swap(sb_src, &sb_dst);
400 strbuf_release(&sb_dst);
a7f01c6 @pclouds pretty: support truncating in %>, %< and %><
pclouds authored
401 }
402
677cfed commit-tree: cope with different ways "utf-8" can be spelled.
Junio C Hamano authored
403 int is_encoding_utf8(const char *name)
404 {
405 if (!name)
406 return 1;
407 if (!strcasecmp(name, "utf-8") || !strcasecmp(name, "utf8"))
408 return 1;
409 return 0;
410 }
411
0e18bcd @gitster reencode_string(): introduce and use same_encoding()
gitster authored
412 int same_encoding(const char *src, const char *dst)
413 {
414 if (is_encoding_utf8(src) && is_encoding_utf8(dst))
415 return 1;
416 return !strcasecmp(src, dst);
417 }
418
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
419 /*
c082196 @jiangxin Add utf8_fprintf helper that returns correct number of columns
jiangxin authored
420 * Wrapper for fprintf and returns the total number of columns required
421 * for the printed string, assuming that the string is utf8.
422 */
423 int utf8_fprintf(FILE *stream, const char *format, ...)
424 {
425 struct strbuf buf = STRBUF_INIT;
426 va_list arg;
427 int columns;
428
429 va_start(arg, format);
430 strbuf_vaddf(&buf, format, arg);
431 va_end(arg);
432
433 columns = fputs(buf.buf, stream);
434 if (0 <= columns) /* keep the error from the I/O */
435 columns = utf8_strwidth(buf.buf);
436 strbuf_release(&buf);
437 return columns;
438 }
439
440 /*
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
441 * Given a buffer and its encoding, return it re-encoded
442 * with iconv. If the conversion fails, returns NULL.
443 */
444 #ifndef NO_ICONV
309dbc8 @drafnel On Solaris choose the OLD_ICONV iconv() declaration based on the UNIX…
drafnel authored
445 #if defined(OLD_ICONV) || (defined(__sun__) && !defined(_XPG6))
fd547a9 Fix a "pointer type missmatch" warning.
Ramsay Jones authored
446 typedef const char * iconv_ibp;
447 #else
448 typedef char * iconv_ibp;
449 #endif
b782bba @pclouds utf8.c: add reencode_string_len() that can handle NULs in string
pclouds authored
450 char *reencode_string_iconv(const char *in, size_t insz, iconv_t conv, int *outsz_p)
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
451 {
76759c7 @tboegi git on Mac OS and precomposed unicode
tboegi authored
452 size_t outsz, outalloc;
fd547a9 Fix a "pointer type missmatch" warning.
Ramsay Jones authored
453 char *out, *outpos;
454 iconv_ibp cp;
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
455
456 outsz = insz;
457 outalloc = outsz + 1; /* for terminating NUL */
458 out = xmalloc(outalloc);
459 outpos = out;
fd547a9 Fix a "pointer type missmatch" warning.
Ramsay Jones authored
460 cp = (iconv_ibp)in;
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
461
462 while (1) {
463 size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);
464
df5213b @johnkeeping utf8: fix iconv error detection
johnkeeping authored
465 if (cnt == (size_t) -1) {
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
466 size_t sofar;
467 if (errno != E2BIG) {
468 free(out);
469 return NULL;
470 }
471 /* insz has remaining number of bytes.
472 * since we started outsz the same as insz,
473 * it is likely that insz is not enough for
474 * converting the rest.
475 */
476 sofar = outpos - out;
477 outalloc = sofar + insz * 2 + 32;
478 out = xrealloc(out, outalloc);
479 outpos = out + sofar;
480 outsz = outalloc - sofar - 1;
481 }
482 else {
483 *outpos = '\0';
b782bba @pclouds utf8.c: add reencode_string_len() that can handle NULs in string
pclouds authored
484 if (outsz_p)
485 *outsz_p = outpos - out;
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
486 break;
487 }
488 }
76759c7 @tboegi git on Mac OS and precomposed unicode
tboegi authored
489 return out;
490 }
491
b782bba @pclouds utf8.c: add reencode_string_len() that can handle NULs in string
pclouds authored
492 char *reencode_string_len(const char *in, int insz,
493 const char *out_encoding, const char *in_encoding,
494 int *outsz)
76759c7 @tboegi git on Mac OS and precomposed unicode
tboegi authored
495 {
496 iconv_t conv;
497 char *out;
498
499 if (!in_encoding)
500 return NULL;
5c680be @peff utf8: accept alternate spellings of UTF-8
peff authored
501
76759c7 @tboegi git on Mac OS and precomposed unicode
tboegi authored
502 conv = iconv_open(out_encoding, in_encoding);
5c680be @peff utf8: accept alternate spellings of UTF-8
peff authored
503 if (conv == (iconv_t) -1) {
504 /*
505 * Some platforms do not have the variously spelled variants of
506 * UTF-8, so let's fall back to trying the most official
507 * spelling. We do so only as a fallback in case the platform
508 * does understand the user's spelling, but not our official
509 * one.
510 */
511 if (is_encoding_utf8(in_encoding))
512 in_encoding = "UTF-8";
513 if (is_encoding_utf8(out_encoding))
514 out_encoding = "UTF-8";
515 conv = iconv_open(out_encoding, in_encoding);
516 if (conv == (iconv_t) -1)
517 return NULL;
518 }
519
b782bba @pclouds utf8.c: add reencode_string_len() that can handle NULs in string
pclouds authored
520 out = reencode_string_iconv(in, insz, conv, outsz);
b45974a Move encoding conversion routine out of mailinfo to utf8.c
Junio C Hamano authored
521 iconv_close(conv);
522 return out;
523 }
524 #endif
6cd3c05 format-patch: RFC 2047 says multi-octet character may not be split
Kirill Smelkov authored
525
526 /*
527 * Returns first character length in bytes for multi-byte `text` according to
528 * `encoding`.
529 *
530 * - The `text` pointer is updated to point at the next character.
531 * - When `remainder_p` is not NULL, on entry `*remainder_p` is how much bytes
532 * we can consume from text, and on exit `*remainder_p` is reduced by returned
533 * character length. Otherwise `text` is treated as limited by NUL.
534 */
535 int mbs_chrlen(const char **text, size_t *remainder_p, const char *encoding)
536 {
537 int chrlen;
538 const char *p = *text;
539 size_t r = (remainder_p ? *remainder_p : SIZE_MAX);
540
541 if (r < 1)
542 return 0;
543
544 if (is_encoding_utf8(encoding)) {
545 pick_one_utf8_char(&p, &r);
546
547 chrlen = p ? (p - *text)
548 : 1 /* not valid UTF-8 -> raw byte sequence */;
549 }
550 else {
551 /*
552 * TODO use iconv to decode one char and obtain its chrlen
553 * for now, let's treat encodings != UTF-8 as one-byte
554 */
555 chrlen = 1;
556 }
557
558 *text += chrlen;
559 if (remainder_p)
560 *remainder_p -= chrlen;
561
562 return chrlen;
563 }
6162a1d @peff utf8: add is_hfs_dotgit() helper
peff authored
564
565 /*
6aaf956 @peff is_hfs_dotgit: loosen over-eager match of \u{..47}
peff authored
566 * Pick the next char from the stream, ignoring codepoints an HFS+ would.
567 * Note that this is _not_ complete by any means. It's just enough
6162a1d @peff utf8: add is_hfs_dotgit() helper
peff authored
568 * to make is_hfs_dotgit() work, and should not be used otherwise.
569 */
570 static ucs_char_t next_hfs_char(const char **in)
571 {
572 while (1) {
573 ucs_char_t out = pick_one_utf8_char(in, NULL);
574 /*
575 * check for malformed utf8. Technically this
576 * gets converted to a percent-sequence, but
577 * returning 0 is good enough for is_hfs_dotgit
578 * to realize it cannot be .git
579 */
580 if (!*in)
581 return 0;
582
583 /* these code points are ignored completely */
584 switch (out) {
585 case 0x200c: /* ZERO WIDTH NON-JOINER */
586 case 0x200d: /* ZERO WIDTH JOINER */
587 case 0x200e: /* LEFT-TO-RIGHT MARK */
588 case 0x200f: /* RIGHT-TO-LEFT MARK */
589 case 0x202a: /* LEFT-TO-RIGHT EMBEDDING */
590 case 0x202b: /* RIGHT-TO-LEFT EMBEDDING */
591 case 0x202c: /* POP DIRECTIONAL FORMATTING */
592 case 0x202d: /* LEFT-TO-RIGHT OVERRIDE */
593 case 0x202e: /* RIGHT-TO-LEFT OVERRIDE */
594 case 0x206a: /* INHIBIT SYMMETRIC SWAPPING */
595 case 0x206b: /* ACTIVATE SYMMETRIC SWAPPING */
596 case 0x206c: /* INHIBIT ARABIC FORM SHAPING */
597 case 0x206d: /* ACTIVATE ARABIC FORM SHAPING */
598 case 0x206e: /* NATIONAL DIGIT SHAPES */
599 case 0x206f: /* NOMINAL DIGIT SHAPES */
600 case 0xfeff: /* ZERO WIDTH NO-BREAK SPACE */
601 continue;
602 }
603
6aaf956 @peff is_hfs_dotgit: loosen over-eager match of \u{..47}
peff authored
604 return out;
6162a1d @peff utf8: add is_hfs_dotgit() helper
peff authored
605 }
606 }
607
608 int is_hfs_dotgit(const char *path)
609 {
610 ucs_char_t c;
611
6aaf956 @peff is_hfs_dotgit: loosen over-eager match of \u{..47}
peff authored
612 c = next_hfs_char(&path);
613 if (c != '.')
614 return 0;
615 c = next_hfs_char(&path);
616
617 /*
618 * there's a great deal of other case-folding that occurs
619 * in HFS+, but this is enough to catch anything that will
620 * convert to ".git"
621 */
622 if (c != 'g' && c != 'G')
623 return 0;
624 c = next_hfs_char(&path);
625 if (c != 'i' && c != 'I')
626 return 0;
627 c = next_hfs_char(&path);
628 if (c != 't' && c != 'T')
6162a1d @peff utf8: add is_hfs_dotgit() helper
peff authored
629 return 0;
630 c = next_hfs_char(&path);
631 if (c && !is_dir_sep(c))
632 return 0;
633
634 return 1;
635 }
dde843e @gitster utf8-bom: introduce skip_utf8_bom() helper
gitster authored
636
637 const char utf8_bom[] = "\357\273\277";
638
639 int skip_utf8_bom(char **text, size_t len)
640 {
641 if (len < strlen(utf8_bom) ||
642 memcmp(*text, utf8_bom, strlen(utf8_bom)))
643 return 0;
644 *text += strlen(utf8_bom);
645 return 1;
646 }
110dcda @KarthikNayak utf8: add function to align a string into given strbuf
KarthikNayak authored
647
648 void strbuf_utf8_align(struct strbuf *buf, align_type position, unsigned int width,
649 const char *s)
650 {
651 int slen = strlen(s);
652 int display_len = utf8_strnwidth(s, slen, 0);
653 int utf8_compensation = slen - display_len;
654
655 if (display_len >= width) {
656 strbuf_addstr(buf, s);
657 return;
658 }
659
660 if (position == ALIGN_LEFT)
661 strbuf_addf(buf, "%-*s", width + utf8_compensation, s);
662 else if (position == ALIGN_MIDDLE) {
663 int left = (width - display_len) / 2;
664 strbuf_addf(buf, "%*s%-*s", left, "", width - left + utf8_compensation, s);
665 } else if (position == ALIGN_RIGHT)
666 strbuf_addf(buf, "%*s", width + utf8_compensation, s);
667 }
Something went wrong with that request. Please try again.