summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLukas Fleischer <lfleischer@calcurse.org>2017-08-30 16:17:28 +0200
committerLukas Fleischer <lfleischer@calcurse.org>2017-08-30 16:17:28 +0200
commit273e32d43d72086ed9b0eeae9e83b2689b0f6154 (patch)
tree1feb05944eb9dc069b64cc328b3225fad8ed9dc8 /src
parent6cb26c2f27020e52053b1f1b7e611dc4e657cd99 (diff)
downloadcalcurse-273e32d43d72086ed9b0eeae9e83b2689b0f6154.tar.gz
calcurse-273e32d43d72086ed9b0eeae9e83b2689b0f6154.zip
Factor out UTF-8 code point decoding
Signed-off-by: Lukas Fleischer <lfleischer@calcurse.org>
Diffstat (limited to 'src')
-rw-r--r--src/calcurse.h1
-rw-r--r--src/utf8.c31
2 files changed, 16 insertions, 16 deletions
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 {