1#include "git-compat-util.h"2#include "utf8.h"34/* This code is originally from http://www.cl.cam.ac.uk/~mgk25/ucs/ */56typedef unsigned int ucs_char_t; /* assuming 32bit int */78struct interval {9int first;10int last;11};1213/* auxiliary function for binary search in interval table */14static int bisearch(ucs_char_t ucs, const struct interval *table, int max)15{16int min = 0;17int mid;1819if (ucs < table[0].first || ucs > table[max].last)20return 0;21while (max >= min) {22mid = (min + max) / 2;23if (ucs > table[mid].last)24min = mid + 1;25else if (ucs < table[mid].first)26max = mid - 1;27else28return 1;29}3031return 0;32}3334/* The following two functions define the column width of an ISO 1064635* character as follows:36*37* - The null character (U+0000) has a column width of 0.38*39* - Other C0/C1 control characters and DEL will lead to a return40* value of -1.41*42* - Non-spacing and enclosing combining characters (general43* category code Mn or Me in the Unicode database) have a44* column width of 0.45*46* - SOFT HYPHEN (U+00AD) has a column width of 1.47*48* - Other format characters (general category code Cf in the Unicode49* database) and ZERO WIDTH SPACE (U+200B) have a column width of 0.50*51* - Hangul Jamo medial vowels and final consonants (U+1160-U+11FF)52* have a column width of 0.53*54* - Spacing characters in the East Asian Wide (W) or East Asian55* Full-width (F) category as defined in Unicode Technical56* Report #11 have a column width of 2.57*58* - All remaining characters (including all printable59* ISO 8859-1 and WGL4 characters, Unicode control characters,60* etc.) have a column width of 1.61*62* This implementation assumes that ucs_char_t characters are encoded63* in ISO 10646.64*/6566static int git_wcwidth(ucs_char_t ch)67{68/*69* Sorted list of non-overlapping intervals of non-spacing characters,70* generated by71* "uniset +cat=Me +cat=Mn +cat=Cf -00AD +1160-11FF +200B c".72*/73static 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 }116};117118/* test for 8-bit control characters */119if (ch == 0)120return 0;121if (ch < 32 || (ch >= 0x7f && ch < 0xa0))122return -1;123124/* binary search in table of non-spacing characters */125if (bisearch(ch, combining, sizeof(combining)126/ sizeof(struct interval) - 1))127return 0;128129/*130* If we arrive here, ch is neither a combining nor a C0/C1131* control character.132*/133134return 1 +135(ch >= 0x1100 &&136/* Hangul Jamo init. consonants */137(ch <= 0x115f ||138ch == 0x2329 || ch == 0x232a ||139/* CJK ... Yi */140(ch >= 0x2e80 && ch <= 0xa4cf &&141ch != 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)));153}154155/*156* This function returns the number of columns occupied by the character157* pointed to by the variable start. The pointer is updated to point at158* the next character. If it was not valid UTF-8, the pointer is set to NULL.159*/160int utf8_width(const char **start)161{162unsigned char *s = (unsigned char *)*start;163ucs_char_t ch;164165if (*s < 0x80) {166/* 0xxxxxxx */167ch = *s;168*start += 1;169} else if ((s[0] & 0xe0) == 0xc0) {170/* 110XXXXx 10xxxxxx */171if ((s[1] & 0xc0) != 0x80 ||172/* overlong? */173(s[0] & 0xfe) == 0xc0)174goto invalid;175ch = ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);176*start += 2;177} else if ((s[0] & 0xf0) == 0xe0) {178/* 1110XXXX 10Xxxxxx 10xxxxxx */179if ((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))188goto invalid;189ch = ((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 */194if ((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)201goto invalid;202ch = ((s[0] & 0x07) << 18) | ((s[1] & 0x3f) << 12) |203((s[2] & 0x3f) << 6) | (s[3] & 0x3f);204*start += 4;205} else {206invalid:207*start = NULL;208return 0;209}210211return git_wcwidth(ch);212}213214int is_utf8(const char *text)215{216while (*text) {217if (*text == '\n' || *text == '\t' || *text == '\r') {218text++;219continue;220}221utf8_width(&text);222if (!text)223return 0;224}225return 1;226}227228static void print_spaces(int count)229{230static const char s[] = " ";231while (count >= sizeof(s)) {232fwrite(s, sizeof(s) - 1, 1, stdout);233count -= sizeof(s) - 1;234}235fwrite(s, count, 1, stdout);236}237238/*239* Wrap the text, if necessary. The variable indent is the indent for the240* first line, indent2 is the indent for all other lines.241* If indent is negative, assume that already -indent columns have been242* consumed (and no extra indent is necessary for the first line).243*/244int print_wrapped_text(const char *text, int indent, int indent2, int width)245{246int w = indent, assume_utf8 = is_utf8(text);247const char *bol = text, *space = NULL;248249if (indent < 0) {250w = -indent;251space = text;252}253254for (;;) {255char c = *text;256if (!c || isspace(c)) {257if (w < width || !space) {258const char *start = bol;259if (space)260start = space;261else262print_spaces(indent);263fwrite(start, text - start, 1, stdout);264if (!c)265return w;266else if (c == '\t')267w |= 0x07;268space = text;269w++;270text++;271}272else {273putchar('\n');274text = bol = space + isspace(*space);275space = NULL;276w = indent = indent2;277}278continue;279}280if (assume_utf8)281w += utf8_width(&text);282else {283w++;284text++;285}286}287}288289int is_encoding_utf8(const char *name)290{291if (!name)292return 1;293if (!strcasecmp(name, "utf-8") || !strcasecmp(name, "utf8"))294return 1;295return 0;296}297298/*299* Given a buffer and its encoding, return it re-encoded300* with iconv. If the conversion fails, returns NULL.301*/302#ifndef NO_ICONV303#ifdef OLD_ICONV304typedef const char * iconv_ibp;305#else306typedef char * iconv_ibp;307#endif308char *reencode_string(const char *in, const char *out_encoding, const char *in_encoding)309{310iconv_t conv;311size_t insz, outsz, outalloc;312char *out, *outpos;313iconv_ibp cp;314315if (!in_encoding)316return NULL;317conv = iconv_open(out_encoding, in_encoding);318if (conv == (iconv_t) -1)319return NULL;320insz = strlen(in);321outsz = insz;322outalloc = outsz + 1; /* for terminating NUL */323out = xmalloc(outalloc);324outpos = out;325cp = (iconv_ibp)in;326327while (1) {328size_t cnt = iconv(conv, &cp, &insz, &outpos, &outsz);329330if (cnt == -1) {331size_t sofar;332if (errno != E2BIG) {333free(out);334iconv_close(conv);335return NULL;336}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 for340* converting the rest.341*/342sofar = outpos - out;343outalloc = sofar + insz * 2 + 32;344out = xrealloc(out, outalloc);345outpos = out + sofar;346outsz = outalloc - sofar - 1;347}348else {349*outpos = '\0';350break;351}352}353iconv_close(conv);354return out;355}356#endif