tuc: make uc_dec() more compact by removing its loop - neatvi - [fork] simple vi-type editor with UTF-8 support (HTM) git clone git://src.adamsgaard.dk/neatvi (DIR) Log (DIR) Files (DIR) Refs (DIR) README --- (DIR) commit 98fc156cbe154ff745f271b07f5e97d54807c858 (DIR) parent 7c800ce91775309ad896f377d3be4f994f398e44 (HTM) Author: Ali Gholami Rudi <ali@rudi.ir> Date: Thu, 12 Mar 2020 20:05:14 +0330 uc: make uc_dec() more compact by removing its loop Diffstat: M regex.c | 18 ++++++++++-------- M uc.c | 7 ++----- 2 files changed, 12 insertions(+), 13 deletions(-) --- (DIR) diff --git a/regex.c b/regex.c t@@ -113,14 +113,16 @@ static int uc_len(char *s) static int uc_dec(char *s) { - int result; - int l = uc_len(s); - if (l <= 1) - return (unsigned char) *s; - result = (0x3f >> --l) & (unsigned char) *s++; - while (l--) - result = (result << 6) | ((unsigned char) *s++ & 0x3f); - return result; + int c = (unsigned char) s[0]; + if (!(c & 0x80)) + return c; + if (!(c & 0x20)) + return ((c & 0x1f) << 6) | (s[1] & 0x3f); + if (!(c & 0x10)) + return ((c & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f); + if (!(c & 0x08)) + return ((c & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f); + return c; } static void ratom_copy(struct ratom *dst, struct ratom *src) (DIR) diff --git a/uc.c b/uc.c t@@ -36,17 +36,14 @@ int uc_slen(char *s) int uc_code(char *s) { int c = (unsigned char) s[0]; - int l; if (!(c & 0x80)) return c; if (!(c & 0x20)) return ((c & 0x1f) << 6) | (s[1] & 0x3f); if (!(c & 0x10)) return ((c & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f); - l = uc_len(s); - c = (0x3f >> --l) & (unsigned char) *s++; - while (l--) - c = (c << 6) | ((unsigned char) *s++ & 0x3f); + if (!(c & 0x08)) + return ((c & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f); return c; }