24#define MAXUNICODE 0x10FFFFu
26#define MAXUTF 0x7FFFFFFFu
29#define MSGInvalid "invalid UTF-8 code"
34#if (UINT_MAX >> 30) >= 1
35typedef unsigned int utfint;
37typedef unsigned long utfint;
41#define iscont(c) (((c) & 0xC0) == 0x80)
42#define iscontp(p) iscont(*(p))
47static lua_Integer u_posrelat (lua_Integer pos,
size_t len) {
48 if (pos >= 0)
return pos;
49 else if (0u - (
size_t)pos > len)
return 0;
50 else return (lua_Integer)len + pos + 1;
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];
70 for (; c & 0x40; c <<= 1) {
71 unsigned int cc = (
unsigned char)s[++count];
74 res = (res << 6) | (cc & 0x3F);
76 res |= ((utfint)(c & 0x7F) << (count * 5));
77 if (count > 5 || res > MAXUTF || res < limits[count])
83 if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
99 const char *s = luaL_checklstring(L, 1, &len);
100 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
101 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
102 int lax = lua_toboolean(L, 4);
103 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
104 "initial position out of bounds");
105 luaL_argcheck(L, --posj < (lua_Integer)len, 3,
106 "final position out of bounds");
107 while (posi <= posj) {
108 const char *s1 = utf8_decode(s + posi, NULL, !lax);
111 lua_pushinteger(L, posi + 1);
117 lua_pushinteger(L, n);
128 const char *s = luaL_checklstring(L, 1, &len);
129 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
130 lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
131 int lax = lua_toboolean(L, 4);
134 luaL_argcheck(L, posi >= 1, 2,
"out of bounds");
135 luaL_argcheck(L, pose <= (lua_Integer)len, 3,
"out of bounds");
136 if (posi > pose)
return 0;
137 if (pose - posi >= INT_MAX)
138 return luaL_error(L,
"string slice too long");
139 n = (int)(pose - posi) + 1;
140 luaL_checkstack(L, n,
"string slice too long");
143 for (s += posi - 1; s < se;) {
145 s = utf8_decode(s, &
code, !lax);
147 return luaL_error(L, MSGInvalid);
148 lua_pushinteger(L,
code);
155static void pushutfchar (
lua_State *L,
int arg) {
156 lua_Unsigned
code = (lua_Unsigned)luaL_checkinteger(L, arg);
157 luaL_argcheck(L,
code <= MAXUTF, arg,
"value out of range");
158 lua_pushfstring(L,
"%U", (
long)
code);
166 int n = lua_gettop(L);
172 luaL_buffinit(L, &b);
173 for (i = 1; i <= n; i++) {
189 const char *s = luaL_checklstring(L, 1, &len);
190 lua_Integer n = luaL_checkinteger(L, 2);
191 lua_Integer posi = (n >= 0) ? 1 : len + 1;
192 posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
193 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
194 "position out of bounds");
197 while (posi > 0 && iscontp(s + posi)) posi--;
200 if (iscontp(s + posi))
201 return luaL_error(L,
"initial position is a continuation byte");
203 while (n < 0 && posi > 0) {
206 }
while (posi > 0 && iscontp(s + posi));
212 while (n > 0 && posi < (lua_Integer)len) {
215 }
while (iscontp(s + posi));
221 lua_pushinteger(L, posi + 1);
228static int iter_aux (
lua_State *L,
int strict) {
230 const char *s = luaL_checklstring(L, 1, &len);
231 lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
233 while (iscontp(s + n)) n++;
239 const char *next = utf8_decode(s + n, &
code, strict);
240 if (next == NULL || iscontp(next))
241 return luaL_error(L, MSGInvalid);
242 lua_pushinteger(L, n + 1);
243 lua_pushinteger(L,
code);
249static int iter_auxstrict (
lua_State *L) {
250 return iter_aux(L, 1);
254 return iter_aux(L, 0);
259 int lax = lua_toboolean(L, 2);
260 const char *s = luaL_checkstring(L, 1);
261 luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
262 lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
264 lua_pushinteger(L, 0);
270#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
274 {
"offset", byteoffset},
275 {
"codepoint", codepoint},
278 {
"codes", iter_codes},
280 {
"charpattern", NULL},
285LUAMOD_API
int luaopen_utf8 (
lua_State *L) {
286 luaL_newlib(L, funcs);
287 lua_pushlstring(L, UTF8PATT,
sizeof(UTF8PATT)/
sizeof(
char) - 1);
288 lua_setfield(L, -2,
"charpattern");