From: "Torsten Bögershausen" <tboegi@web.de>
To: git@vger.kernel.org
Cc: kevin@bracey.fi, peter@softwolves.pp.se, tboegi@web.de
Subject: [PATCH 1/2] utf8.c: use a table for double_width
Date: Fri, 09 May 2014 23:51:38 +0200 [thread overview]
Message-ID: <536D4DEA.7070406@web.de> (raw)
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>
---
utf8.c | 41 ++++++++++++++++++-----------------------
1 file changed, 18 insertions(+), 23 deletions(-)
diff --git a/utf8.c b/utf8.c
index 77c28d4..b5d8136 100644
--- a/utf8.c
+++ b/utf8.c
@@ -126,6 +126,19 @@ static int git_wcwidth(ucs_char_t ch)
{ 0x1D1AA, 0x1D1AD }, { 0xE0001, 0xE0001 },
{ 0xE0020, 0xE007F }, { 0xE0100, 0xE01EF }
};
+ static const struct interval double_width[] = {
+ { 0x1100, 0x115F },
+ { 0x2329, 0x232A },
+ { 0x2E80, 0x303E },
+ { 0x3040, 0xA4CF },
+ { 0xAC00, 0xD7A3 },
+ { 0xF900, 0xFAFF },
+ { 0xFE30, 0xFE6F },
+ { 0xFF00, 0xFF60 },
+ { 0xFFE0, 0xFFE6 },
+ { 0x20000, 0x2FFFD },
+ { 0x30000, 0x3FFFD }
+ };
/* test for 8-bit control characters */
if (ch == 0)
@@ -138,30 +151,12 @@ static int git_wcwidth(ucs_char_t ch)
/ sizeof(struct interval) - 1))
return 0;
- /*
- * If we arrive here, ch is neither a combining nor a C0/C1
- * control character.
- */
+ /* binary search in table of double width characters */
+ if (bisearch(ch, double_width, sizeof(double_width)
+ / sizeof(struct interval) - 1))
+ return 2;
- return 1 +
- (ch >= 0x1100 &&
- /* Hangul Jamo init. consonants */
- (ch <= 0x115f ||
- ch == 0x2329 || ch == 0x232a ||
- /* CJK ... Yi */
- (ch >= 0x2e80 && ch <= 0xa4cf &&
- ch != 0x303f) ||
- /* Hangul Syllables */
- (ch >= 0xac00 && ch <= 0xd7a3) ||
- /* CJK Compatibility Ideographs */
- (ch >= 0xf900 && ch <= 0xfaff) ||
- /* CJK Compatibility Forms */
- (ch >= 0xfe30 && ch <= 0xfe6f) ||
- /* Fullwidth Forms */
- (ch >= 0xff00 && ch <= 0xff60) ||
- (ch >= 0xffe0 && ch <= 0xffe6) ||
- (ch >= 0x20000 && ch <= 0x2fffd) ||
- (ch >= 0x30000 && ch <= 0x3fffd)));
+ return 1;
}
/*
--
1.9.2.691.g8d8dc6d
reply other threads:[~2014-05-09 21:51 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=536D4DEA.7070406@web.de \
--to=tboegi@web.de \
--cc=git@vger.kernel.org \
--cc=kevin@bracey.fi \
--cc=peter@softwolves.pp.se \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).