Skip to content

Commit 0846034

Browse files
tboegigitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d813ab9 commit 0846034

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

utf8.c

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,19 @@ static int git_wcwidth(ucs_char_t ch)
126126
{ 0x1D1AA, 0x1D1AD }, { 0xE0001, 0xE0001 },
127127
{ 0xE0020, 0xE007F }, { 0xE0100, 0xE01EF }
128128
};
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+
};
129142

130143
/* test for 8-bit control characters */
131144
if (ch == 0)
@@ -138,30 +151,12 @@ static int git_wcwidth(ucs_char_t ch)
138151
/ sizeof(struct interval) - 1))
139152
return 0;
140153

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;
145158

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;
165160
}
166161

167162
/*

0 commit comments

Comments
 (0)