Factor out UTF-8 code point decoding

Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
This commit is contained in:
Lukas Fleischer 2017-08-30 16:17:28 +02:00
parent 6cb26c2f27
commit 273e32d43d
2 changed files with 16 additions and 16 deletions

View File

@ -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);

View File

@ -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 {