{"thread":{"id":"36629","subject":"[PATCH 1/2] utf8.c: use a table for double_width","startedAt":"2014-05-09T21:51:38Z","lastAt":"2014-05-09T21:51:38Z","messageCount":1,"participants":["Torsten Bögershausen"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"241232","messageId":"536D4DEA.7070406@web.de","threadId":"36629","inReplyTo":null,"subject":"[PATCH 1/2] utf8.c: use a table for double_width","fromName":"Torsten Bögershausen","fromEmail":"tboegi@web.de","sentAt":"2014-05-09T21:51:38Z","receivedAt":"2014-05-09T21:51:38Z","isPatch":true,"sender":{"key":"tboegi@web.de","avatar":"https://avatars.githubusercontent.com/u/7138363?v=4"},"body":"Refactor git_wcwidth() and replace the if-else-if chain.\nUse the table double_width which is scanned by the bisearch() function,\nwhich is already used to find combining code points.\n\nSigned-off-by: Torsten Bögershausen <tboegi@web.de>\n---\n utf8.c | 41 ++++++++++++++++++-----------------------\n 1 file changed, 18 insertions(+), 23 deletions(-)\n\ndiff --git a/utf8.c b/utf8.c\nindex 77c28d4..b5d8136 100644\n--- a/utf8.c\n+++ b/utf8.c\n@@ -126,6 +126,19 @@ static int git_wcwidth(ucs_char_t ch)\n \t\t{ 0x1D1AA, 0x1D1AD }, { 0xE0001, 0xE0001 },\n \t\t{ 0xE0020, 0xE007F }, { 0xE0100, 0xE01EF }\n \t};\n+\tstatic const struct interval double_width[] = {\n+\t\t{ 0x1100, 0x115F },\n+\t\t{ 0x2329, 0x232A },\n+\t\t{ 0x2E80, 0x303E },\n+\t\t{ 0x3040, 0xA4CF },\n+\t\t{ 0xAC00, 0xD7A3 },\n+\t\t{ 0xF900, 0xFAFF },\n+\t\t{ 0xFE30, 0xFE6F },\n+\t\t{ 0xFF00, 0xFF60 },\n+\t\t{ 0xFFE0, 0xFFE6 },\n+\t\t{ 0x20000, 0x2FFFD },\n+\t\t{ 0x30000, 0x3FFFD }\n+\t};\n \n \t/* test for 8-bit control characters */\n \tif (ch == 0)\n@@ -138,30 +151,12 @@ static int git_wcwidth(ucs_char_t ch)\n \t\t\t\t/ sizeof(struct interval) - 1))\n \t\treturn 0;\n \n-\t/*\n-\t * If we arrive here, ch is neither a combining nor a C0/C1\n-\t * control character.\n-\t */\n+\t/* binary search in table of double width characters */\n+\tif (bisearch(ch, double_width, sizeof(double_width)\n+\t\t\t\t/ sizeof(struct interval) - 1))\n+\t\treturn 2;\n \n-\treturn 1 +\n-\t\t(ch >= 0x1100 &&\n-                    /* Hangul Jamo init. consonants */\n-\t\t (ch <= 0x115f ||\n-\t\t  ch == 0x2329 || ch == 0x232a ||\n-                  /* CJK ... Yi */\n-\t\t  (ch >= 0x2e80 && ch <= 0xa4cf &&\n-\t\t   ch != 0x303f) ||\n-\t\t  /* Hangul Syllables */\n-\t\t  (ch >= 0xac00 && ch <= 0xd7a3) ||\n-\t\t  /* CJK Compatibility Ideographs */\n-\t\t  (ch >= 0xf900 && ch <= 0xfaff) ||\n-\t\t  /* CJK Compatibility Forms */\n-\t\t  (ch >= 0xfe30 && ch <= 0xfe6f) ||\n-\t\t  /* Fullwidth Forms */\n-\t\t  (ch >= 0xff00 && ch <= 0xff60) ||\n-\t\t  (ch >= 0xffe0 && ch <= 0xffe6) ||\n-\t\t  (ch >= 0x20000 && ch <= 0x2fffd) ||\n-\t\t  (ch >= 0x30000 && ch <= 0x3fffd)));\n+\treturn 1;\n }\n \n /*\n-- \n1.9.2.691.g8d8dc6d\n"}]}