tuc: make uc_len() more compact by combining the first two conditions - 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 cf0d56f4f7ecd22878eda626d835db86d1fd8cf7
(DIR) parent 98fc156cbe154ff745f271b07f5e97d54807c858
(HTM) Author: Ali Gholami Rudi <ali@rudi.ir>
Date: Fri, 13 Mar 2020 00:01:38 +0330
uc: make uc_len() more compact by combining the first two conditions
Diffstat:
M regex.c | 12 +++++-------
M uc.c | 12 +++++-------
2 files changed, 10 insertions(+), 14 deletions(-)
---
(DIR) diff --git a/regex.c b/regex.c
t@@ -98,10 +98,8 @@ static void rnode_free(struct rnode *rnode)
static int uc_len(char *s)
{
int c = (unsigned char) s[0];
- if (~c & 0x80) /* ASCII */
+ if (~c & 0xc0) /* ASCII or invalid */
return c > 0;
- if (~c & 0x40) /* invalid UTF-8 */
- return 1;
if (~c & 0x20)
return 2;
if (~c & 0x10)
t@@ -114,13 +112,13 @@ static int uc_len(char *s)
static int uc_dec(char *s)
{
int c = (unsigned char) s[0];
- if (!(c & 0x80))
+ if (~c & 0xc0) /* ASCII or invalid */
return c;
- if (!(c & 0x20))
+ if (~c & 0x20)
return ((c & 0x1f) << 6) | (s[1] & 0x3f);
- if (!(c & 0x10))
+ if (~c & 0x10)
return ((c & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
- if (!(c & 0x08))
+ if (~c & 0x08)
return ((c & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
return c;
}
(DIR) diff --git a/uc.c b/uc.c
t@@ -10,10 +10,8 @@
int uc_len(char *s)
{
int c = (unsigned char) s[0];
- if (~c & 0x80) /* ASCII */
+ if (~c & 0xc0) /* ASCII or invalid */
return c > 0;
- if (~c & 0x40) /* invalid UTF-8 */
- return 1;
if (~c & 0x20)
return 2;
if (~c & 0x10)
t@@ -36,13 +34,13 @@ int uc_slen(char *s)
int uc_code(char *s)
{
int c = (unsigned char) s[0];
- if (!(c & 0x80))
+ if (~c & 0xc0) /* ASCII or invalid */
return c;
- if (!(c & 0x20))
+ if (~c & 0x20)
return ((c & 0x1f) << 6) | (s[1] & 0x3f);
- if (!(c & 0x10))
+ if (~c & 0x10)
return ((c & 0x0f) << 12) | ((s[1] & 0x3f) << 6) | (s[2] & 0x3f);
- if (!(c & 0x08))
+ if (~c & 0x08)
return ((c & 0x07) << 18) | ((s[1] & 0x3f) << 12) | ((s[2] & 0x3f) << 6) | (s[3] & 0x3f);
return c;
}