MCPcopy Create free account
hub / github.com/Serial-Studio/Serial-Studio / utf8_decode

Function utf8_decode

lib/lua/src/lutf8lib.c:61–88  ·  view source on GitHub ↗

** Decode one UTF-8 sequence, returning NULL if byte sequence is ** invalid. The array 'limits' stores the minimum value for each ** sequence length, to check for overlong representations. Its first ** entry forces an error for non-ascii bytes with no continuation ** bytes (count == 0). */

Source from the content-addressed store, hash-verified

59** bytes (count == 0).
60*/
61static const char *utf8_decode (const char *s, utfint *val, int strict) {
62 static const utfint limits[] =
63 {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
64 unsigned int c = (unsigned char)s[0];
65 utfint res = 0; /* final result */
66 if (c < 0x80) /* ascii? */
67 res = c;
68 else {
69 int count = 0; /* to count number of continuation bytes */
70 for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
71 unsigned int cc = (unsigned char)s[++count]; /* read next byte */
72 if (!iscont(cc)) /* not a continuation byte? */
73 return NULL; /* invalid byte sequence */
74 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
75 }
76 res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
77 if (count > 5 || res > MAXUTF || res < limits[count])
78 return NULL; /* invalid byte sequence */
79 s += count; /* skip continuation bytes read */
80 }
81 if (strict) {
82 /* check for invalid code points; too large or surrogates */
83 if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
84 return NULL;
85 }
86 if (val) *val = res;
87 return s + 1; /* +1 to include first byte */
88}
89
90
91/*

Callers 3

utflenFunction · 0.85
codepointFunction · 0.85
iter_auxFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected