34 #if !defined(LUA_MAXCAPTURES) 35 #define LUA_MAXCAPTURES 32 40 #define uchar(c) ((unsigned char)(c)) 47 #define MAX_SIZET ((size_t)(~(size_t)0)) 50 (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX)) 78 else return len + (size_t)pos + 1;
96 else return len + (size_t)pos + 1;
117 for (i = 0; i < l; i++)
131 p[i] = tolower(
uchar(s[i]));
144 p[i] = toupper(
uchar(s[i]));
156 else if (l + lsep < l || l + lsep >
MAXSIZE / n)
157 return luaL_error(L,
"resulting string too large");
159 size_t totallen = (size_t)n * l + (
size_t)(n - 1) * lsep;
163 memcpy(p, s, l *
sizeof(
char)); p += l;
165 memcpy(p, sep, lsep *
sizeof(
char));
169 memcpy(p, s, l *
sizeof(
char));
183 if (posi > pose)
return 0;
184 if (pose - posi >= (
size_t)INT_MAX)
185 return luaL_error(L,
"string slice too long");
186 n = (int)(pose - posi) + 1;
199 for (i=1; i<=n; i++) {
239 return luaL_error(L,
"unable to dump given function");
252 #if defined(LUA_NOCVTS2N) 279 luaL_error(L,
"attempt to %s a '%s' with a '%s'", mtname + 2,
352 #define CAP_UNFINISHED (-1) 353 #define CAP_POSITION (-2) 371 static const char *
match (
MatchState *ms,
const char *s,
const char *p);
375 #if !defined(MAXCCALLS) 376 #define MAXCCALLS 200 381 #define SPECIALS "^$*+?.([%-" 387 return luaL_error(ms->
L,
"invalid capture index %%%d", l + 1);
393 int level = ms->
level;
394 for (level--; level>=0; level--)
396 return luaL_error(ms->
L,
"invalid pattern capture");
404 luaL_error(ms->
L,
"malformed pattern (ends with '%%')");
411 luaL_error(ms->
L,
"malformed pattern (missing ']')");
412 if (*(p++) ==
L_ESC && p < ms->p_end)
426 switch (tolower(cl)) {
427 case 'a' : res = isalpha(c);
break;
428 case 'c' : res = iscntrl(c);
break;
429 case 'd' : res = isdigit(c);
break;
430 case 'g' : res = isgraph(c);
break;
431 case 'l' : res = islower(c);
break;
432 case 'p' : res = ispunct(c);
break;
433 case 's' : res = isspace(c);
break;
434 case 'u' : res = isupper(c);
break;
435 case 'w' : res = isalnum(c);
break;
436 case 'x' : res = isxdigit(c);
break;
437 case 'z' : res = (c == 0);
break;
438 default:
return (cl == c);
440 return (islower(cl) ? res : !res);
456 else if ((*(p+1) ==
'-') && (p+2 < ec)) {
461 else if (
uchar(*p) == c)
return sig;
477 default:
return (
uchar(*p) == c);
485 if (p >= ms->
p_end - 1)
486 luaL_error(ms->
L,
"malformed pattern (missing arguments to '%%b')");
487 if (*s != *p)
return NULL;
492 while (++s < ms->src_end) {
494 if (--cont == 0)
return s+1;
496 else if (*s == b) cont++;
504 const char *p,
const char *ep) {
510 const char *res =
match(ms, (s+i), ep+1);
519 const char *p,
const char *ep) {
521 const char *res =
match(ms, s, ep+1);
532 const char *p,
int what) {
534 int level = ms->
level;
539 if ((res=
match(ms, s, p)) == NULL)
550 if ((res =
match(ms, s, p)) == NULL)
560 if ((
size_t)(ms->
src_end-s) >= len &&
571 if (p != ms->
p_end) {
585 if ((p + 1) != ms->
p_end)
587 s = (s == ms->
src_end) ? s : NULL;
600 const char *ep;
char previous;
603 luaL_error(ms->
L,
"missing '[' after '%%f' in pattern");
605 previous = (s == ms->
src_init) ?
'\0' : *(s - 1);
613 case '0':
case '1':
case '2':
case '3':
614 case '4':
case '5':
case '6':
case '7':
615 case '8':
case '9': {
630 if (*ep ==
'*' || *ep ==
'?' || *ep ==
'-') {
631 p = ep + 1;
goto init;
640 if ((res =
match(ms, s + 1, ep + 1)) != NULL)
643 p = ep + 1;
goto init;
657 s++; p = ep;
goto init;
670 static const char *
lmemfind (
const char *s1,
size_t l1,
671 const char *s2,
size_t l2) {
672 if (l2 == 0)
return s1;
673 else if (l2 > l1)
return NULL;
678 while (l1 > 0 && (init = (
const char *)memchr(s1, *s2, l1)) != NULL) {
680 if (memcmp(init, s2+1, l2) == 0)
700 const char *e,
const char **cap) {
701 if (i >= ms->
level) {
703 luaL_error(ms->
L,
"invalid capture index %%%d", i + 1);
734 int nlevels = (ms->
level == 0 && s) ? 1 : ms->
level;
736 for (i = 0; i < nlevels; i++)
748 upto += strlen(p + upto) + 1;
755 const char *s,
size_t ls,
const char *p,
size_t lp) {
782 const char *s2 =
lmemfind(s + init, ls - init, p, lp);
791 const char *s1 = s +
init;
792 int anchor = (*p ==
'^');
800 if ((res=
match(&ms, s1, p)) != NULL) {
809 }
while (s1++ < ms.
src_end && !anchor);
839 for (src = gm->
src; src <= gm->ms.src_end; src++) {
874 while ((p = (
char *)memchr(news,
L_ESC, l)) != NULL) {
881 else if (isdigit(
uchar(*p))) {
904 const char *e,
int tr) {
930 return luaL_error(L,
"invalid replacement value (a %s)",
943 const char *lastmatch = NULL;
946 int anchor = (*p ==
'^');
953 "string/function/table");
962 if ((e =
match(&ms, src, p)) != NULL && e != lastmatch) {
964 changed =
add_value(&ms, &b, src, e, tr) | changed;
992 #if !defined(lua_number2strx) 998 #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char)) 1007 #define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1) 1016 buff[n] = (d < 10 ? d +
'0' : d - 10 +
'a');
1021 static int num2straux (
char *buff,
int sz,
lua_Number x) {
1037 buff[n++] =
'0'; buff[n++] =
'x';
1038 m = adddigit(buff, n++, m * (1 << L_NBFD));
1043 m = adddigit(buff, n++, m * 16);
1046 n +=
l_sprintf(buff + n, sz - n,
"p%+d", e);
1055 int n = num2straux(buff, sz, x);
1056 if (fmt[SIZELENMOD] ==
'A') {
1058 for (i = 0; i < n; i++)
1059 buff[i] = toupper(
uchar(buff[i]));
1061 else if (fmt[SIZELENMOD] !=
'a')
1062 return luaL_error(L,
"modifiers for format '%%a'/'%%A' not implemented");
1075 #define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP)) 1086 #define MAX_ITEM 120 1090 #if !defined(L_FMTFLAGS) 1091 #define L_FMTFLAGS "-+ #0" 1098 #define MAX_FORMAT 32 1104 if (*s ==
'"' || *s ==
'\\' || *s ==
'\n') {
1108 else if (iscntrl(
uchar(*s))) {
1110 if (!isdigit(
uchar(*(s+1))))
1142 if (memchr(buff,
'.', nb) == NULL) {
1144 char *ppoint = (
char *)memchr(buff, point, nb);
1145 if (ppoint) *ppoint =
'.';
1169 const char *
format = (n == LUA_MININTEGER)
1170 ?
"0x%" LUA_INTEGER_FRMLEN
"x" 1190 const char *p = strfrmt;
1191 while (*p !=
'\0' && strchr(
L_FMTFLAGS, *p) != NULL) p++;
1192 if ((
size_t)(p - strfrmt) >=
sizeof(
L_FMTFLAGS)/
sizeof(
char))
1193 luaL_error(L,
"invalid format (repeated flags)");
1194 if (isdigit(
uchar(*p))) p++;
1195 if (isdigit(
uchar(*p))) p++;
1198 if (isdigit(
uchar(*p))) p++;
1199 if (isdigit(
uchar(*p))) p++;
1201 if (isdigit(
uchar(*p)))
1202 luaL_error(L,
"invalid format (width or precision too long)");
1204 memcpy(form, strfrmt, ((p - strfrmt) + 1) *
sizeof(
char));
1205 form += (p - strfrmt) + 1;
1215 size_t l = strlen(form);
1216 size_t lm = strlen(lenmod);
1217 char spec = form[l - 1];
1218 strcpy(form + l - 1, lenmod);
1219 form[l + lm - 1] = spec;
1220 form[l + lm] =
'\0';
1229 const char *strfrmt_end = strfrmt+sfl;
1232 while (strfrmt < strfrmt_end) {
1233 if (*strfrmt !=
L_ESC)
1235 else if (*++strfrmt ==
L_ESC)
1245 switch (*strfrmt++) {
1251 case 'o':
case 'u':
case 'x':
case 'X': {
1266 case 'e':
case 'E':
case 'g':
case 'G': {
1276 form[strlen(form) - 1] =
's';
1282 if (form[2] !=
'\0')
1283 return luaL_error(L,
"specifier '%%q' cannot have modifiers");
1290 if (form[2] ==
'\0')
1293 luaL_argcheck(L, l == strlen(s), arg,
"string contains zeros");
1294 if (!strchr(form,
'.') && l >= 100) {
1306 return luaL_error(L,
"invalid conversion '%s' to 'format'", form);
1328 #if !defined(LUAL_PACKPADBYTE) 1329 #define LUAL_PACKPADBYTE 0x00 1333 #define MAXINTSIZE 16 1339 #define MC ((1 << NB) - 1) 1342 #define SZINT ((int)sizeof(lua_Integer)) 1346 static const union {
1358 #define MAXALIGN (offsetof(struct cD, u)) 1402 static int digit (
int c) {
return '0' <= c && c <=
'9'; }
1404 static int getnum (
const char **fmt,
int df) {
1410 a = a*10 + (*((*fmt)++) -
'0');
1422 int sz =
getnum(fmt, df);
1424 return luaL_error(h->
L,
"integral size (%d) out of limits [1,%d]",
1444 int opt = *((*fmt)++);
1447 case 'b': *size =
sizeof(char);
return Kint;
1448 case 'B': *size =
sizeof(char);
return Kuint;
1449 case 'h': *size =
sizeof(short);
return Kint;
1450 case 'H': *size =
sizeof(short);
return Kuint;
1451 case 'l': *size =
sizeof(long);
return Kint;
1452 case 'L': *size =
sizeof(long);
return Kuint;
1455 case 'T': *size =
sizeof(size_t);
return Kuint;
1456 case 'f': *size =
sizeof(float);
return Kfloat;
1457 case 'd': *size =
sizeof(double);
return Kfloat;
1465 luaL_error(h->
L,
"missing size for format option 'c'");
1467 case 'z':
return Kzstr;
1468 case 'x': *size = 1;
return Kpadding;
1475 default:
luaL_error(h->
L,
"invalid format option '%c'", opt);
1491 const char **fmt,
int *psize,
int *ntoalign) {
1495 if (**fmt ==
'\0' ||
getoption(h, fmt, &align) ==
Kchar || align == 0)
1498 if (align <= 1 || opt ==
Kchar)
1503 if ((align & (align - 1)) != 0)
1504 luaL_argerror(h->
L, 1,
"format asks for alignment not power of 2");
1505 *ntoalign = (align - (int)(totalsize & (align - 1))) & (align - 1);
1518 int islittle,
int size,
int neg) {
1521 buff[islittle ? 0 : size - 1] = (char)(n &
MC);
1522 for (i = 1; i < size; i++) {
1524 buff[islittle ? i : size - 1 - i] = (char)(n & MC);
1526 if (neg && size >
SZINT) {
1527 for (i =
SZINT; i < size; i++)
1528 buff[islittle ? i : size - 1 - i] = (
char)
MC;
1539 int size,
int islittle) {
1542 *(dest++) = *(src++);
1547 *(dest--) = *(src++);
1557 size_t totalsize = 0;
1561 while (*fmt !=
'\0') {
1564 totalsize += ntoalign + size;
1565 while (ntoalign-- > 0)
1573 luaL_argcheck(L, -lim <= n && n < lim, arg,
"integer overflow");
1582 arg,
"unsigned overflow");
1590 if (size ==
sizeof(u.
f)) u.
f = (float)n;
1591 else if (size ==
sizeof(u.
d)) u.
d = (
double)n;
1602 "string longer than given size");
1604 while (len++ < (
size_t)size)
1612 len < ((
size_t)1 << (size *
NB)),
1613 arg,
"string length does not fit in given size");
1622 luaL_argcheck(L, strlen(s) == len, arg,
"string contains zeros");
1625 totalsize += len + 1;
1642 size_t totalsize = 0;
1644 while (*fmt !=
'\0') {
1648 "variable-length format");
1651 "format result too large");
1668 int islittle,
int size,
int issigned) {
1672 for (i = limit - 1; i >= 0; i--) {
1674 res |= (
lua_Unsigned)(
unsigned char)str[islittle ? i : size - 1 - i];
1679 res = ((res ^ mask) - mask);
1682 else if (size >
SZINT) {
1684 for (i = limit; i < size; i++) {
1685 if ((
unsigned char)str[islittle ? i : size - 1 - i] != mask)
1686 luaL_error(L,
"%d-byte integer does not fit into Lua Integer", size);
1700 luaL_argcheck(L, pos <= ld, 3,
"initial position out of string");
1702 while (*fmt !=
'\0') {
1706 "data string too short");
1735 luaL_argcheck(L, len <= ld - pos - size, 2,
"data string too short");
1741 size_t len = (int)strlen(data + pos);
1743 "unfinished string for format 'z'");
#define luaL_addsize(B, s)
LUA_API int lua_isstring(lua_State *L, int idx)
static int str_format(lua_State *L)
static void copywithendian(volatile char *dest, volatile const char *src, int size, int islittle)
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data, int strip)
LUA_API int lua_isinteger(lua_State *L, int idx)
static const union @35 nativeendian
static const char * scanformat(lua_State *L, const char *strfrmt, char *form)
static int nospecials(const char *p, size_t l)
FMT_INLINE std::basic_string< Char > format(const S &format_str, Args &&...args)
LUA_API void lua_pushnil(lua_State *L)
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
LUA_API const void * lua_topointer(lua_State *L, int idx)
static const char * match_capture(MatchState *ms, const char *s, int l)
static size_t get_onecapture(MatchState *ms, int i, const char *s, const char *e, const char **cap)
static int str_char(lua_State *L)
static const luaL_Reg stringmetamethods[]
static int arith_sub(lua_State *L)
struct MatchState::@36 capture[LUA_MAXCAPTURES]
static const char * max_expand(MatchState *ms, const char *s, const char *p, const char *ep)
LUA_API int lua_setmetatable(lua_State *L, int objindex)
LUA_API int lua_toboolean(lua_State *L, int idx)
static size_t posrelatI(lua_Integer pos, size_t len)
static void createmetatable(lua_State *L)
LUA_API void * lua_touserdata(lua_State *L, int idx)
#define luaL_argexpected(L, cond, arg, tname)
static int getnum(const char **fmt, int df)
static int arith_div(lua_State *L)
LUALIB_API void luaL_pushresultsize(luaL_Buffer *B, size_t sz)
static void addquoted(luaL_Buffer *b, const char *s, size_t len)
static int tonum(lua_State *L, int arg)
static int add_value(MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
static int writer(lua_State *L, const void *b, size_t size, void *ud)
static void addliteral(lua_State *L, luaL_Buffer *b, int arg)
static const char * end_capture(MatchState *ms, const char *s, const char *p)
#define lua_tointeger(L, i)
static int arith_idiv(lua_State *L)
static const char * start_capture(MatchState *ms, const char *s, const char *p, int what)
static int arith_add(lua_State *L)
#define lua_tonumber(L, i)
#define luaL_typename(L, i)
static KOption getdetails(Header *h, size_t totalsize, const char **fmt, int *psize, int *ntoalign)
#define lua_number2strx(L, b, sz, f, n)
static const char * matchbalance(MatchState *ms, const char *s, const char *p)
static int str_dump(lua_State *L)
static int gmatch_aux(lua_State *L)
static int str_match(lua_State *L)
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
static const luaL_Reg strlib[]
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
static int singlematch(MatchState *ms, const char *s, const char *p, const char *ep)
static int str_gsub(lua_State *L)
#define luaL_newlibtable(L, l)
LUA_API void lua_pushvalue(lua_State *L, int idx)
static int match_class(int c, int cl)
char buff[5 *sizeof(lua_Number)]
LUA_API void lua_settop(lua_State *L, int idx)
static int arith_pow(lua_State *L)
#define lua_upvalueindex(i)
static int str_find(lua_State *L)
static int check_capture(MatchState *ms, int l)
static lua_Integer unpackint(lua_State *L, const char *str, int islittle, int size, int issigned)
struct MatchState MatchState
LUALIB_API const char * luaL_checklstring(lua_State *L, int arg, size_t *len)
LUALIB_API char * luaL_buffinitsize(lua_State *L, luaL_Buffer *B, size_t sz)
static int push_captures(MatchState *ms, const char *s, const char *e)
static void trymt(lua_State *L, const char *mtname)
#define l_sprintf(s, sz, f, i)
FMT_CONSTEXPR bool find(Ptr first, Ptr last, T value, Ptr &out)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
static int str_packsize(lua_State *L)
static const char * lmemfind(const char *s1, size_t l1, const char *s2, size_t l2)
static void prepstate(MatchState *ms, lua_State *L, const char *s, size_t ls, const char *p, size_t lp)
static int arith_mul(lua_State *L)
struct GMatchState GMatchState
static const char * min_expand(MatchState *ms, const char *s, const char *p, const char *ep)
#define luaL_addchar(B, c)
LUA_API void lua_arith(lua_State *L, int op)
static size_t getendpos(lua_State *L, int arg, lua_Integer def, size_t len)
LUALIB_API const char * luaL_optlstring(lua_State *L, int arg, const char *def, size_t *len)
static int str_upper(lua_State *L)
static int capture_to_close(MatchState *ms)
#define lua_pushliteral(L, s)
LUA_API size_t lua_stringtonumber(lua_State *L, const char *s)
LUA_API const char * lua_tolstring(lua_State *L, int idx, size_t *len)
static int arith_mod(lua_State *L)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *msg)
static void initheader(lua_State *L, Header *h)
LUALIB_API const char * luaL_tolstring(lua_State *L, int idx, size_t *len)
#define lua_getlocaledecpoint()
static int str_find_aux(lua_State *L, int find)
static int quotefloat(lua_State *L, char *buff, lua_Number n)
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
detail::named_arg< Char, T > arg(const Char *name, const T &arg)
LUA_API void * lua_newuserdatauv(lua_State *L, size_t size, int nuvalue)
LUAMOD_API int luaopen_string(lua_State *L)
static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
LUA_API int lua_type(lua_State *L, int idx)
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
static int str_pack(lua_State *L)
static void addlenmod(char *form, const char *lenmod)
static int getnumlimit(Header *h, const char **fmt, int df)
static int str_unpack(lua_State *L)
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int arg, lua_Integer def)
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
static int matchbracketclass(int c, const char *p, const char *ec)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
#define luaL_newlib(L, l)
static int str_reverse(lua_State *L)
LUA_UNSIGNED lua_Unsigned
#define lua_call(L, n, r)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
#define luaL_argcheck(L, cond, arg, extramsg)
static KOption getoption(Header *h, const char **fmt, int *size)
static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
#define LUA_NUMBER_FRMLEN
static int str_len(lua_State *L)
static int gmatch(lua_State *L)
static const char * classend(MatchState *ms, const char *p)
static int str_sub(lua_State *L)
LUALIB_API void luaL_checktype(lua_State *L, int arg, int t)
static int arith_unm(lua_State *L)
static const char * match(MatchState *ms, const char *s, const char *p)
static int str_lower(lua_State *L)
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int arg)
#define lua_insert(L, idx)
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
static int arith(lua_State *L, int op, const char *mtname)
static void reprepstate(MatchState *ms)
LUALIB_API int luaL_getmetafield(lua_State *L, int obj, const char *event)
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
static void packint(luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg)
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
static int str_rep(lua_State *L)
static int str_byte(lua_State *L)
LUA_API int lua_gettable(lua_State *L, int idx)
LUA_API int lua_gettop(lua_State *L)
#define luaL_checkstring(L, n)