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