18e3e3a7aSWarner Losh /*
20495ed39SKyle Evans ** $Id: lutf8lib.c $
38e3e3a7aSWarner Losh ** Standard library for UTF-8 manipulation
48e3e3a7aSWarner Losh ** See Copyright Notice in lua.h
58e3e3a7aSWarner Losh */
68e3e3a7aSWarner Losh
78e3e3a7aSWarner Losh #define lutf8lib_c
88e3e3a7aSWarner Losh #define LUA_LIB
98e3e3a7aSWarner Losh
108e3e3a7aSWarner Losh #include "lprefix.h"
118e3e3a7aSWarner Losh
128e3e3a7aSWarner Losh
138e3e3a7aSWarner Losh #include <assert.h>
148e3e3a7aSWarner Losh #include <limits.h>
158e3e3a7aSWarner Losh #include <stdlib.h>
168e3e3a7aSWarner Losh #include <string.h>
178e3e3a7aSWarner Losh
188e3e3a7aSWarner Losh #include "lua.h"
198e3e3a7aSWarner Losh
208e3e3a7aSWarner Losh #include "lauxlib.h"
218e3e3a7aSWarner Losh #include "lualib.h"
228e3e3a7aSWarner Losh
230495ed39SKyle Evans
240495ed39SKyle Evans #define MAXUNICODE 0x10FFFFu
250495ed39SKyle Evans
260495ed39SKyle Evans #define MAXUTF 0x7FFFFFFFu
270495ed39SKyle Evans
28*a9490b81SWarner Losh
29*a9490b81SWarner Losh #define MSGInvalid "invalid UTF-8 code"
30*a9490b81SWarner Losh
310495ed39SKyle Evans /*
320495ed39SKyle Evans ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
330495ed39SKyle Evans */
340495ed39SKyle Evans #if (UINT_MAX >> 30) >= 1
350495ed39SKyle Evans typedef unsigned int utfint;
360495ed39SKyle Evans #else
370495ed39SKyle Evans typedef unsigned long utfint;
380495ed39SKyle Evans #endif
390495ed39SKyle Evans
408e3e3a7aSWarner Losh
41*a9490b81SWarner Losh #define iscont(c) (((c) & 0xC0) == 0x80)
42*a9490b81SWarner Losh #define iscontp(p) iscont(*(p))
438e3e3a7aSWarner Losh
448e3e3a7aSWarner Losh
458e3e3a7aSWarner Losh /* from strlib */
468e3e3a7aSWarner Losh /* translate a relative string position: negative means back from end */
u_posrelat(lua_Integer pos,size_t len)478e3e3a7aSWarner Losh static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
488e3e3a7aSWarner Losh if (pos >= 0) return pos;
498e3e3a7aSWarner Losh else if (0u - (size_t)pos > len) return 0;
508e3e3a7aSWarner Losh else return (lua_Integer)len + pos + 1;
518e3e3a7aSWarner Losh }
528e3e3a7aSWarner Losh
538e3e3a7aSWarner Losh
548e3e3a7aSWarner Losh /*
550495ed39SKyle Evans ** Decode one UTF-8 sequence, returning NULL if byte sequence is
560495ed39SKyle Evans ** invalid. The array 'limits' stores the minimum value for each
570495ed39SKyle Evans ** sequence length, to check for overlong representations. Its first
580495ed39SKyle Evans ** entry forces an error for non-ascii bytes with no continuation
590495ed39SKyle Evans ** bytes (count == 0).
608e3e3a7aSWarner Losh */
utf8_decode(const char * s,utfint * val,int strict)610495ed39SKyle Evans static const char *utf8_decode (const char *s, utfint *val, int strict) {
620495ed39SKyle Evans static const utfint limits[] =
630495ed39SKyle Evans {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
640495ed39SKyle Evans unsigned int c = (unsigned char)s[0];
650495ed39SKyle Evans utfint res = 0; /* final result */
668e3e3a7aSWarner Losh if (c < 0x80) /* ascii? */
678e3e3a7aSWarner Losh res = c;
688e3e3a7aSWarner Losh else {
698e3e3a7aSWarner Losh int count = 0; /* to count number of continuation bytes */
700495ed39SKyle Evans for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
710495ed39SKyle Evans unsigned int cc = (unsigned char)s[++count]; /* read next byte */
72*a9490b81SWarner Losh if (!iscont(cc)) /* not a continuation byte? */
738e3e3a7aSWarner Losh return NULL; /* invalid byte sequence */
748e3e3a7aSWarner Losh res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
758e3e3a7aSWarner Losh }
760495ed39SKyle Evans res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
770495ed39SKyle Evans if (count > 5 || res > MAXUTF || res < limits[count])
788e3e3a7aSWarner Losh return NULL; /* invalid byte sequence */
798e3e3a7aSWarner Losh s += count; /* skip continuation bytes read */
808e3e3a7aSWarner Losh }
810495ed39SKyle Evans if (strict) {
820495ed39SKyle Evans /* check for invalid code points; too large or surrogates */
830495ed39SKyle Evans if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
840495ed39SKyle Evans return NULL;
850495ed39SKyle Evans }
868e3e3a7aSWarner Losh if (val) *val = res;
870495ed39SKyle Evans return s + 1; /* +1 to include first byte */
888e3e3a7aSWarner Losh }
898e3e3a7aSWarner Losh
908e3e3a7aSWarner Losh
918e3e3a7aSWarner Losh /*
920495ed39SKyle Evans ** utf8len(s [, i [, j [, lax]]]) --> number of characters that
930495ed39SKyle Evans ** start in the range [i,j], or nil + current position if 's' is not
940495ed39SKyle Evans ** well formed in that interval
958e3e3a7aSWarner Losh */
utflen(lua_State * L)968e3e3a7aSWarner Losh static int utflen (lua_State *L) {
970495ed39SKyle Evans lua_Integer n = 0; /* counter for the number of characters */
980495ed39SKyle Evans size_t len; /* string length in bytes */
998e3e3a7aSWarner Losh const char *s = luaL_checklstring(L, 1, &len);
1008e3e3a7aSWarner Losh lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
1018e3e3a7aSWarner Losh lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
1020495ed39SKyle Evans int lax = lua_toboolean(L, 4);
1038e3e3a7aSWarner Losh luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
1040495ed39SKyle Evans "initial position out of bounds");
1058e3e3a7aSWarner Losh luaL_argcheck(L, --posj < (lua_Integer)len, 3,
1060495ed39SKyle Evans "final position out of bounds");
1078e3e3a7aSWarner Losh while (posi <= posj) {
1080495ed39SKyle Evans const char *s1 = utf8_decode(s + posi, NULL, !lax);
1098e3e3a7aSWarner Losh if (s1 == NULL) { /* conversion error? */
1100495ed39SKyle Evans luaL_pushfail(L); /* return fail ... */
1118e3e3a7aSWarner Losh lua_pushinteger(L, posi + 1); /* ... and current position */
1128e3e3a7aSWarner Losh return 2;
1138e3e3a7aSWarner Losh }
1148e3e3a7aSWarner Losh posi = s1 - s;
1158e3e3a7aSWarner Losh n++;
1168e3e3a7aSWarner Losh }
1178e3e3a7aSWarner Losh lua_pushinteger(L, n);
1188e3e3a7aSWarner Losh return 1;
1198e3e3a7aSWarner Losh }
1208e3e3a7aSWarner Losh
1218e3e3a7aSWarner Losh
1228e3e3a7aSWarner Losh /*
1230495ed39SKyle Evans ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
1240495ed39SKyle Evans ** characters that start in the range [i,j]
1258e3e3a7aSWarner Losh */
codepoint(lua_State * L)1268e3e3a7aSWarner Losh static int codepoint (lua_State *L) {
1278e3e3a7aSWarner Losh size_t len;
1288e3e3a7aSWarner Losh const char *s = luaL_checklstring(L, 1, &len);
1298e3e3a7aSWarner Losh lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
1308e3e3a7aSWarner Losh lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
1310495ed39SKyle Evans int lax = lua_toboolean(L, 4);
1328e3e3a7aSWarner Losh int n;
1338e3e3a7aSWarner Losh const char *se;
1340495ed39SKyle Evans luaL_argcheck(L, posi >= 1, 2, "out of bounds");
1350495ed39SKyle Evans luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
1368e3e3a7aSWarner Losh if (posi > pose) return 0; /* empty interval; return no values */
1378e3e3a7aSWarner Losh if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
1388e3e3a7aSWarner Losh return luaL_error(L, "string slice too long");
1390495ed39SKyle Evans n = (int)(pose - posi) + 1; /* upper bound for number of returns */
1408e3e3a7aSWarner Losh luaL_checkstack(L, n, "string slice too long");
1410495ed39SKyle Evans n = 0; /* count the number of returns */
1420495ed39SKyle Evans se = s + pose; /* string end */
1438e3e3a7aSWarner Losh for (s += posi - 1; s < se;) {
1440495ed39SKyle Evans utfint code;
1450495ed39SKyle Evans s = utf8_decode(s, &code, !lax);
1468e3e3a7aSWarner Losh if (s == NULL)
147*a9490b81SWarner Losh return luaL_error(L, MSGInvalid);
1488e3e3a7aSWarner Losh lua_pushinteger(L, code);
1498e3e3a7aSWarner Losh n++;
1508e3e3a7aSWarner Losh }
1518e3e3a7aSWarner Losh return n;
1528e3e3a7aSWarner Losh }
1538e3e3a7aSWarner Losh
1548e3e3a7aSWarner Losh
pushutfchar(lua_State * L,int arg)1558e3e3a7aSWarner Losh static void pushutfchar (lua_State *L, int arg) {
1560495ed39SKyle Evans lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
1570495ed39SKyle Evans luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
1588e3e3a7aSWarner Losh lua_pushfstring(L, "%U", (long)code);
1598e3e3a7aSWarner Losh }
1608e3e3a7aSWarner Losh
1618e3e3a7aSWarner Losh
1628e3e3a7aSWarner Losh /*
1638e3e3a7aSWarner Losh ** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
1648e3e3a7aSWarner Losh */
utfchar(lua_State * L)1658e3e3a7aSWarner Losh static int utfchar (lua_State *L) {
1668e3e3a7aSWarner Losh int n = lua_gettop(L); /* number of arguments */
1678e3e3a7aSWarner Losh if (n == 1) /* optimize common case of single char */
1688e3e3a7aSWarner Losh pushutfchar(L, 1);
1698e3e3a7aSWarner Losh else {
1708e3e3a7aSWarner Losh int i;
1718e3e3a7aSWarner Losh luaL_Buffer b;
1728e3e3a7aSWarner Losh luaL_buffinit(L, &b);
1738e3e3a7aSWarner Losh for (i = 1; i <= n; i++) {
1748e3e3a7aSWarner Losh pushutfchar(L, i);
1758e3e3a7aSWarner Losh luaL_addvalue(&b);
1768e3e3a7aSWarner Losh }
1778e3e3a7aSWarner Losh luaL_pushresult(&b);
1788e3e3a7aSWarner Losh }
1798e3e3a7aSWarner Losh return 1;
1808e3e3a7aSWarner Losh }
1818e3e3a7aSWarner Losh
1828e3e3a7aSWarner Losh
1838e3e3a7aSWarner Losh /*
1848e3e3a7aSWarner Losh ** offset(s, n, [i]) -> index where n-th character counting from
1858e3e3a7aSWarner Losh ** position 'i' starts; 0 means character at 'i'.
1868e3e3a7aSWarner Losh */
byteoffset(lua_State * L)1878e3e3a7aSWarner Losh static int byteoffset (lua_State *L) {
1888e3e3a7aSWarner Losh size_t len;
1898e3e3a7aSWarner Losh const char *s = luaL_checklstring(L, 1, &len);
1908e3e3a7aSWarner Losh lua_Integer n = luaL_checkinteger(L, 2);
1918e3e3a7aSWarner Losh lua_Integer posi = (n >= 0) ? 1 : len + 1;
1928e3e3a7aSWarner Losh posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
1938e3e3a7aSWarner Losh luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
1940495ed39SKyle Evans "position out of bounds");
1958e3e3a7aSWarner Losh if (n == 0) {
1968e3e3a7aSWarner Losh /* find beginning of current byte sequence */
197*a9490b81SWarner Losh while (posi > 0 && iscontp(s + posi)) posi--;
1988e3e3a7aSWarner Losh }
1998e3e3a7aSWarner Losh else {
200*a9490b81SWarner Losh if (iscontp(s + posi))
201e112e9d2SKyle Evans return luaL_error(L, "initial position is a continuation byte");
2028e3e3a7aSWarner Losh if (n < 0) {
2038e3e3a7aSWarner Losh while (n < 0 && posi > 0) { /* move back */
2048e3e3a7aSWarner Losh do { /* find beginning of previous character */
2058e3e3a7aSWarner Losh posi--;
206*a9490b81SWarner Losh } while (posi > 0 && iscontp(s + posi));
2078e3e3a7aSWarner Losh n++;
2088e3e3a7aSWarner Losh }
2098e3e3a7aSWarner Losh }
2108e3e3a7aSWarner Losh else {
2118e3e3a7aSWarner Losh n--; /* do not move for 1st character */
2128e3e3a7aSWarner Losh while (n > 0 && posi < (lua_Integer)len) {
2138e3e3a7aSWarner Losh do { /* find beginning of next character */
2148e3e3a7aSWarner Losh posi++;
215*a9490b81SWarner Losh } while (iscontp(s + posi)); /* (cannot pass final '\0') */
2168e3e3a7aSWarner Losh n--;
2178e3e3a7aSWarner Losh }
2188e3e3a7aSWarner Losh }
2198e3e3a7aSWarner Losh }
2208e3e3a7aSWarner Losh if (n == 0) /* did it find given character? */
2218e3e3a7aSWarner Losh lua_pushinteger(L, posi + 1);
2228e3e3a7aSWarner Losh else /* no such character */
2230495ed39SKyle Evans luaL_pushfail(L);
2248e3e3a7aSWarner Losh return 1;
2258e3e3a7aSWarner Losh }
2268e3e3a7aSWarner Losh
2278e3e3a7aSWarner Losh
iter_aux(lua_State * L,int strict)2280495ed39SKyle Evans static int iter_aux (lua_State *L, int strict) {
2298e3e3a7aSWarner Losh size_t len;
2308e3e3a7aSWarner Losh const char *s = luaL_checklstring(L, 1, &len);
2318c784bb8SWarner Losh lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
2328c784bb8SWarner Losh if (n < len) {
233*a9490b81SWarner Losh while (iscontp(s + n)) n++; /* go to next character */
2348e3e3a7aSWarner Losh }
2358c784bb8SWarner Losh if (n >= len) /* (also handles original 'n' being negative) */
2368e3e3a7aSWarner Losh return 0; /* no more codepoints */
2378e3e3a7aSWarner Losh else {
2380495ed39SKyle Evans utfint code;
2390495ed39SKyle Evans const char *next = utf8_decode(s + n, &code, strict);
240*a9490b81SWarner Losh if (next == NULL || iscontp(next))
241*a9490b81SWarner Losh return luaL_error(L, MSGInvalid);
2428e3e3a7aSWarner Losh lua_pushinteger(L, n + 1);
2438e3e3a7aSWarner Losh lua_pushinteger(L, code);
2448e3e3a7aSWarner Losh return 2;
2458e3e3a7aSWarner Losh }
2468e3e3a7aSWarner Losh }
2478e3e3a7aSWarner Losh
2488e3e3a7aSWarner Losh
iter_auxstrict(lua_State * L)2490495ed39SKyle Evans static int iter_auxstrict (lua_State *L) {
2500495ed39SKyle Evans return iter_aux(L, 1);
2510495ed39SKyle Evans }
2520495ed39SKyle Evans
iter_auxlax(lua_State * L)2530495ed39SKyle Evans static int iter_auxlax (lua_State *L) {
2540495ed39SKyle Evans return iter_aux(L, 0);
2550495ed39SKyle Evans }
2560495ed39SKyle Evans
2570495ed39SKyle Evans
iter_codes(lua_State * L)2588e3e3a7aSWarner Losh static int iter_codes (lua_State *L) {
2590495ed39SKyle Evans int lax = lua_toboolean(L, 2);
260*a9490b81SWarner Losh const char *s = luaL_checkstring(L, 1);
261*a9490b81SWarner Losh luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
2620495ed39SKyle Evans lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
2638e3e3a7aSWarner Losh lua_pushvalue(L, 1);
2648e3e3a7aSWarner Losh lua_pushinteger(L, 0);
2658e3e3a7aSWarner Losh return 3;
2668e3e3a7aSWarner Losh }
2678e3e3a7aSWarner Losh
2688e3e3a7aSWarner Losh
2698e3e3a7aSWarner Losh /* pattern to match a single UTF-8 character */
2700495ed39SKyle Evans #define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
2718e3e3a7aSWarner Losh
2728e3e3a7aSWarner Losh
2738e3e3a7aSWarner Losh static const luaL_Reg funcs[] = {
2748e3e3a7aSWarner Losh {"offset", byteoffset},
2758e3e3a7aSWarner Losh {"codepoint", codepoint},
2768e3e3a7aSWarner Losh {"char", utfchar},
2778e3e3a7aSWarner Losh {"len", utflen},
2788e3e3a7aSWarner Losh {"codes", iter_codes},
2798e3e3a7aSWarner Losh /* placeholders */
2808e3e3a7aSWarner Losh {"charpattern", NULL},
2818e3e3a7aSWarner Losh {NULL, NULL}
2828e3e3a7aSWarner Losh };
2838e3e3a7aSWarner Losh
2848e3e3a7aSWarner Losh
luaopen_utf8(lua_State * L)2858e3e3a7aSWarner Losh LUAMOD_API int luaopen_utf8 (lua_State *L) {
2868e3e3a7aSWarner Losh luaL_newlib(L, funcs);
2878e3e3a7aSWarner Losh lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
2888e3e3a7aSWarner Losh lua_setfield(L, -2, "charpattern");
2898e3e3a7aSWarner Losh return 1;
2908e3e3a7aSWarner Losh }
2918e3e3a7aSWarner Losh
292