1*bdda0531Snikita /* $NetBSD: lutf8lib.c,v 1.11 2023/06/08 21:12:08 nikita Exp $ */
24ab4902eSlneto
34ab4902eSlneto /*
4f0dad708Snikita ** Id: lutf8lib.c
54ab4902eSlneto ** Standard library for UTF-8 manipulation
64ab4902eSlneto ** See Copyright Notice in lua.h
74ab4902eSlneto */
84ab4902eSlneto
928a92413Slneto #define lutf8lib_c
1028a92413Slneto #define LUA_LIB
1128a92413Slneto
1228a92413Slneto #include "lprefix.h"
1328a92413Slneto
144ab4902eSlneto
1573008250Slneto #ifndef _KERNEL
164ab4902eSlneto #include <assert.h>
17bee09862Smbalmer #include <limits.h>
184ab4902eSlneto #include <stdlib.h>
194ab4902eSlneto #include <string.h>
202d6cb6c2Slneto #endif /* _KERNEL */
214ab4902eSlneto
224ab4902eSlneto #include "lua.h"
234ab4902eSlneto
244ab4902eSlneto #include "lauxlib.h"
254ab4902eSlneto #include "lualib.h"
264ab4902eSlneto
27f0dad708Snikita
28f0dad708Snikita #define MAXUNICODE 0x10FFFFu
29f0dad708Snikita
30f0dad708Snikita #define MAXUTF 0x7FFFFFFFu
31f0dad708Snikita
32*bdda0531Snikita
33d5cb1924Snikita #define MSGInvalid "invalid UTF-8 code"
34d5cb1924Snikita
35f0dad708Snikita /*
36f0dad708Snikita ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
37f0dad708Snikita */
38f0dad708Snikita #if (UINT_MAX >> 30) >= 1
39f0dad708Snikita typedef unsigned int utfint;
40f0dad708Snikita #else
41f0dad708Snikita typedef unsigned long utfint;
42f0dad708Snikita #endif
43f0dad708Snikita
444ab4902eSlneto
45d5cb1924Snikita #define iscont(c) (((c) & 0xC0) == 0x80)
46d5cb1924Snikita #define iscontp(p) iscont(*(p))
474ab4902eSlneto
484ab4902eSlneto
494ab4902eSlneto /* from strlib */
504ab4902eSlneto /* translate a relative string position: negative means back from end */
u_posrelat(lua_Integer pos,size_t len)514ab4902eSlneto static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
524ab4902eSlneto if (pos >= 0) return pos;
534ab4902eSlneto else if (0u - (size_t)pos > len) return 0;
544ab4902eSlneto else return (lua_Integer)len + pos + 1;
554ab4902eSlneto }
564ab4902eSlneto
574ab4902eSlneto
584ab4902eSlneto /*
59f0dad708Snikita ** Decode one UTF-8 sequence, returning NULL if byte sequence is
60f0dad708Snikita ** invalid. The array 'limits' stores the minimum value for each
61f0dad708Snikita ** sequence length, to check for overlong representations. Its first
62f0dad708Snikita ** entry forces an error for non-ascii bytes with no continuation
63f0dad708Snikita ** bytes (count == 0).
644ab4902eSlneto */
utf8_decode(const char * s,utfint * val,int strict)65f0dad708Snikita static const char *utf8_decode (const char *s, utfint *val, int strict) {
66f0dad708Snikita static const utfint limits[] =
67f0dad708Snikita {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
68f0dad708Snikita unsigned int c = (unsigned char)s[0];
69f0dad708Snikita utfint res = 0; /* final result */
704ab4902eSlneto if (c < 0x80) /* ascii? */
714ab4902eSlneto res = c;
724ab4902eSlneto else {
734ab4902eSlneto int count = 0; /* to count number of continuation bytes */
74f0dad708Snikita for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
75f0dad708Snikita unsigned int cc = (unsigned char)s[++count]; /* read next byte */
76d5cb1924Snikita if (!iscont(cc)) /* not a continuation byte? */
774ab4902eSlneto return NULL; /* invalid byte sequence */
784ab4902eSlneto res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
794ab4902eSlneto }
80f0dad708Snikita res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
81f0dad708Snikita if (count > 5 || res > MAXUTF || res < limits[count])
824ab4902eSlneto return NULL; /* invalid byte sequence */
834ab4902eSlneto s += count; /* skip continuation bytes read */
844ab4902eSlneto }
85f0dad708Snikita if (strict) {
86f0dad708Snikita /* check for invalid code points; too large or surrogates */
87f0dad708Snikita if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
88f0dad708Snikita return NULL;
89f0dad708Snikita }
904ab4902eSlneto if (val) *val = res;
91f0dad708Snikita return s + 1; /* +1 to include first byte */
924ab4902eSlneto }
934ab4902eSlneto
944ab4902eSlneto
954ab4902eSlneto /*
96f0dad708Snikita ** utf8len(s [, i [, j [, lax]]]) --> number of characters that
97f0dad708Snikita ** start in the range [i,j], or nil + current position if 's' is not
98f0dad708Snikita ** well formed in that interval
994ab4902eSlneto */
utflen(lua_State * L)1004ab4902eSlneto static int utflen (lua_State *L) {
101f0dad708Snikita lua_Integer n = 0; /* counter for the number of characters */
102f0dad708Snikita size_t len; /* string length in bytes */
1034ab4902eSlneto const char *s = luaL_checklstring(L, 1, &len);
1044ab4902eSlneto lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
1054ab4902eSlneto lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
106f0dad708Snikita int lax = lua_toboolean(L, 4);
1074ab4902eSlneto luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
108f0dad708Snikita "initial position out of bounds");
1094ab4902eSlneto luaL_argcheck(L, --posj < (lua_Integer)len, 3,
110f0dad708Snikita "final position out of bounds");
1114ab4902eSlneto while (posi <= posj) {
112f0dad708Snikita const char *s1 = utf8_decode(s + posi, NULL, !lax);
1134ab4902eSlneto if (s1 == NULL) { /* conversion error? */
114f0dad708Snikita luaL_pushfail(L); /* return fail ... */
1154ab4902eSlneto lua_pushinteger(L, posi + 1); /* ... and current position */
1164ab4902eSlneto return 2;
1174ab4902eSlneto }
1184ab4902eSlneto posi = s1 - s;
1194ab4902eSlneto n++;
1204ab4902eSlneto }
1214ab4902eSlneto lua_pushinteger(L, n);
1224ab4902eSlneto return 1;
1234ab4902eSlneto }
1244ab4902eSlneto
1254ab4902eSlneto
1264ab4902eSlneto /*
127f0dad708Snikita ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
128f0dad708Snikita ** characters that start in the range [i,j]
1294ab4902eSlneto */
codepoint(lua_State * L)1304ab4902eSlneto static int codepoint (lua_State *L) {
1314ab4902eSlneto size_t len;
1324ab4902eSlneto const char *s = luaL_checklstring(L, 1, &len);
1334ab4902eSlneto lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
1344ab4902eSlneto lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
135f0dad708Snikita int lax = lua_toboolean(L, 4);
1364ab4902eSlneto int n;
1374ab4902eSlneto const char *se;
138f0dad708Snikita luaL_argcheck(L, posi >= 1, 2, "out of bounds");
139f0dad708Snikita luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
1404ab4902eSlneto if (posi > pose) return 0; /* empty interval; return no values */
141bee09862Smbalmer if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
1424ab4902eSlneto return luaL_error(L, "string slice too long");
143f0dad708Snikita n = (int)(pose - posi) + 1; /* upper bound for number of returns */
1444ab4902eSlneto luaL_checkstack(L, n, "string slice too long");
145f0dad708Snikita n = 0; /* count the number of returns */
146f0dad708Snikita se = s + pose; /* string end */
1474ab4902eSlneto for (s += posi - 1; s < se;) {
148f0dad708Snikita utfint code;
149f0dad708Snikita s = utf8_decode(s, &code, !lax);
1504ab4902eSlneto if (s == NULL)
151d5cb1924Snikita return luaL_error(L, MSGInvalid);
1524ab4902eSlneto lua_pushinteger(L, code);
1534ab4902eSlneto n++;
1544ab4902eSlneto }
1554ab4902eSlneto return n;
1564ab4902eSlneto }
1574ab4902eSlneto
1584ab4902eSlneto
pushutfchar(lua_State * L,int arg)1594ab4902eSlneto static void pushutfchar (lua_State *L, int arg) {
160f0dad708Snikita lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
161f0dad708Snikita luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
16228a92413Slneto lua_pushfstring(L, "%U", (long)code);
1634ab4902eSlneto }
1644ab4902eSlneto
1654ab4902eSlneto
1664ab4902eSlneto /*
1674ab4902eSlneto ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
1684ab4902eSlneto */
utfchar(lua_State * L)1694ab4902eSlneto static int utfchar (lua_State *L) {
1704ab4902eSlneto int n = lua_gettop(L); /* number of arguments */
1714ab4902eSlneto if (n == 1) /* optimize common case of single char */
1724ab4902eSlneto pushutfchar(L, 1);
1734ab4902eSlneto else {
1744ab4902eSlneto int i;
1754ab4902eSlneto luaL_Buffer b;
1764ab4902eSlneto luaL_buffinit(L, &b);
1774ab4902eSlneto for (i = 1; i <= n; i++) {
1784ab4902eSlneto pushutfchar(L, i);
1794ab4902eSlneto luaL_addvalue(&b);
1804ab4902eSlneto }
1814ab4902eSlneto luaL_pushresult(&b);
1824ab4902eSlneto }
1834ab4902eSlneto return 1;
1844ab4902eSlneto }
1854ab4902eSlneto
1864ab4902eSlneto
1874ab4902eSlneto /*
1884ab4902eSlneto ** offset(s, n, [i]) -> index where n-th character counting from
1894ab4902eSlneto ** position 'i' starts; 0 means character at 'i'.
1904ab4902eSlneto */
byteoffset(lua_State * L)1914ab4902eSlneto static int byteoffset (lua_State *L) {
1924ab4902eSlneto size_t len;
1934ab4902eSlneto const char *s = luaL_checklstring(L, 1, &len);
19428a92413Slneto lua_Integer n = luaL_checkinteger(L, 2);
1954ab4902eSlneto lua_Integer posi = (n >= 0) ? 1 : len + 1;
1964ab4902eSlneto posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
1974ab4902eSlneto luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
198f0dad708Snikita "position out of bounds");
1994ab4902eSlneto if (n == 0) {
2004ab4902eSlneto /* find beginning of current byte sequence */
201d5cb1924Snikita while (posi > 0 && iscontp(s + posi)) posi--;
2024ab4902eSlneto }
2034ab4902eSlneto else {
204d5cb1924Snikita if (iscontp(s + posi))
2052bf4ff61Salnsn return luaL_error(L, "initial position is a continuation byte");
2064ab4902eSlneto if (n < 0) {
2074ab4902eSlneto while (n < 0 && posi > 0) { /* move back */
2084ab4902eSlneto do { /* find beginning of previous character */
2094ab4902eSlneto posi--;
210d5cb1924Snikita } while (posi > 0 && iscontp(s + posi));
2114ab4902eSlneto n++;
2124ab4902eSlneto }
2134ab4902eSlneto }
2144ab4902eSlneto else {
2154ab4902eSlneto n--; /* do not move for 1st character */
2164ab4902eSlneto while (n > 0 && posi < (lua_Integer)len) {
2174ab4902eSlneto do { /* find beginning of next character */
2184ab4902eSlneto posi++;
219d5cb1924Snikita } while (iscontp(s + posi)); /* (cannot pass final '\0') */
2204ab4902eSlneto n--;
2214ab4902eSlneto }
2224ab4902eSlneto }
2234ab4902eSlneto }
2244ab4902eSlneto if (n == 0) /* did it find given character? */
2254ab4902eSlneto lua_pushinteger(L, posi + 1);
2264ab4902eSlneto else /* no such character */
227f0dad708Snikita luaL_pushfail(L);
2284ab4902eSlneto return 1;
2294ab4902eSlneto }
2304ab4902eSlneto
2314ab4902eSlneto
iter_aux(lua_State * L,int strict)232f0dad708Snikita static int iter_aux (lua_State *L, int strict) {
2334ab4902eSlneto size_t len;
2344ab4902eSlneto const char *s = luaL_checklstring(L, 1, &len);
235f0dad708Snikita lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
236f0dad708Snikita if (n < len) {
237*bdda0531Snikita while (iscontp(s + n)) n++; /* go to next character */
2384ab4902eSlneto }
239f0dad708Snikita if (n >= len) /* (also handles original 'n' being negative) */
2404ab4902eSlneto return 0; /* no more codepoints */
2414ab4902eSlneto else {
242f0dad708Snikita utfint code;
243f0dad708Snikita const char *next = utf8_decode(s + n, &code, strict);
244d5cb1924Snikita if (next == NULL || iscontp(next))
245d5cb1924Snikita return luaL_error(L, MSGInvalid);
2464ab4902eSlneto lua_pushinteger(L, n + 1);
2474ab4902eSlneto lua_pushinteger(L, code);
2484ab4902eSlneto return 2;
2494ab4902eSlneto }
2504ab4902eSlneto }
2514ab4902eSlneto
2524ab4902eSlneto
iter_auxstrict(lua_State * L)253f0dad708Snikita static int iter_auxstrict (lua_State *L) {
254f0dad708Snikita return iter_aux(L, 1);
255f0dad708Snikita }
256f0dad708Snikita
iter_auxlax(lua_State * L)257f0dad708Snikita static int iter_auxlax (lua_State *L) {
258f0dad708Snikita return iter_aux(L, 0);
259f0dad708Snikita }
260f0dad708Snikita
261f0dad708Snikita
iter_codes(lua_State * L)2624ab4902eSlneto static int iter_codes (lua_State *L) {
263f0dad708Snikita int lax = lua_toboolean(L, 2);
264d5cb1924Snikita const char *s = luaL_checkstring(L, 1);
265d5cb1924Snikita luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
266f0dad708Snikita lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
2674ab4902eSlneto lua_pushvalue(L, 1);
2684ab4902eSlneto lua_pushinteger(L, 0);
2694ab4902eSlneto return 3;
2704ab4902eSlneto }
2714ab4902eSlneto
2724ab4902eSlneto
2734ab4902eSlneto /* pattern to match a single UTF-8 character */
274f0dad708Snikita #define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
2754ab4902eSlneto
2764ab4902eSlneto
277bee09862Smbalmer static const luaL_Reg funcs[] = {
2784ab4902eSlneto {"offset", byteoffset},
2794ab4902eSlneto {"codepoint", codepoint},
2804ab4902eSlneto {"char", utfchar},
2814ab4902eSlneto {"len", utflen},
2824ab4902eSlneto {"codes", iter_codes},
28328a92413Slneto /* placeholders */
28428a92413Slneto {"charpattern", NULL},
2854ab4902eSlneto {NULL, NULL}
2864ab4902eSlneto };
2874ab4902eSlneto
2884ab4902eSlneto
luaopen_utf8(lua_State * L)2894ab4902eSlneto LUAMOD_API int luaopen_utf8 (lua_State *L) {
2904ab4902eSlneto luaL_newlib(L, funcs);
291bee09862Smbalmer lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
29228a92413Slneto lua_setfield(L, -2, "charpattern");
2934ab4902eSlneto return 1;
2944ab4902eSlneto }
2954ab4902eSlneto
296