From 273e32d43d72086ed9b0eeae9e83b2689b0f6154 Mon Sep 17 00:00:00 2001 From: Lukas Fleischer Date: Wed, 30 Aug 2017 16:17:28 +0200 Subject: Factor out UTF-8 code point decoding Signed-off-by: Lukas Fleischer --- src/calcurse.h | 1 + src/utf8.c | 31 +++++++++++++++---------------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/calcurse.h b/src/calcurse.h index 7abeb03..c5ad2fe 100644 --- a/src/calcurse.h +++ b/src/calcurse.h @@ -1099,6 +1099,7 @@ int ui_todo_get_view(void); void ui_todo_set_view(int); /* utf8.c */ +int utf8_ord(const char *); int utf8_width(char *); int utf8_strwidth(char *); int utf8_chop(char *, int); diff --git a/src/utf8.c b/src/utf8.c index 4da2bb9..47d83dc 100644 --- a/src/utf8.c +++ b/src/utf8.c @@ -269,43 +269,42 @@ static const struct utf8_range utf8_widthtab[] = { {0xe0100, 0xe01ef, 0} }; -/* Get the width of a UTF-8 character. */ -int utf8_width(char *s) +/* Decode a UTF-8 code point. */ +int utf8_ord(const char *s) { - int val, low, high, cur; - if (UTF8_ISCONT(*s)) return 0; switch (UTF8_LENGTH(*s)) { case 1: - val = s[0]; - break; + return s[0]; case 2: - val = (s[1] & 0x3f) | (s[0] & 0x1f) << 6; - break; + return (s[1] & 0x3f) | (s[0] & 0x1f) << 6; case 3: - val = ((s[2] & 0x3f) | (s[1] & 0x3f) << 6) | + return ((s[2] & 0x3f) | (s[1] & 0x3f) << 6) | (s[0] & 0x0f) << 12; - break; case 4: - val = (((s[3] & 0x3f) | (s[2] & 0x3f) << 6) | + return (((s[3] & 0x3f) | (s[2] & 0x3f) << 6) | (s[1] & 0x3f) << 12) | (s[0] & 0x3f) << 18; - break; case 5: - val = ((((s[4] & 0x3f) | (s[3] & 0x3f) << 6) | + return ((((s[4] & 0x3f) | (s[3] & 0x3f) << 6) | (s[2] & 0x3f) << 12) | (s[1] & 0x3f) << 18) | (s[0] & 0x3f) << 24; - break; case 6: - val = (((((s[5] & 0x3f) | (s[4] & 0x3f) << 6) | + return (((((s[5] & 0x3f) | (s[4] & 0x3f) << 6) | (s[3] & 0x3f) << 12) | (s[2] & 0x3f) << 18) | (s[1] & 0x3f) << 24) | (s[0] & 0x3f) << 30; - break; default: return 0; } +} + +/* Get the width of a UTF-8 character. */ +int utf8_width(char *s) +{ + int val, low, high, cur; + val = utf8_ord(s); low = 0; high = ARRAY_SIZE(utf8_widthtab); do { -- cgit v1.2.3-70-g09d2