33#if !defined(LUA_MAXCAPTURES)
34#define LUA_MAXCAPTURES 32
39#define uchar(c) ((unsigned char)(c))
46#define MAX_SIZET ((size_t)(~(size_t)0))
49 (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
64 if (pos >= 0)
return pos;
65 else if (0u - (
size_t)pos > len)
return 0;
75 if (start < 1) start = 1;
89 for (i = 0; i < l; i++)
103 p[i] = tolower(
uchar(
s[i]));
116 p[i] = toupper(
uchar(
s[i]));
128 else if (l + lsep < l || l + lsep >
MAXSIZE / n)
129 return luaL_error(L,
"resulting string too large");
131 size_t totallen = (size_t)n * l + (
size_t)(n - 1) * lsep;
135 memcpy(p,
s, l *
sizeof(
char)); p += l;
137 memcpy(p, sep, lsep *
sizeof(
char));
141 memcpy(p,
s, l *
sizeof(
char));
154 if (posi < 1) posi = 1;
156 if (posi > pose)
return 0;
157 if (pose - posi >= INT_MAX)
158 return luaL_error(L,
"string slice too long");
159 n = (int)(pose - posi) + 1;
172 for (i=1; i<=n; i++) {
196 return luaL_error(L,
"unable to dump given function");
210#define CAP_UNFINISHED (-1)
211#define CAP_POSITION (-2)
233#if !defined(MAXCCALLS)
239#define SPECIALS "^$*+?.([%-"
245 return luaL_error(ms->
L,
"invalid capture index %%%d", l + 1);
251 int level = ms->
level;
252 for (level--; level>=0; level--)
254 return luaL_error(ms->
L,
"invalid pattern capture");
262 luaL_error(ms->
L,
"malformed pattern (ends with '%%')");
269 luaL_error(ms->
L,
"malformed pattern (missing ']')");
270 if (*(p++) ==
L_ESC && p < ms->p_end)
284 switch (tolower(cl)) {
285 case 'a' : res = isalpha(c);
break;
286 case 'c' : res = iscntrl(c);
break;
287 case 'd' : res = isdigit(c);
break;
288 case 'g' : res = isgraph(c);
break;
289 case 'l' : res = islower(c);
break;
290 case 'p' : res = ispunct(c);
break;
291 case 's' : res = isspace(c);
break;
292 case 'u' : res = isupper(c);
break;
293 case 'w' : res = isalnum(c);
break;
294 case 'x' : res = isxdigit(c);
break;
295 case 'z' : res = (c == 0);
break;
296 default:
return (cl == c);
298 return (islower(cl) ? res : !res);
314 else if ((*(p+1) ==
'-') && (p+2 < ec)) {
319 else if (
uchar(*p) == c)
return sig;
335 default:
return (
uchar(*p) == c);
343 if (p >= ms->
p_end - 1)
344 luaL_error(ms->
L,
"malformed pattern (missing arguments to '%%b')");
345 if (*
s != *p)
return NULL;
350 while (++s < ms->src_end) {
352 if (--cont == 0)
return s+1;
354 else if (*
s == b) cont++;
362 const char *p,
const char *ep) {
368 const char *res =
match(ms, (
s+i), ep+1);
377 const char *p,
const char *ep) {
379 const char *res =
match(ms,
s, ep+1);
390 const char *p,
int what) {
392 int level = ms->
level;
418 if ((
size_t)(ms->
src_end-
s) >= len &&
429 if (p != ms->
p_end) {
443 if ((p + 1) != ms->
p_end)
458 const char *ep;
char previous;
461 luaL_error(ms->
L,
"missing '[' after '%%f' in pattern");
463 previous = (
s == ms->
src_init) ?
'\0' : *(
s - 1);
471 case '0':
case '1':
case '2':
case '3':
472 case '4':
case '5':
case '6':
case '7':
473 case '8':
case '9': {
488 if (*ep ==
'*' || *ep ==
'?' || *ep ==
'-') {
489 p = ep + 1;
goto init;
501 p = ep + 1;
goto init;
515 s++; p = ep;
goto init;
528static const char *
lmemfind (
const char *s1,
size_t l1,
529 const char *s2,
size_t l2) {
530 if (l2 == 0)
return s1;
531 else if (l2 > l1)
return NULL;
536 while (l1 > 0 && (init = (
const char *)memchr(s1, *s2, l1)) !=
NULL) {
538 if (memcmp(init, s2+1, l2) == 0)
552 if (i >= ms->
level) {
556 luaL_error(ms->
L,
"invalid capture index %%%d", i + 1);
571 int nlevels = (ms->
level == 0 &&
s) ? 1 : ms->
level;
573 for (i = 0; i < nlevels; i++)
585 upto += strlen(p + upto) + 1;
592 const char *
s,
size_t ls,
const char *p,
size_t lp) {
612 if (init < 1) init = 1;
620 const char *s2 =
lmemfind(
s + init - 1, ls - (
size_t)init + 1, p, lp);
629 const char *s1 =
s + init - 1;
630 int anchor = (*p ==
'^');
647 }
while (s1++ < ms.
src_end && !anchor);
708 for (i = 0; i < l; i++) {
709 if (news[i] !=
L_ESC)
713 if (!isdigit(
uchar(news[i]))) {
714 if (news[i] !=
L_ESC)
718 else if (news[i] ==
'0')
732 const char *e,
int tr) {
766 const char *lastmatch =
NULL;
769 int anchor = (*p ==
'^');
775 "string/function/table expected");
784 if ((e =
match(&ms, src, p)) !=
NULL && e != lastmatch) {
810#if !defined(lua_number2strx)
818#define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
827#define L_NBFD ((l_mathlim(MANT_DIG) - 1)%4 + 1)
836 buff[n] = (d < 10 ? d +
'0' : d - 10 +
'a');
841static int num2straux (
char *buff,
int sz,
lua_Number x) {
857 buff[n++] =
'0'; buff[n++] =
'x';
858 m = adddigit(buff, n++, m * (1 << L_NBFD));
863 m = adddigit(buff, n++, m * 16);
866 n +=
l_sprintf(buff + n, sz - n,
"p%+d", e);
875 int n = num2straux(buff, sz, x);
876 if (fmt[SIZELENMOD] ==
'A') {
878 for (i = 0; i < n; i++)
879 buff[i] = toupper(
uchar(buff[i]));
881 else if (fmt[SIZELENMOD] !=
'a')
882 return luaL_error(L,
"modifiers for format '%%a'/'%%A' not implemented");
896#define MAX_ITEM (120 + l_mathlim(MAX_10_EXP))
911 if (*
s ==
'"' || *
s ==
'\\' || *
s ==
'\n') {
915 else if (iscntrl(
uchar(*
s))) {
917 if (!isdigit(
uchar(*(
s+1))))
935 if (memchr(buff,
'.', nb) ==
NULL) {
937 char *ppoint = (
char *)memchr(buff, point, nb);
938 if (ppoint) *ppoint =
'.';
961 const char *
format = (n == LUA_MININTEGER)
962 ?
"0x%" LUA_INTEGER_FRMLEN
"x"
982 const char *p = strfrmt;
983 while (*p !=
'\0' && strchr(
FLAGS, *p) !=
NULL) p++;
984 if ((
size_t)(p - strfrmt) >=
sizeof(
FLAGS)/
sizeof(
char))
985 luaL_error(L,
"invalid format (repeated flags)");
986 if (isdigit(
uchar(*p))) p++;
987 if (isdigit(
uchar(*p))) p++;
990 if (isdigit(
uchar(*p))) p++;
991 if (isdigit(
uchar(*p))) p++;
993 if (isdigit(
uchar(*p)))
994 luaL_error(L,
"invalid format (width or precision too long)");
996 memcpy(form, strfrmt, ((p - strfrmt) + 1) *
sizeof(
char));
997 form += (p - strfrmt) + 1;
1007 size_t l = strlen(form);
1008 size_t lm = strlen(lenmod);
1009 char spec = form[l - 1];
1010 strcpy(form + l - 1, lenmod);
1011 form[l + lm - 1] = spec;
1012 form[l + lm] =
'\0';
1021 const char *strfrmt_end = strfrmt+sfl;
1024 while (strfrmt < strfrmt_end) {
1025 if (*strfrmt !=
L_ESC)
1027 else if (*++strfrmt ==
L_ESC)
1036 switch (*strfrmt++) {
1042 case 'o':
case 'u':
case 'x':
case 'X': {
1053 case 'e':
case 'E':
case 'f':
1054 case 'g':
case 'G': {
1067 if (form[2] ==
'\0')
1071 if (!strchr(form,
'.') && l >= 100) {
1083 return luaL_error(L,
"invalid option '%%%c' to 'format'",
1106#if !defined(LUAL_PACKPADBYTE)
1107#define LUAL_PACKPADBYTE 0x00
1111#define MAXINTSIZE 16
1117#define MC ((1 << NB) - 1)
1120#define SZINT ((int)sizeof(lua_Integer))
1136#define MAXALIGN (offsetof(struct cD, u))
1180static int digit (
int c) {
return '0' <= c && c <=
'9'; }
1182static int getnum (
const char **fmt,
int df) {
1188 a =
a*10 + (*((*fmt)++) -
'0');
1200 int sz =
getnum(fmt, df);
1202 return luaL_error(h->
L,
"integral size (%d) out of limits [1,%d]",
1222 int opt = *((*fmt)++);
1225 case 'b': *size =
sizeof(char);
return Kint;
1226 case 'B': *size =
sizeof(char);
return Kuint;
1227 case 'h': *size =
sizeof(short);
return Kint;
1228 case 'H': *size =
sizeof(short);
return Kuint;
1229 case 'l': *size =
sizeof(long);
return Kint;
1230 case 'L': *size =
sizeof(long);
return Kuint;
1233 case 'T': *size =
sizeof(size_t);
return Kuint;
1234 case 'f': *size =
sizeof(float);
return Kfloat;
1235 case 'd': *size =
sizeof(double);
return Kfloat;
1243 luaL_error(h->
L,
"missing size for format option 'c'");
1245 case 'z':
return Kzstr;
1246 case 'x': *size = 1;
return Kpadding;
1253 default:
luaL_error(h->
L,
"invalid format option '%c'", opt);
1269 const char **fmt,
int *psize,
int *ntoalign) {
1273 if (**fmt ==
'\0' ||
getoption(h, fmt, &align) ==
Kchar || align == 0)
1276 if (align <= 1 || opt ==
Kchar)
1281 if ((align & (align - 1)) != 0)
1282 luaL_argerror(h->
L, 1,
"format asks for alignment not power of 2");
1283 *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
1296 int islittle,
int size,
int neg) {
1299 buff[islittle ? 0 : size - 1] = (char)(n &
MC);
1300 for (i = 1; i < size; i++) {
1302 buff[islittle ? i : size - 1 - i] = (char)(n &
MC);
1304 if (neg && size >
SZINT) {
1305 for (i =
SZINT; i < size; i++)
1306 buff[islittle ? i : size - 1 - i] = (char)
MC;
1317 int size,
int islittle) {
1320 *(dest++) = *(src++);
1325 *(dest--) = *(src++);
1335 size_t totalsize = 0;
1339 while (*fmt !=
'\0') {
1342 totalsize += ntoalign + size;
1343 while (ntoalign-- > 0)
1360 arg,
"unsigned overflow");
1368 if (size ==
sizeof(u.
f)) u.
f = (float)n;
1369 else if (size ==
sizeof(u.
d)) u.
d = (
double)n;
1380 "string longer than given size");
1382 while (len++ < (
size_t)size)
1390 len < ((
size_t)1 << (size *
NB)),
1391 arg,
"string length does not fit in given size");
1403 totalsize += len + 1;
1420 size_t totalsize = 0;
1422 while (*fmt !=
'\0') {
1427 "format result too large");
1451 int islittle,
int size,
int issigned) {
1455 for (i = limit - 1; i >= 0; i--) {
1457 res |= (
lua_Unsigned)(
unsigned char)str[islittle ? i : size - 1 - i];
1465 else if (size >
SZINT) {
1467 for (i = limit; i < size; i++) {
1468 if ((
unsigned char)str[islittle ? i : size - 1 - i] !=
mask)
1469 luaL_error(L,
"%d-byte integer does not fit into Lua Integer", size);
1483 luaL_argcheck(L, pos <= ld, 3,
"initial position out of string");
1485 while (*fmt !=
'\0') {
1488 if ((
size_t)ntoalign + size > ~pos || pos + ntoalign + size > ld)
1518 luaL_argcheck(L, pos + len + size <= ld, 2,
"data string too short");
1524 size_t len = (int)strlen(data + pos);
static size_t find(const char *s, const char *pattern, size_t start)
CURL_EXTERN int void * arg
json_t format(printf, 1, 2)))
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API void lua_pushnil(lua_State *L)
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data)
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API int lua_toboolean(lua_State *L, int idx)
LUA_API const char * lua_tolstring(lua_State *L, int idx, size_t *len)
LUA_API void lua_pushvalue(lua_State *L, int idx)
LUA_API int lua_type(lua_State *L, int idx)
LUA_API void lua_gettable(lua_State *L, int idx)
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
LUA_API int lua_isstring(lua_State *L, int idx)
LUA_API void * lua_touserdata(lua_State *L, int idx)
LUA_API void lua_pushlstring(lua_State *L, const char *s, size_t len)
LUA_API void lua_settop(lua_State *L, int idx)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
LUA_API int lua_gettop(lua_State *L)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *mes)
LUALIB_API void luaL_checktype(lua_State *L, int narg, int t)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
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)
LUALIB_API const char * luaL_optlstring(lua_State *L, int narg, const char *def, size_t *len)
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
LUALIB_API int luaL_argerror(lua_State *L, int narg, const char *extramsg)
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int narg)
#define luaL_typename(L, i)
#define luaL_addsize(B, n)
#define luaL_addchar(B, c)
#define luaL_checkstring(L, n)
#define luaL_argcheck(L, cond, numarg, extramsg)
#define lua_pushliteral(L, s)
#define lua_upvalueindex(i)
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
LUALIB_API char * luaL_buffinitsize(lua_State *L, luaL_Buffer *B, size_t sz)
LUALIB_API const char * luaL_tolstring(lua_State *L, int idx, size_t *len)
LUALIB_API void luaL_pushresultsize(luaL_Buffer *B, size_t sz)
#define luaL_newlib(L, l)
#define lua_tointeger(L, i)
LUA_UNSIGNED lua_Unsigned
#define lua_tonumber(L, i)
#define lua_call(L, n, r)
LUA_API int lua_isinteger(lua_State *L, int idx)
static int str_sub(lua_State *L)
static lua_Integer unpackint(lua_State *L, const char *str, int islittle, int size, int issigned)
static void reprepstate(MatchState *ms)
static void copywithendian(volatile char *dest, volatile const char *src, int size, int islittle)
static const union @79 nativeendian
static const char * lmemfind(const char *s1, size_t l1, const char *s2, size_t l2)
static int singlematch(MatchState *ms, const char *s, const char *p, const char *ep)
static const char * match_capture(MatchState *ms, const char *s, int l)
static int matchbracketclass(int c, const char *p, const char *ec)
static void createmetatable(lua_State *L)
static void initheader(lua_State *L, Header *h)
static const char * max_expand(MatchState *ms, const char *s, const char *p, const char *ep)
static int getnum(const char **fmt, int df)
static KOption getdetails(Header *h, size_t totalsize, const char **fmt, int *psize, int *ntoalign)
static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
static int getnumlimit(Header *h, const char **fmt, int df)
static void addliteral(lua_State *L, luaL_Buffer *b, int arg)
static void packint(luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg)
static int str_len(lua_State *L)
static int str_find(lua_State *L)
static int str_upper(lua_State *L)
static int str_unpack(lua_State *L)
static int str_char(lua_State *L)
static int str_gsub(lua_State *L)
static int match_class(int c, int cl)
struct GMatchState GMatchState
static int str_reverse(lua_State *L)
static int str_match(lua_State *L)
static const char * match(MatchState *ms, const char *s, const char *p)
static int capture_to_close(MatchState *ms)
static int str_find_aux(lua_State *L, int find)
static void checkdp(char *buff, int nb)
static lua_Integer posrelat(lua_Integer pos, size_t len)
static void addquoted(luaL_Buffer *b, const char *s, size_t len)
static int nospecials(const char *p, size_t l)
static const char * end_capture(MatchState *ms, const char *s, const char *p)
static const char * scanformat(lua_State *L, const char *strfrmt, char *form)
static void prepstate(MatchState *ms, lua_State *L, const char *s, size_t ls, const char *p, size_t lp)
struct MatchState MatchState
static int check_capture(MatchState *ms, int l)
static int str_lower(lua_State *L)
static void addlenmod(char *form, const char *lenmod)
static int gmatch(lua_State *L)
LUAMOD_API int luaopen_string(lua_State *L)
static int gmatch_aux(lua_State *L)
static int str_dump(lua_State *L)
static int str_pack(lua_State *L)
static int str_packsize(lua_State *L)
static int str_rep(lua_State *L)
static int push_captures(MatchState *ms, const char *s, const char *e)
static const char * matchbalance(MatchState *ms, const char *s, const char *p)
static void add_value(MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr)
static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
static KOption getoption(Header *h, const char **fmt, int *size)
static int str_format(lua_State *L)
static const char * min_expand(MatchState *ms, const char *s, const char *p, const char *ep)
static int str_byte(lua_State *L)
static int writer(lua_State *L, const void *b, size_t size, void *B)
static const luaL_Reg strlib[]
static const char * classend(MatchState *ms, const char *p)
static const char * start_capture(MatchState *ms, const char *s, const char *p, int what)
#define lua_remove(L, idx)
#define lua_getlocaledecpoint()
#define l_sprintf(s, sz, f, i)
#define LUA_NUMBER_FRMLEN
#define lua_number2strx(L, b, sz, f, n)
#define lua_newuserdata(L, s)
CURL_EXTERN CURLMcode curl_socket_t s
struct MatchState::@54 capture[LUA_MAXCAPTURES]
char buff[5 *sizeof(lua_Number)]