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   0x10FFFF
 
#define iscont(p)   ((*(p) & 0xC0) == 0x80)
 
#define UTF8PATT   "[\0-\x7F\xC2-\xF4][\x80-\xBF]*"
 

Functions

static lua_Integer u_posrelat (lua_Integer pos, size_t len)
 
static const char * utf8_decode (const char *o, int *val)
 
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)
 
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 25 of file lua-5.3.6/src/lutf8lib.c.

Referenced by byteoffset(), and iter_aux().

◆ LUA_LIB

#define LUA_LIB

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

◆ lutf8lib_c

#define lutf8lib_c

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

◆ MAXUNICODE

#define MAXUNICODE   0x10FFFF

Definition at line 23 of file lua-5.3.6/src/lutf8lib.c.

Referenced by pushutfchar(), and utf8_decode().

◆ UTF8PATT

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

Definition at line 235 of file lua-5.3.6/src/lutf8lib.c.

Referenced by luaopen_utf8().

Function Documentation

◆ byteoffset()

static int byteoffset ( lua_State * L)
static

Definition at line 160 of file lua-5.3.6/src/lutf8lib.c.

160 {
161 size_t len;
162 const char *s = luaL_checklstring(L, 1, &len);
164 lua_Integer posi = (n >= 0) ? 1 : len + 1;
165 posi = u_posrelat(luaL_optinteger(L, 3, posi), len);
166 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 3,
167 "position out of range");
168 if (n == 0) {
169 /* find beginning of current byte sequence */
170 while (posi > 0 && iscont(s + posi)) posi--;
171 }
172 else {
173 if (iscont(s + posi))
174 return luaL_error(L, "initial position is a continuation byte");
175 if (n < 0) {
176 while (n < 0 && posi > 0) { /* move back */
177 do { /* find beginning of previous character */
178 posi--;
179 } while (posi > 0 && iscont(s + posi));
180 n++;
181 }
182 }
183 else {
184 n--; /* do not move for 1st character */
185 while (n > 0 && posi < (lua_Integer)len) {
186 do { /* find beginning of next character */
187 posi++;
188 } while (iscont(s + posi)); /* (cannot pass final '\0') */
189 n--;
190 }
191 }
192 }
193 if (n == 0) /* did it find given character? */
194 lua_pushinteger(L, posi + 1);
195 else /* no such character */
196 lua_pushnil(L);
197 return 1;
198}
LUA_API void lua_pushnil(lua_State *L)
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 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(), lua_pushnil(), luaL_argcheck, luaL_checkinteger(), luaL_checklstring(), luaL_error(), luaL_optinteger(), s, and u_posrelat().

◆ codepoint()

static int codepoint ( lua_State * L)
static

Definition at line 100 of file lua-5.3.6/src/lutf8lib.c.

100 {
101 size_t len;
102 const char *s = luaL_checklstring(L, 1, &len);
103 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
104 lua_Integer pose = u_posrelat(luaL_optinteger(L, 3, posi), len);
105 int n;
106 const char *se;
107 luaL_argcheck(L, posi >= 1, 2, "out of range");
108 luaL_argcheck(L, pose <= (lua_Integer)len, 3, "out of range");
109 if (posi > pose) return 0; /* empty interval; return no values */
110 if (pose - posi >= INT_MAX) /* (lua_Integer -> int) overflow? */
111 return luaL_error(L, "string slice too long");
112 n = (int)(pose - posi) + 1;
113 luaL_checkstack(L, n, "string slice too long");
114 n = 0;
115 se = s + pose;
116 for (s += posi - 1; s < se;) {
117 int code;
118 s = utf8_decode(s, &code);
119 if (s == NULL)
120 return luaL_error(L, "invalid UTF-8 code");
121 lua_pushinteger(L, code);
122 n++;
123 }
124 return n;
125}
#define NULL
Definition gmacros.h:924
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
static const char * utf8_decode(const char *o, int *val)

References lua_pushinteger(), 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)
static

Definition at line 201 of file lua-5.3.6/src/lutf8lib.c.

201 {
202 size_t len;
203 const char *s = luaL_checklstring(L, 1, &len);
204 lua_Integer n = lua_tointeger(L, 2) - 1;
205 if (n < 0) /* first iteration? */
206 n = 0; /* start from here */
207 else if (n < (lua_Integer)len) {
208 n++; /* skip current byte */
209 while (iscont(s + n)) n++; /* and its continuations */
210 }
211 if (n >= (lua_Integer)len)
212 return 0; /* no more codepoints */
213 else {
214 int code;
215 const char *next = utf8_decode(s + n, &code);
216 if (next == NULL || iscont(next))
217 return luaL_error(L, "invalid UTF-8 code");
218 lua_pushinteger(L, n + 1);
219 lua_pushinteger(L, code);
220 return 2;
221 }
222}
#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_codes().

◆ iter_codes()

static int iter_codes ( lua_State * L)
static

Definition at line 225 of file lua-5.3.6/src/lutf8lib.c.

225 {
226 luaL_checkstring(L, 1);
228 lua_pushvalue(L, 1);
229 lua_pushinteger(L, 0);
230 return 3;
231}
LUA_API void lua_pushvalue(lua_State *L, int idx)
#define luaL_checkstring(L, n)
#define lua_pushcfunction(L, f)
static int iter_aux(lua_State *L)

References iter_aux(), lua_pushcfunction, lua_pushinteger(), lua_pushvalue(), and luaL_checkstring.

◆ luaopen_utf8()

LUAMOD_API int luaopen_utf8 ( lua_State * L)

Definition at line 250 of file lua-5.3.6/src/lutf8lib.c.

250 {
251 luaL_newlib(L, funcs);
252 lua_pushlstring(L, UTF8PATT, sizeof(UTF8PATT)/sizeof(char) - 1);
253 lua_setfield(L, -2, "charpattern");
254 return 1;
255}
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[]

◆ pushutfchar()

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

Definition at line 128 of file lua-5.3.6/src/lutf8lib.c.

128 {
130 luaL_argcheck(L, 0 <= code && code <= MAXUNICODE, arg, "value out of range");
131 lua_pushfstring(L, "%U", (long)code);
132}
CURL_EXTERN int void * arg
Definition curl.h:2622
LUA_API const char * lua_pushfstring(lua_State *L, const char *fmt,...)
#define MAXUNICODE

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

Referenced by utfchar().

◆ u_posrelat()

static lua_Integer u_posrelat ( lua_Integer pos,
size_t len )
static

Definition at line 30 of file lua-5.3.6/src/lutf8lib.c.

30 {
31 if (pos >= 0) return pos;
32 else if (0u - (size_t)pos > len) return 0;
33 else return (lua_Integer)len + pos + 1;
34}

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

◆ utf8_decode()

static const char * utf8_decode ( const char * o,
int * val )
static

Definition at line 40 of file lua-5.3.6/src/lutf8lib.c.

40 {
41 static const unsigned int limits[] = {0xFF, 0x7F, 0x7FF, 0xFFFF};
42 const unsigned char *s = (const unsigned char *)o;
43 unsigned int c = s[0];
44 unsigned int res = 0; /* final result */
45 if (c < 0x80) /* ascii? */
46 res = c;
47 else {
48 int count = 0; /* to count number of continuation bytes */
49 while (c & 0x40) { /* still have continuation bytes? */
50 int cc = s[++count]; /* read next byte */
51 if ((cc & 0xC0) != 0x80) /* not a continuation byte? */
52 return NULL; /* invalid byte sequence */
53 res = (res << 6) | (cc & 0x3F); /* add lower 6 bits from cont. byte */
54 c <<= 1; /* to test next bit */
55 }
56 res |= ((c & 0x7F) << (count * 5)); /* add first byte */
57 if (count > 3 || res > MAXUNICODE || res <= limits[count])
58 return NULL; /* invalid byte sequence */
59 s += count; /* skip continuation bytes read */
60 }
61 if (val) *val = res;
62 return (const char *)s + 1; /* +1 to include first byte */
63}

References MAXUNICODE, NULL, and s.

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

◆ utfchar()

static int utfchar ( lua_State * L)
static

Definition at line 138 of file lua-5.3.6/src/lutf8lib.c.

138 {
139 int n = lua_gettop(L); /* number of arguments */
140 if (n == 1) /* optimize common case of single char */
141 pushutfchar(L, 1);
142 else {
143 int i;
144 luaL_Buffer b;
145 luaL_buffinit(L, &b);
146 for (i = 1; i <= n; i++) {
147 pushutfchar(L, i);
148 luaL_addvalue(&b);
149 }
150 luaL_pushresult(&b);
151 }
152 return 1;
153}
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 71 of file lua-5.3.6/src/lutf8lib.c.

71 {
72 int n = 0;
73 size_t len;
74 const char *s = luaL_checklstring(L, 1, &len);
75 lua_Integer posi = u_posrelat(luaL_optinteger(L, 2, 1), len);
76 lua_Integer posj = u_posrelat(luaL_optinteger(L, 3, -1), len);
77 luaL_argcheck(L, 1 <= posi && --posi <= (lua_Integer)len, 2,
78 "initial position out of string");
79 luaL_argcheck(L, --posj < (lua_Integer)len, 3,
80 "final position out of string");
81 while (posi <= posj) {
82 const char *s1 = utf8_decode(s + posi, NULL);
83 if (s1 == NULL) { /* conversion error? */
84 lua_pushnil(L); /* return nil ... */
85 lua_pushinteger(L, posi + 1); /* ... and current position */
86 return 2;
87 }
88 posi = s1 - s;
89 n++;
90 }
91 lua_pushinteger(L, n);
92 return 1;
93}

References lua_pushinteger(), lua_pushnil(), luaL_argcheck, luaL_checklstring(), luaL_optinteger(), 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 238 of file lua-5.3.6/src/lutf8lib.c.

238 {
239 {"offset", byteoffset},
240 {"codepoint", codepoint},
241 {"char", utfchar},
242 {"len", utflen},
243 {"codes", iter_codes},
244 /* placeholders */
245 {"charpattern", NULL},
246 {NULL, NULL}
247};

Referenced by duk__inc_data_inner_refcounts(), duk__refcount_finalize_hobject(), duk_put_function_list(), duk_put_number_list(), luaopen_LuaXML_lib(), and luaopen_utf8().