utf8.c: use a table for double_width

Refactor git_wcwidth() and replace the if-else-if chain. Use the table double_width which is scanned by the bisearch() function, which is already used to find combining code points. Signed-off-by: Torsten Bögershausen <tboegi@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Torsten Bögershausen committed May 9, 2014 at 23:51 UTC 08460345b5a221c0076a48c6be875d64b78b6015
1 file changed +18 -23
utf8.c
+18 -23
@@ -126,6 +126,19 @@ static int git_wcwidth(ucs_char_t ch)
126 { 0x1D1AA, 0x1D1AD }, { 0xE0001, 0xE0001 },
127 { 0xE0020, 0xE007F }, { 0xE0100, 0xE01EF }
128 };
129 + static const struct interval double_width[] = {
130 + { 0x1100, 0x115F },
131 + { 0x2329, 0x232A },
132 + { 0x2E80, 0x303E },
133 + { 0x3040, 0xA4CF },
134 + { 0xAC00, 0xD7A3 },
135 + { 0xF900, 0xFAFF },
136 + { 0xFE30, 0xFE6F },
137 + { 0xFF00, 0xFF60 },
138 + { 0xFFE0, 0xFFE6 },
139 + { 0x20000, 0x2FFFD },
140 + { 0x30000, 0x3FFFD }
141 + };
142
143 /* test for 8-bit control characters */
144 if (ch == 0)
@@ -138,30 +151,12 @@ static int git_wcwidth(ucs_char_t ch)
151 / sizeof(struct interval) - 1))
152 return 0;
153
141 - /*
142 - * If we arrive here, ch is neither a combining nor a C0/C1
143 - * control character.
144 - */
154 + /* binary search in table of double width characters */
155 + if (bisearch(ch, double_width, sizeof(double_width)
156 + / sizeof(struct interval) - 1))
157 + return 2;
158
146 - return 1 +
147 - (ch >= 0x1100 &&
148 - /* Hangul Jamo init. consonants */
149 - (ch <= 0x115f ||
150 - ch == 0x2329 || ch == 0x232a ||
151 - /* CJK ... Yi */
152 - (ch >= 0x2e80 && ch <= 0xa4cf &&
153 - ch != 0x303f) ||
154 - /* Hangul Syllables */
155 - (ch >= 0xac00 && ch <= 0xd7a3) ||
156 - /* CJK Compatibility Ideographs */
157 - (ch >= 0xf900 && ch <= 0xfaff) ||
158 - /* CJK Compatibility Forms */
159 - (ch >= 0xfe30 && ch <= 0xfe6f) ||
160 - /* Fullwidth Forms */
161 - (ch >= 0xff00 && ch <= 0xff60) ||
162 - (ch >= 0xffe0 && ch <= 0xffe6) ||
163 - (ch >= 0x20000 && ch <= 0x2fffd) ||
164 - (ch >= 0x30000 && ch <= 0x3fffd)));
159 + return 1;
160 }
161
162 /*