Github User Fetcher 1.0.0
C Application with Server and GUI
Loading...
Searching...
No Matches
lutf8lib.c File Reference
#include "lprefix.h"
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"

Go to the source code of this file.

Macros

#define lutf8lib_c
 
#define LUA_LIB
 
#define MAXUNICODE   0x10FFFFu
 
#define MAXUTF   0x7FFFFFFFu
 
#define iscont(p)   ((*(p) & 0xC0) == 0x80)
 
#define UTF8PATT   "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"
 

Typedefs

typedef unsigned long utfint
 

Functions

static lua_Integer u_posrelat (lua_Integer pos, size_t len)
 
static const char * utf8_decode (const char *s, utfint *val, int strict)
 
static int utflen (lua_State *L)
 
static int codepoint (lua_State *L)
 
static void pushutfchar (lua_State *L, int arg)
 
static int utfchar (lua_State *L)
 
static int byteoffset (lua_State *L)
 
static int iter_aux (lua_State *L, int strict)
 
static int iter_auxstrict (lua_State *L)
 
static int iter_auxlax (lua_State *L)
 
static int iter_codes (lua_State *L)
 
LUAMOD_API int luaopen_utf8 (lua_State *L)
 

Variables

static const luaL_Reg funcs []
 

Macro Definition Documentation

◆ iscont

#define iscont ( p)    ((*(p) & 0xC0) == 0x80)

Definition at line 38 of file lua-5.4.3/src/lutf8lib.c.

Referenced by byteoffset(), and iter_aux().

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lua-5.4.3/src/lutf8lib.c.

◆ lutf8lib_c

#define lutf8lib_c

Definition at line 7 of file lua-5.4.3/src/lutf8lib.c.

◆ MAXUNICODE

#define MAXUNICODE   0x10FFFFu

Definition at line 24 of file lua-5.4.3/src/lutf8lib.c.

Referenced by utf8_decode().

◆ MAXUTF

#define MAXUTF   0x7FFFFFFFu

Definition at line 26 of file lua-5.4.3/src/lutf8lib.c.

Referenced by pushutfchar(), and utf8_decode().

◆ UTF8PATT

#define UTF8PATT   "[\0-\x7F\xC2-\xFD][\x80-\xBF]*"

Definition at line 268 of file lua-5.4.3/src/lutf8lib.c.

Referenced by luaopen_utf8().

Typedef Documentation

◆ utfint

typedef unsigned long utfint

Definition at line 34 of file lua-5.4.3/src/lutf8lib.c.

Function Documentation

◆ byteoffset()

static int byteoffset ( lua_State * L)
static

Definition at line 183 of file lua-5.4.3/src/lutf8lib.c.

183 {
184 size_t len;
185 const char *s = luaL_checklstring(L, 1, &len);
187 lua_Integer posi = (n >= 0) ? 1 : len + 1;
188 posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
189 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
190 "position out of bounds");
191 if (n == 0) {
192 /* find beginning of current byte sequence */
193 while (posi > 0 && iscont(s + posi)) posi--;
194 }
195 else {
196 if (iscont(s + posi))
197 return luaL_error(L, "initial position is a continuation byte");
198 if (n < 0) {
199 while (n < 0 && posi > 0) { /* move back */
200 do { /* find beginning of previous character */
201 posi--;
202 } while (posi > 0 && iscont(s + posi));
203 n++;
204 }
205 }
206 else {
207 n--; /* do not move for 1st character */
208 while (n > 0 && posi < (lua_Integer)len) {
209 do { /* find beginning of next character */
210 posi++;
211 } while (iscont(s + posi)); /* (cannot pass final '\0') */
212 n--;
213 }
214 }
215 }
216 if (n == 0) /* did it find given character? */
217 lua_pushinteger(L, posi + 1);
218 else /* no such character */
219 luaL_pushfail(L);
220 return 1;
221}
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int narg)
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int narg, lua_Integer def)
LUALIB_API const char * luaL_checklstring(lua_State *L, int narg, size_t *len)
#define luaL_argcheck(L, cond, numarg, extramsg)
LUA_INTEGER lua_Integer
#define luaL_pushfail(L)
#define iscont(p)
static lua_Integer u_posrelat(lua_Integer pos, size_t len)
CURL_EXTERN CURLMcode curl_socket_t s
Definition multi.h:318

References iscont, lua_pushinteger(), luaL_argcheck, luaL_checkinteger(), luaL_checklstring(), luaL_error(), luaL_optinteger(), luaL_pushfail, s, and u_posrelat().

◆ codepoint()

static int codepoint ( lua_State * L)
static

Definition at line 122 of file lua-5.4.3/src/lutf8lib.c.

122 {
123 size_t len;
124 const char *s = luaL_checklstring(L, 1, &len);
125 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
126 lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
127 int lax = lua_toboolean(L, 4);
128 int n;
129 const char *se;
130 luaL_argcheck(L, posi >= 1, 2, "out of bounds");
131 luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of bounds");
132 if (posi > pose) return 0; /* empty interval; return no values */
133 if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
134 return luaL_error(L, "string slice too long");
135 n = (int)(pose - posi) + 1; /* upper bound for number of returns */
136 luaL_checkstack(L, n, "string slice too long");
137 n = 0; /* count the number of returns */
138 se = s + pose; /* string end */
139 for (s += posi - 1; s < se;) {
140 utfint code;
141 s = utf8_decode(s, &code, !lax);
142 if (s == NULL)
143 return luaL_error(L, "invalid UTF-8 code");
144 lua_pushinteger(L, code);
145 n++;
146 }
147 return n;
148}
#define NULL
Definition gmacros.h:924
LUA_API int lua_toboolean(lua_State *L, int idx)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
unsigned long utfint
static const char * utf8_decode(const char *s, utfint *val, int strict)

References lua_pushinteger(), lua_toboolean(), luaL_argcheck, luaL_checklstring(), luaL_checkstack(), luaL_error(), luaL_optinteger(), NULL, s, u_posrelat(), and utf8_decode().

◆ iter_aux()

static int iter_aux ( lua_State * L,
int strict )
static

Definition at line 224 of file lua-5.4.3/src/lutf8lib.c.

224 {
225 size_t len;
226 const char *s = luaL_checklstring(L, 1, &len);
227 lua_Integer n = lua_tointeger(L, 2) - 1;
228 if (n < 0) /* first iteration? */
229 n = 0; /* start from here */
230 else if (n < (lua_Integer)len) {
231 n++; /* skip current byte */
232 while (iscont(s + n)) n++; /* and its continuations */
233 }
234 if (n >= (lua_Integer)len)
235 return 0; /* no more codepoints */
236 else {
237 utfint code;
238 const char *next = utf8_decode(s + n, &code, strict);
239 if (next == NULL)
240 return luaL_error(L, "invalid UTF-8 code");
241 lua_pushinteger(L, n + 1);
242 lua_pushinteger(L, code);
243 return 2;
244 }
245}
#define next(ls)
#define lua_tointeger(L, i)

References iscont, lua_pushinteger(), lua_tointeger, luaL_checklstring(), luaL_error(), next, NULL, s, and utf8_decode().

Referenced by iter_auxlax(), and iter_auxstrict().

◆ iter_auxlax()

static int iter_auxlax ( lua_State * L)
static

Definition at line 252 of file lua-5.4.3/src/lutf8lib.c.

252 {
253 return iter_aux(L, 0);
254}
static int iter_aux(lua_State *L, int strict)

References iter_aux().

Referenced by iter_codes().

◆ iter_auxstrict()

static int iter_auxstrict ( lua_State * L)
static

Definition at line 248 of file lua-5.4.3/src/lutf8lib.c.

248 {
249 return iter_aux(L, 1);
250}

References iter_aux().

Referenced by iter_codes().

◆ iter_codes()

static int iter_codes ( lua_State * L)
static

Definition at line 257 of file lua-5.4.3/src/lutf8lib.c.

257 {
258 int lax = lua_toboolean(L, 2);
259 luaL_checkstring(L, 1);
261 lua_pushvalue(L, 1);
262 lua_pushinteger(L, 0);
263 return 3;
264}
LUA_API void lua_pushvalue(lua_State *L, int idx)
#define luaL_checkstring(L, n)
#define lua_pushcfunction(L, f)
static int iter_auxlax(lua_State *L)
static int iter_auxstrict(lua_State *L)

References iter_auxlax(), iter_auxstrict(), lua_pushcfunction, lua_pushinteger(), lua_pushvalue(), lua_toboolean(), and luaL_checkstring.

◆ luaopen_utf8()

LUAMOD_API int luaopen_utf8 ( lua_State * L)

Definition at line 283 of file lua-5.4.3/src/lutf8lib.c.

283 {
284 luaL_newlib(L, funcs);
285 lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
286 lua_setfield(L, -2, "charpattern");
287 return 1;
288}
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
#define luaL_newlib(L, l)
#define UTF8PATT
static const luaL_Reg funcs[]

References funcs, funcs, lua_pushlstring(), lua_setfield(), luaL_newlib, and UTF8PATT.

◆ pushutfchar()

static void pushutfchar ( lua_State * L,
int arg )
static

Definition at line 151 of file lua-5.4.3/src/lutf8lib.c.

151 {
153 luaL_argcheck(L, code <= MAXUTF, arg, "value out of range");
154 lua_pushfstring(L, "%U", (long)code);
155}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
LUA_UNSIGNED lua_Unsigned
#define MAXUTF

References arg, lua_pushfstring(), luaL_argcheck, luaL_checkinteger(), and MAXUTF.

Referenced by utfchar().

◆ u_posrelat()

static lua_Integer u_posrelat ( lua_Integer pos,
size_t len )
static

Definition at line 43 of file lua-5.4.3/src/lutf8lib.c.

43 {
44 if (pos >= 0) return pos;
45 else if (0u - (size_t)pos > len) return 0;
46 else return (lua_Integer)len + pos + 1;
47}

Referenced by byteoffset(), codepoint(), and utflen().

◆ utf8_decode()

static const char * utf8_decode ( const char * s,
utfint * val,
int strict )
static

Definition at line 57 of file lua-5.4.3/src/lutf8lib.c.

57 {
58 static const utfint limits[] =
59 {~(utfint)0, 0x80, 0x800, 0x10000u, 0x200000u, 0x4000000u};
60 unsigned int c = (unsigned char)s[0];
61 utfint res = 0; /* final result */
62 if (c < 0x80) /* ascii? */
63 res = c;
64 else {
65 int count = 0; /* to count number of continuation bytes */
66 for (; c & 0x40; c <<= 1) { /* while it needs continuation bytes... */
67 unsigned int cc = (unsigned char)s[++count]; /* read next byte */
68 if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
69 return NULL; /* invalid byte sequence */
70 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
71 }
72 res |= ((utfint)(c & 0x7F) << (count * 5)); /* add first byte */
73 if (count > 5 || res > MAXUTF || res < limits[count])
74 return NULL; /* invalid byte sequence */
75 s += count; /* skip continuation bytes read */
76 }
77 if (strict) {
78 /* check for invalid code points; too large or surrogates */
79 if (res > MAXUNICODE || (0xD800u <= res && res <= 0xDFFFu))
80 return NULL;
81 }
82 if (val) *val = res;
83 return s + 1; /* +1 to include first byte */
84}
#define MAXUNICODE

References MAXUNICODE, MAXUTF, NULL, and s.

Referenced by codepoint(), iter_aux(), and utflen().

◆ utfchar()

static int utfchar ( lua_State * L)
static

Definition at line 161 of file lua-5.4.3/src/lutf8lib.c.

161 {
162 int n = lua_gettop(L); /* number of arguments */
163 if (n == 1) /* optimize common case of single char */
164 pushutfchar(L, 1);
165 else {
166 int i;
167 luaL_Buffer b;
168 luaL_buffinit(L, &b);
169 for (i = 1; i <= n; i++) {
170 pushutfchar(L, i);
171 luaL_addvalue(&b);
172 }
173 luaL_pushresult(&b);
174 }
175 return 1;
176}
LUA_API int lua_gettop(lua_State *L)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
static void pushutfchar(lua_State *L, int arg)

References lua_gettop(), luaL_addvalue(), luaL_buffinit(), luaL_pushresult(), and pushutfchar().

◆ utflen()

static int utflen ( lua_State * L)
static

Definition at line 92 of file lua-5.4.3/src/lutf8lib.c.

92 {
93 lua_Integer n = 0; /* counter for the number of characters */
94 size_t len; /* string length in bytes */
95 const char *s = luaL_checklstring(L, 1, &len);
96 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
97 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
98 int lax = lua_toboolean(L, 4);
99 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
100 "initial position out of bounds");
101 luaL_argcheck(L, --posj < (lua_Integer)len, 3,
102 "final position out of bounds");
103 while (posi <= posj) {
104 const char *s1 = utf8_decode(s + posi, NULL, !lax);
105 if (s1 == NULL) { /* conversion error? */
106 luaL_pushfail(L); /* return fail ... */
107 lua_pushinteger(L, posi + 1); /* ... and current position */
108 return 2;
109 }
110 posi = s1 - s;
111 n++;
112 }
113 lua_pushinteger(L, n);
114 return 1;
115}

References lua_pushinteger(), lua_toboolean(), luaL_argcheck, luaL_checklstring(), luaL_optinteger(), luaL_pushfail, NULL, s, u_posrelat(), and utf8_decode().

Variable Documentation

◆ funcs

const luaL_Reg funcs[]
static
Initial value:
= {
{"offset", byteoffset},
{"codepoint", codepoint},
{"char", utfchar},
{"len", utflen},
{"codes", iter_codes},
{"charpattern", NULL},
}
static int utfchar(lua_State *L)
static int byteoffset(lua_State *L)
static int utflen(lua_State *L)
static int iter_codes(lua_State *L)
static int codepoint(lua_State *L)

Definition at line 271 of file lua-5.4.3/src/lutf8lib.c.

271 {
272 {"offset", byteoffset},
273 {"codepoint", codepoint},
274 {"char", utfchar},
275 {"len", utflen},
276 {"codes", iter_codes},
277 /* placeholders */
278 {"charpattern", NULL},
279 {NULL, NULL}
280};

Referenced by luaopen_utf8().