2014-02-07 00:03:24 +04:00
|
|
|
/*
|
2018-07-12 21:56:44 +03:00
|
|
|
** $Id: lutf8lib.c $
|
2014-02-07 00:03:24 +04:00
|
|
|
** Standard library for UTF-8 manipulation
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
2014-11-02 22:19:04 +03:00
|
|
|
#define lutf8lib_c
|
|
|
|
#define LUA_LIB
|
|
|
|
|
|
|
|
#include "lprefix.h"
|
|
|
|
|
2014-02-06 21:32:33 +04:00
|
|
|
|
|
|
|
#include <assert.h>
|
2015-03-28 22:16:55 +03:00
|
|
|
#include <limits.h>
|
2014-02-06 21:32:33 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "lua.h"
|
2014-02-07 00:03:24 +04:00
|
|
|
|
2014-02-06 21:32:33 +04:00
|
|
|
#include "lauxlib.h"
|
2014-02-07 00:03:24 +04:00
|
|
|
#include "lualib.h"
|
2024-06-20 20:46:06 +03:00
|
|
|
#include "llimits.h"
|
2014-02-06 21:32:33 +04:00
|
|
|
|
2018-07-12 21:56:44 +03:00
|
|
|
|
2019-03-15 19:14:17 +03:00
|
|
|
#define MAXUNICODE 0x10FFFFu
|
|
|
|
|
|
|
|
#define MAXUTF 0x7FFFFFFFu
|
2014-02-06 21:32:33 +04:00
|
|
|
|
2022-09-23 16:41:16 +03:00
|
|
|
|
|
|
|
#define MSGInvalid "invalid UTF-8 code"
|
|
|
|
|
2018-07-12 21:56:44 +03:00
|
|
|
|
2022-09-23 16:41:16 +03:00
|
|
|
#define iscont(c) (((c) & 0xC0) == 0x80)
|
|
|
|
#define iscontp(p) iscont(*(p))
|
2014-02-06 21:32:33 +04:00
|
|
|
|
|
|
|
|
|
|
|
/* from strlib */
|
|
|
|
/* translate a relative string position: negative means back from end */
|
2014-03-20 18:11:00 +04:00
|
|
|
static lua_Integer u_posrelat (lua_Integer pos, size_t len) {
|
2014-02-06 21:32:33 +04:00
|
|
|
if (pos >= 0) return pos;
|
|
|
|
else if (0u - (size_t)pos > len) return 0;
|
|
|
|
else return (lua_Integer)len + pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-03-15 19:14:17 +03:00
|
|
|
** Decode one UTF-8 sequence, returning NULL if byte sequence is
|
|
|
|
** invalid. The array 'limits' stores the minimum value for each
|
|
|
|
** sequence length, to check for overlong representations. Its first
|
|
|
|
** entry forces an error for non-ascii bytes with no continuation
|
|
|
|
** bytes (count == 0).
|
2014-02-06 21:32:33 +04:00
|
|
|
*/
|
2024-06-20 20:46:06 +03:00
|
|
|
static const char *utf8_decode (const char *s, l_uint32 *val, int strict) {
|
|
|
|
static const l_uint32 limits[] =
|
|
|
|
{~(l_uint32)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
|
2019-03-15 19:14:17 +03:00
|
|
|
unsigned int c = (unsigned char)s[0];
|
2024-06-20 20:46:06 +03:00
|
|
|
l_uint32 res = 0; /* final result */
|
2014-02-06 21:32:33 +04:00
|
|
|
if (c < 0x80) /* ascii? */
|
|
|
|
res = c;
|
|
|
|
else {
|
|
|
|
int count = 0; /* to count number of continuation bytes */
|
2019-03-15 19:14:17 +03:00
|
|
|
for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
|
|
|
|
unsigned int cc = (unsigned char)s[++count]; /* read next byte */
|
2022-09-23 16:41:16 +03:00
|
|
|
if (!iscont(cc)) /* not a continuation byte? */
|
2014-02-06 21:32:33 +04:00
|
|
|
return NULL; /* invalid byte sequence */
|
|
|
|
res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
|
|
|
|
}
|
2024-06-20 20:46:06 +03:00
|
|
|
res |= ((l_uint32)(c & 0x7F) << (count * 5)); /* add first byte */
|
2019-03-15 19:14:17 +03:00
|
|
|
if (count > 5 || res > MAXUTF || res < limits[count])
|
2014-02-06 21:32:33 +04:00
|
|
|
return NULL; /* invalid byte sequence */
|
|
|
|
s += count; /* skip continuation bytes read */
|
|
|
|
}
|
2019-03-15 19:14:17 +03:00
|
|
|
if (strict) {
|
|
|
|
/* check for invalid code points; too large or surrogates */
|
|
|
|
if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-02-06 21:32:33 +04:00
|
|
|
if (val) *val = res;
|
2019-03-15 19:14:17 +03:00
|
|
|
return s + 1; /* +1 to include first byte */
|
2014-02-06 21:32:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-03-19 17:15:49 +03:00
|
|
|
** utf8len(s [, i [, j [, lax]]]) --> number of characters that
|
2019-03-15 19:14:17 +03:00
|
|
|
** start in the range [i,j], or nil + current position if 's' is not
|
|
|
|
** well formed in that interval
|
2014-02-06 21:32:33 +04:00
|
|
|
*/
|
|
|
|
static int utflen (lua_State *L) {
|
2018-07-12 21:56:44 +03:00
|
|
|
lua_Integer n = 0; /* counter for the number of characters */
|
|
|
|
size_t len; /* string length in bytes */
|
2014-02-06 21:32:33 +04:00
|
|
|
const char *s = luaL_checklstring(L, 1, &len);
|
2014-04-02 21:01:22 +04:00
|
|
|
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
|
|
|
lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
|
2019-03-19 17:15:49 +03:00
|
|
|
int lax = lua_toboolean(L, 4);
|
2014-04-02 21:01:22 +04:00
|
|
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
|
2020-05-27 17:46:47 +03:00
|
|
|
"initial position out of bounds");
|
2014-04-02 21:01:22 +04:00
|
|
|
luaL_argcheck(L, --posj < (lua_Integer)len, 3,
|
2020-05-27 17:46:47 +03:00
|
|
|
"final position out of bounds");
|
2014-04-02 21:01:22 +04:00
|
|
|
while (posi <= posj) {
|
2019-03-19 17:15:49 +03:00
|
|
|
const char *s1 = utf8_decode(s + posi, NULL, !lax);
|
2014-04-02 21:01:22 +04:00
|
|
|
if (s1 == NULL) { /* conversion error? */
|
2019-08-16 20:58:02 +03:00
|
|
|
luaL_pushfail(L); /* return fail ... */
|
2014-04-02 21:01:22 +04:00
|
|
|
lua_pushinteger(L, posi + 1); /* ... and current position */
|
|
|
|
return 2;
|
|
|
|
}
|
2024-07-27 21:13:21 +03:00
|
|
|
posi = ct_diff2S(s1 - s);
|
2014-02-06 21:32:33 +04:00
|
|
|
n++;
|
2014-04-02 21:01:22 +04:00
|
|
|
}
|
|
|
|
lua_pushinteger(L, n);
|
2014-02-06 21:32:33 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2019-03-19 17:15:49 +03:00
|
|
|
** codepoint(s, [i, [j [, lax]]]) -> returns codepoints for all
|
2019-03-15 19:14:17 +03:00
|
|
|
** characters that start in the range [i,j]
|
2014-02-06 21:32:33 +04:00
|
|
|
*/
|
|
|
|
static int codepoint (lua_State *L) {
|
|
|
|
size_t len;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &len);
|
2014-03-20 18:11:00 +04:00
|
|
|
lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
|
|
|
|
lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
2019-03-19 17:15:49 +03:00
|
|
|
int lax = lua_toboolean(L, 4);
|
2014-02-06 21:32:33 +04:00
|
|
|
int n;
|
|
|
|
const char *se;
|
2020-05-27 17:46:47 +03:00
|
|
|
luaL_argcheck(L, posi >= 1, 2, "out of bounds");
|
|
|
|
luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
|
2014-02-06 21:32:33 +04:00
|
|
|
if (posi > pose) return 0; /* empty interval; return no values */
|
2015-03-28 22:16:55 +03:00
|
|
|
if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
|
2014-02-06 21:32:33 +04:00
|
|
|
return luaL_error(L, "string slice too long");
|
2018-07-12 21:56:44 +03:00
|
|
|
n = (int)(pose - posi) + 1; /* upper bound for number of returns */
|
2014-02-06 21:32:33 +04:00
|
|
|
luaL_checkstack(L, n, "string slice too long");
|
2018-07-12 21:56:44 +03:00
|
|
|
n = 0; /* count the number of returns */
|
|
|
|
se = s + pose; /* string end */
|
2014-02-06 21:32:33 +04:00
|
|
|
for (s += posi - 1; s < se;) {
|
2024-06-20 20:46:06 +03:00
|
|
|
l_uint32 code;
|
2019-03-19 17:15:49 +03:00
|
|
|
s = utf8_decode(s, &code, !lax);
|
2014-02-06 21:32:33 +04:00
|
|
|
if (s == NULL)
|
2022-09-23 16:41:16 +03:00
|
|
|
return luaL_error(L, MSGInvalid);
|
2024-07-27 21:13:21 +03:00
|
|
|
lua_pushinteger(L, l_castU2S(code));
|
2014-02-06 21:32:33 +04:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void pushutfchar (lua_State *L, int arg) {
|
2019-03-15 19:14:17 +03:00
|
|
|
lua_Unsigned code = (lua_Unsigned)luaL_checkinteger(L, arg);
|
|
|
|
luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
|
2014-10-01 15:52:33 +04:00
|
|
|
lua_pushfstring(L, "%U", (long)code);
|
2014-02-06 21:32:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
** utfchar(n1, n2, ...) -> char(n1)..char(n2)...
|
|
|
|
*/
|
|
|
|
static int utfchar (lua_State *L) {
|
|
|
|
int n = lua_gettop(L); /* number of arguments */
|
|
|
|
if (n == 1) /* optimize common case of single char */
|
|
|
|
pushutfchar(L, 1);
|
|
|
|
else {
|
|
|
|
int i;
|
|
|
|
luaL_Buffer b;
|
|
|
|
luaL_buffinit(L, &b);
|
|
|
|
for (i = 1; i <= n; i++) {
|
|
|
|
pushutfchar(L, i);
|
|
|
|
luaL_addvalue(&b);
|
|
|
|
}
|
|
|
|
luaL_pushresult(&b);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2024-05-27 17:29:39 +03:00
|
|
|
** offset(s, n, [i]) -> indices where n-th character counting from
|
|
|
|
** position 'i' starts and ends; 0 means character at 'i'.
|
2014-02-06 21:32:33 +04:00
|
|
|
*/
|
|
|
|
static int byteoffset (lua_State *L) {
|
|
|
|
size_t len;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &len);
|
2014-10-01 15:52:33 +04:00
|
|
|
lua_Integer n = luaL_checkinteger(L, 2);
|
2024-07-27 19:32:59 +03:00
|
|
|
lua_Integer posi = (n >= 0) ? 1 : cast_st2S(len) + 1;
|
2014-04-03 17:45:09 +04:00
|
|
|
posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
|
|
|
|
luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
|
2020-05-27 17:46:47 +03:00
|
|
|
"position out of bounds");
|
2014-02-06 21:32:33 +04:00
|
|
|
if (n == 0) {
|
|
|
|
/* find beginning of current byte sequence */
|
2022-09-23 16:41:16 +03:00
|
|
|
while (posi > 0 && iscontp(s + posi)) posi--;
|
2014-02-06 21:32:33 +04:00
|
|
|
}
|
|
|
|
else {
|
2022-09-23 16:41:16 +03:00
|
|
|
if (iscontp(s + posi))
|
2017-03-14 15:40:44 +03:00
|
|
|
return luaL_error(L, "initial position is a continuation byte");
|
2014-04-03 17:45:09 +04:00
|
|
|
if (n < 0) {
|
2024-09-06 20:35:04 +03:00
|
|
|
while (n < 0 && posi > 0) { /* move back */
|
|
|
|
do { /* find beginning of previous character */
|
|
|
|
posi--;
|
|
|
|
} while (posi > 0 && iscontp(s + posi));
|
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
n--; /* do not move for 1st character */
|
|
|
|
while (n > 0 && posi < (lua_Integer)len) {
|
|
|
|
do { /* find beginning of next character */
|
|
|
|
posi++;
|
|
|
|
} while (iscontp(s + posi)); /* (cannot pass final '\0') */
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
}
|
2014-02-06 21:32:33 +04:00
|
|
|
}
|
2024-05-27 17:29:39 +03:00
|
|
|
if (n != 0) { /* did not find given character? */
|
2019-08-16 20:58:02 +03:00
|
|
|
luaL_pushfail(L);
|
2024-05-27 17:29:39 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
lua_pushinteger(L, posi + 1); /* initial position */
|
|
|
|
if ((s[posi] & 0x80) != 0) { /* multi-byte character? */
|
|
|
|
do {
|
|
|
|
posi++;
|
|
|
|
} while (iscontp(s + posi + 1)); /* skip to final byte */
|
|
|
|
}
|
|
|
|
/* else one-byte character: final position is the initial one */
|
|
|
|
lua_pushinteger(L, posi + 1); /* 'posi' now is the final position */
|
|
|
|
return 2;
|
2014-02-06 21:32:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 19:14:17 +03:00
|
|
|
static int iter_aux (lua_State *L, int strict) {
|
2014-02-06 21:32:33 +04:00
|
|
|
size_t len;
|
|
|
|
const char *s = luaL_checklstring(L, 1, &len);
|
2021-09-22 19:10:39 +03:00
|
|
|
lua_Unsigned n = (lua_Unsigned)lua_tointeger(L, 2);
|
|
|
|
if (n < len) {
|
2022-09-23 16:41:16 +03:00
|
|
|
while (iscontp(s + n)) n++; /* go to next character */
|
2014-02-06 21:32:33 +04:00
|
|
|
}
|
2021-09-22 19:10:39 +03:00
|
|
|
if (n >= len) /* (also handles original 'n' being negative) */
|
2014-02-06 21:32:33 +04:00
|
|
|
return 0; /* no more codepoints */
|
|
|
|
else {
|
2024-06-20 20:46:06 +03:00
|
|
|
l_uint32 code;
|
2019-03-15 19:14:17 +03:00
|
|
|
const char *next = utf8_decode(s + n, &code, strict);
|
2022-09-23 16:41:16 +03:00
|
|
|
if (next == NULL || iscontp(next))
|
|
|
|
return luaL_error(L, MSGInvalid);
|
2024-07-27 19:32:59 +03:00
|
|
|
lua_pushinteger(L, l_castU2S(n + 1));
|
2024-07-27 21:13:21 +03:00
|
|
|
lua_pushinteger(L, l_castU2S(code));
|
2014-02-06 21:32:33 +04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-03-15 19:14:17 +03:00
|
|
|
static int iter_auxstrict (lua_State *L) {
|
|
|
|
return iter_aux(L, 1);
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:15:49 +03:00
|
|
|
static int iter_auxlax (lua_State *L) {
|
2019-03-15 19:14:17 +03:00
|
|
|
return iter_aux(L, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-02-06 21:32:33 +04:00
|
|
|
static int iter_codes (lua_State *L) {
|
2019-03-19 17:15:49 +03:00
|
|
|
int lax = lua_toboolean(L, 2);
|
2022-09-23 16:41:16 +03:00
|
|
|
const char *s = luaL_checkstring(L, 1);
|
|
|
|
luaL_argcheck(L, !iscontp(s), 1, MSGInvalid);
|
2019-03-19 17:15:49 +03:00
|
|
|
lua_pushcfunction(L, lax ? iter_auxlax : iter_auxstrict);
|
2014-02-06 21:32:33 +04:00
|
|
|
lua_pushvalue(L, 1);
|
|
|
|
lua_pushinteger(L, 0);
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* pattern to match a single UTF-8 character */
|
2019-03-15 19:14:17 +03:00
|
|
|
#define UTF8PATT "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
|
2014-02-06 21:32:33 +04:00
|
|
|
|
|
|
|
|
2015-03-28 22:16:55 +03:00
|
|
|
static const luaL_Reg funcs[] = {
|
2014-02-06 21:32:33 +04:00
|
|
|
{"offset", byteoffset},
|
|
|
|
{"codepoint", codepoint},
|
|
|
|
{"char", utfchar},
|
|
|
|
{"len", utflen},
|
|
|
|
{"codes", iter_codes},
|
2014-07-16 17:56:14 +04:00
|
|
|
/* placeholders */
|
2014-10-15 18:31:10 +04:00
|
|
|
{"charpattern", NULL},
|
2014-02-06 21:32:33 +04:00
|
|
|
{NULL, NULL}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2014-05-14 22:33:37 +04:00
|
|
|
LUAMOD_API int luaopen_utf8 (lua_State *L) {
|
2014-02-06 21:32:33 +04:00
|
|
|
luaL_newlib(L, funcs);
|
2015-03-05 19:07:46 +03:00
|
|
|
lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
|
2014-10-15 18:31:10 +04:00
|
|
|
lua_setfield(L, -2, "charpattern");
|
2014-02-06 21:32:33 +04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|