xref: /aosp_15_r20/external/lua/src/lutf8lib.c (revision 088332b5b69e7ab13924864b272aabfc2509d2d5)
1*088332b5SXin Li /*
2*088332b5SXin Li ** $Id: lutf8lib.c $
3*088332b5SXin Li ** Standard library for UTF-8 manipulation
4*088332b5SXin Li ** See Copyright Notice in lua.h
5*088332b5SXin Li */
6*088332b5SXin Li 
7*088332b5SXin Li #define lutf8lib_c
8*088332b5SXin Li #define LUA_LIB
9*088332b5SXin Li 
10*088332b5SXin Li #include "lprefix.h"
11*088332b5SXin Li 
12*088332b5SXin Li 
13*088332b5SXin Li #include <assert.h>
14*088332b5SXin Li #include <limits.h>
15*088332b5SXin Li #include <stdlib.h>
16*088332b5SXin Li #include <string.h>
17*088332b5SXin Li 
18*088332b5SXin Li #include "lua.h"
19*088332b5SXin Li 
20*088332b5SXin Li #include "lauxlib.h"
21*088332b5SXin Li #include "lualib.h"
22*088332b5SXin Li 
23*088332b5SXin Li 
24*088332b5SXin Li #define MAXUNICODE	0x10FFFFu
25*088332b5SXin Li 
26*088332b5SXin Li #define MAXUTF		0x7FFFFFFFu
27*088332b5SXin Li 
28*088332b5SXin Li /*
29*088332b5SXin Li ** Integer type for decoded UTF-8 values; MAXUTF needs 31 bits.
30*088332b5SXin Li */
31*088332b5SXin Li #if (UINT_MAX >> 30) >= 1
32*088332b5SXin Li typedef unsigned int utfint;
33*088332b5SXin Li #else
34*088332b5SXin Li typedef unsigned long utfint;
35*088332b5SXin Li #endif
36*088332b5SXin Li 
37*088332b5SXin Li 
38*088332b5SXin Li #define iscont(p)	((*(p) & 0xC0) == 0x80)
39*088332b5SXin Li 
40*088332b5SXin Li 
41*088332b5SXin Li /* from strlib */
42*088332b5SXin Li /* translate a relative string position: negative means back from end */
u_posrelat(lua_Integer pos,size_t len)43*088332b5SXin Li static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
44*088332b5SXin Li   if (pos >= 0) return pos;
45*088332b5SXin Li   else if (0u - (size_t)pos > len) return 0;
46*088332b5SXin Li   else return (lua_Integer)len + pos + 1;
47*088332b5SXin Li }
48*088332b5SXin Li 
49*088332b5SXin Li 
50*088332b5SXin Li /*
51*088332b5SXin Li ** Decode one UTF-8 sequence, returning NULL if byte sequence is
52*088332b5SXin Li ** invalid.  The array 'limits' stores the minimum value for each
53*088332b5SXin Li ** sequence length, to check for overlong representations. Its first
54*088332b5SXin Li ** entry forces an error for non-ascii bytes with no continuation
55*088332b5SXin Li ** bytes (count == 0).
56*088332b5SXin Li */
utf8_decode(const char * s,utfint * val,int strict)57*088332b5SXin Li static const char *utf8_decode (const char *s, utfint *val, int strict) {
58*088332b5SXin Li   static const utfint limits[] =
59*088332b5SXin Li         {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
60*088332b5SXin Li   unsigned int c = (unsigned char)s[0];
61*088332b5SXin Li   utfint res = 0;  /* final result */
62*088332b5SXin Li   if (c < 0x80)  /* ascii? */
63*088332b5SXin Li     res = c;
64*088332b5SXin Li   else {
65*088332b5SXin Li     int count = 0;  /* to count number of continuation bytes */
66*088332b5SXin Li     for (; c & 0x40; c <<= 1) {  /* while it needs continuation bytes... */
67*088332b5SXin Li       unsigned int cc = (unsigned char)s[++count];  /* read next byte */
68*088332b5SXin Li       if ((cc & 0xC0) != 0x80)  /* not a continuation byte? */
69*088332b5SXin Li         return NULL;  /* invalid byte sequence */
70*088332b5SXin Li       res = (res << 6) | (cc & 0x3F);  /* add lower 6 bits from cont. byte */
71*088332b5SXin Li     }
72*088332b5SXin Li     res |= ((utfint)(c & 0x7F) << (count * 5));  /* add first byte */
73*088332b5SXin Li     if (count > 5 || res > MAXUTF || res < limits[count])
74*088332b5SXin Li       return NULL;  /* invalid byte sequence */
75*088332b5SXin Li     s += count;  /* skip continuation bytes read */
76*088332b5SXin Li   }
77*088332b5SXin Li   if (strict) {
78*088332b5SXin Li     /* check for invalid code points; too large or surrogates */
79*088332b5SXin Li     if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
80*088332b5SXin Li       return NULL;
81*088332b5SXin Li   }
82*088332b5SXin Li   if (val) *val = res;
83*088332b5SXin Li   return s + 1;  /* +1 to include first byte */
84*088332b5SXin Li }
85*088332b5SXin Li 
86*088332b5SXin Li 
87*088332b5SXin Li /*
88*088332b5SXin Li ** utf8len(s [, i [, j [, lax]]]) --> number of characters that
89*088332b5SXin Li ** start in the range [i,j], or nil + current position if 's' is not
90*088332b5SXin Li ** well formed in that interval
91*088332b5SXin Li */
utflen(lua_State * L)92*088332b5SXin Li static int utflen (lua_State *L) {
93*088332b5SXin Li   lua_Integer n = 0;  /* counter for the number of characters */
94*088332b5SXin Li   size_t len;  /* string length in bytes */
95*088332b5SXin Li   const char *s = luaL_checklstring(L, 1, &len);
96*088332b5SXin Li   lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
97*088332b5SXin Li   lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
98*088332b5SXin Li   int lax = lua_toboolean(L, 4);
99*088332b5SXin Li   luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
100*088332b5SXin Li                    "initial position out of bounds");
101*088332b5SXin Li   luaL_argcheck(L, --posj < (lua_Integer)len, 3,
102*088332b5SXin Li                    "final position out of bounds");
103*088332b5SXin Li   while (posi <= posj) {
104*088332b5SXin Li     const char *s1 = utf8_decode(s + posi, NULL, !lax);
105*088332b5SXin Li     if (s1 == NULL) {  /* conversion error? */
106*088332b5SXin Li       luaL_pushfail(L);  /* return fail ... */
107*088332b5SXin Li       lua_pushinteger(L, posi + 1);  /* ... and current position */
108*088332b5SXin Li       return 2;
109*088332b5SXin Li     }
110*088332b5SXin Li     posi = s1 - s;
111*088332b5SXin Li     n++;
112*088332b5SXin Li   }
113*088332b5SXin Li   lua_pushinteger(L, n);
114*088332b5SXin Li   return 1;
115*088332b5SXin Li }
116*088332b5SXin Li 
117*088332b5SXin Li 
118*088332b5SXin Li /*
119*088332b5SXin Li ** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
120*088332b5SXin Li ** characters that start in the range [i,j]
121*088332b5SXin Li */
codepoint(lua_State * L)122*088332b5SXin Li static int codepoint (lua_State *L) {
123*088332b5SXin Li   size_t len;
124*088332b5SXin Li   const char *s = luaL_checklstring(L, 1, &len);
125*088332b5SXin Li   lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
126*088332b5SXin Li   lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
127*088332b5SXin Li   int lax = lua_toboolean(L, 4);
128*088332b5SXin Li   int n;
129*088332b5SXin Li   const char *se;
130*088332b5SXin Li   luaL_argcheck(L, posi >= 1, 2, "out of bounds");
131*088332b5SXin Li   luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
132*088332b5SXin Li   if (posi > pose) return 0;  /* empty interval; return no values */
133*088332b5SXin Li   if (pose - posi >= INT_MAX)  /* (lua_Integer -> int) overflow? */
134*088332b5SXin Li     return luaL_error(L, "string slice too long");
135*088332b5SXin Li   n = (int)(pose -  posi) + 1;  /* upper bound for number of returns */
136*088332b5SXin Li   luaL_checkstack(L, n, "string slice too long");
137*088332b5SXin Li   n = 0;  /* count the number of returns */
138*088332b5SXin Li   se = s + pose;  /* string end */
139*088332b5SXin Li   for (s += posi - 1; s < se;) {
140*088332b5SXin Li     utfint code;
141*088332b5SXin Li     s = utf8_decode(s, &code, !lax);
142*088332b5SXin Li     if (s == NULL)
143*088332b5SXin Li       return luaL_error(L, "invalid UTF-8 code");
144*088332b5SXin Li     lua_pushinteger(L, code);
145*088332b5SXin Li     n++;
146*088332b5SXin Li   }
147*088332b5SXin Li   return n;
148*088332b5SXin Li }
149*088332b5SXin Li 
150*088332b5SXin Li 
pushutfchar(lua_State * L,int arg)151*088332b5SXin Li static void pushutfchar (lua_State *L, int arg) {
152*088332b5SXin Li   lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
153*088332b5SXin Li   luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
154*088332b5SXin Li   lua_pushfstring(L, "%U", (long)code);
155*088332b5SXin Li }
156*088332b5SXin Li 
157*088332b5SXin Li 
158*088332b5SXin Li /*
159*088332b5SXin Li ** utfchar(n1, n2, ...)  -> char(n1)..char(n2)...
160*088332b5SXin Li */
utfchar(lua_State * L)161*088332b5SXin Li static int utfchar (lua_State *L) {
162*088332b5SXin Li   int n = lua_gettop(L);  /* number of arguments */
163*088332b5SXin Li   if (n == 1)  /* optimize common case of single char */
164*088332b5SXin Li     pushutfchar(L, 1);
165*088332b5SXin Li   else {
166*088332b5SXin Li     int i;
167*088332b5SXin Li     luaL_Buffer b;
168*088332b5SXin Li     luaL_buffinit(L, &b);
169*088332b5SXin Li     for (i = 1; i <= n; i++) {
170*088332b5SXin Li       pushutfchar(L, i);
171*088332b5SXin Li       luaL_addvalue(&b);
172*088332b5SXin Li     }
173*088332b5SXin Li     luaL_pushresult(&b);
174*088332b5SXin Li   }
175*088332b5SXin Li   return 1;
176*088332b5SXin Li }
177*088332b5SXin Li 
178*088332b5SXin Li 
179*088332b5SXin Li /*
180*088332b5SXin Li ** offset(s, n, [i])  -> index where n-th character counting from
181*088332b5SXin Li **   position 'i' starts; 0 means character at 'i'.
182*088332b5SXin Li */
byteoffset(lua_State * L)183*088332b5SXin Li static int byteoffset (lua_State *L) {
184*088332b5SXin Li   size_t len;
185*088332b5SXin Li   const char *s = luaL_checklstring(L, 1, &len);
186*088332b5SXin Li   lua_Integer n  = luaL_checkinteger(L, 2);
187*088332b5SXin Li   lua_Integer posi = (n >= 0) ? 1 : len + 1;
188*088332b5SXin Li   posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
189*088332b5SXin Li   luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
190*088332b5SXin Li                    "position out of bounds");
191*088332b5SXin Li   if (n == 0) {
192*088332b5SXin Li     /* find beginning of current byte sequence */
193*088332b5SXin Li     while (posi > 0 && iscont(s + posi)) posi--;
194*088332b5SXin Li   }
195*088332b5SXin Li   else {
196*088332b5SXin Li     if (iscont(s + posi))
197*088332b5SXin Li       return luaL_error(L, "initial position is a continuation byte");
198*088332b5SXin Li     if (n < 0) {
199*088332b5SXin Li        while (n < 0 && posi > 0) {  /* move back */
200*088332b5SXin Li          do {  /* find beginning of previous character */
201*088332b5SXin Li            posi--;
202*088332b5SXin Li          } while (posi > 0 && iscont(s + posi));
203*088332b5SXin Li          n++;
204*088332b5SXin Li        }
205*088332b5SXin Li      }
206*088332b5SXin Li      else {
207*088332b5SXin Li        n--;  /* do not move for 1st character */
208*088332b5SXin Li        while (n > 0 && posi < (lua_Integer)len) {
209*088332b5SXin Li          do {  /* find beginning of next character */
210*088332b5SXin Li            posi++;
211*088332b5SXin Li          } while (iscont(s + posi));  /* (cannot pass final '\0') */
212*088332b5SXin Li          n--;
213*088332b5SXin Li        }
214*088332b5SXin Li      }
215*088332b5SXin Li   }
216*088332b5SXin Li   if (n == 0)  /* did it find given character? */
217*088332b5SXin Li     lua_pushinteger(L, posi + 1);
218*088332b5SXin Li   else  /* no such character */
219*088332b5SXin Li     luaL_pushfail(L);
220*088332b5SXin Li   return 1;
221*088332b5SXin Li }
222*088332b5SXin Li 
223*088332b5SXin Li 
iter_aux(lua_State * L,int strict)224*088332b5SXin Li static int iter_aux (lua_State *L, int strict) {
225*088332b5SXin Li   size_t len;
226*088332b5SXin Li   const char *s = luaL_checklstring(L, 1, &len);
227*088332b5SXin Li   lua_Integer n = lua_tointeger(L, 2) - 1;
228*088332b5SXin Li   if (n < 0)  /* first iteration? */
229*088332b5SXin Li     n = 0;  /* start from here */
230*088332b5SXin Li   else if (n < (lua_Integer)len) {
231*088332b5SXin Li     n++;  /* skip current byte */
232*088332b5SXin Li     while (iscont(s + n)) n++;  /* and its continuations */
233*088332b5SXin Li   }
234*088332b5SXin Li   if (n >= (lua_Integer)len)
235*088332b5SXin Li     return 0;  /* no more codepoints */
236*088332b5SXin Li   else {
237*088332b5SXin Li     utfint code;
238*088332b5SXin Li     const char *next = utf8_decode(s + n, &code, strict);
239*088332b5SXin Li     if (next == NULL)
240*088332b5SXin Li       return luaL_error(L, "invalid UTF-8 code");
241*088332b5SXin Li     lua_pushinteger(L, n + 1);
242*088332b5SXin Li     lua_pushinteger(L, code);
243*088332b5SXin Li     return 2;
244*088332b5SXin Li   }
245*088332b5SXin Li }
246*088332b5SXin Li 
247*088332b5SXin Li 
iter_auxstrict(lua_State * L)248*088332b5SXin Li static int iter_auxstrict (lua_State *L) {
249*088332b5SXin Li   return iter_aux(L, 1);
250*088332b5SXin Li }
251*088332b5SXin Li 
iter_auxlax(lua_State * L)252*088332b5SXin Li static int iter_auxlax (lua_State *L) {
253*088332b5SXin Li   return iter_aux(L, 0);
254*088332b5SXin Li }
255*088332b5SXin Li 
256*088332b5SXin Li 
iter_codes(lua_State * L)257*088332b5SXin Li static int iter_codes (lua_State *L) {
258*088332b5SXin Li   int lax = lua_toboolean(L, 2);
259*088332b5SXin Li   luaL_checkstring(L, 1);
260*088332b5SXin Li   lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
261*088332b5SXin Li   lua_pushvalue(L, 1);
262*088332b5SXin Li   lua_pushinteger(L, 0);
263*088332b5SXin Li   return 3;
264*088332b5SXin Li }
265*088332b5SXin Li 
266*088332b5SXin Li 
267*088332b5SXin Li /* pattern to match a single UTF-8 character */
268*088332b5SXin Li #define UTF8PATT	"[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
269*088332b5SXin Li 
270*088332b5SXin Li 
271*088332b5SXin Li static const luaL_Reg funcs[] = {
272*088332b5SXin Li   {"offset", byteoffset},
273*088332b5SXin Li   {"codepoint", codepoint},
274*088332b5SXin Li   {"char", utfchar},
275*088332b5SXin Li   {"len", utflen},
276*088332b5SXin Li   {"codes", iter_codes},
277*088332b5SXin Li   /* placeholders */
278*088332b5SXin Li   {"charpattern", NULL},
279*088332b5SXin Li   {NULL, NULL}
280*088332b5SXin Li };
281*088332b5SXin Li 
282*088332b5SXin Li 
luaopen_utf8(lua_State * L)283*088332b5SXin Li LUAMOD_API int luaopen_utf8 (lua_State *L) {
284*088332b5SXin Li   luaL_newlib(L, funcs);
285*088332b5SXin Li   lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
286*088332b5SXin Li   lua_setfield(L, -2, "charpattern");
287*088332b5SXin Li   return 1;
288*088332b5SXin Li }
289*088332b5SXin Li 
290