ref: 4a2f3ed4e7ca9287c29afd21a4f4b77801f682ac
parent: 20ac9a45005c9b50fc0e1a733d6ac11101d85542
author: Ali Gholami Rudi <ali@rudi.ir>
date: Thu Mar 12 20:03:02 EDT 2020
post: make utf8len more compact
--- a/post.c
+++ b/post.c
@@ -54,10 +54,8 @@
static int utf8len(int c)
{
- 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)
@@ -550,13 +548,13 @@
static int utf8code(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;
}