Go to the documentation of this file.
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]));
157 else if (l_unlikely(l + lsep < l || l + lsep >
MAXSIZE / n))
158 return luaL_error(L,
"resulting string too large");
160 size_t totallen = (
size_t)n * l + (
size_t)(n - 1) * lsep;
164 memcpy(p,
s, l *
sizeof(
char)); p += l;
166 memcpy(p, sep, lsep *
sizeof(
char));
170 memcpy(p,
s, l *
sizeof(
char));
184 if (posi > pose)
return 0;
185 if (l_unlikely(pose - posi >= (
size_t)INT_MAX))
186 return luaL_error(L,
"string slice too long");
187 n = (
int)(pose - posi) + 1;
200 for (i=1; i<=n; i++) {
240 return luaL_error(L,
"unable to dump given function");
253 #if defined(LUA_NOCVTS2N)
281 luaL_error(L,
"attempt to %s a '%s' with a '%s'", mtname + 2,
354 #define CAP_UNFINISHED (-1)
355 #define CAP_POSITION (-2)
373 static const char *
match (
MatchState *ms,
const char *s,
const char *p);
377 #if !defined(MAXCCALLS)
378 #define MAXCCALLS 200
383 #define SPECIALS "^$*+?.([%-"
388 if (l_unlikely(l < 0 || l >= ms->
level ||
390 return luaL_error(ms->
L,
"invalid capture index %%%d", l + 1);
396 int level = ms->
level;
397 for (level--; level>=0; level--)
399 return luaL_error(ms->
L,
"invalid pattern capture");
406 if (l_unlikely(p == ms->
p_end))
407 luaL_error(ms->
L,
"malformed pattern (ends with '%%')");
413 if (l_unlikely(p == ms->
p_end))
414 luaL_error(ms->
L,
"malformed pattern (missing ']')");
415 if (*(p++) ==
L_ESC && p < ms->p_end)
429 switch (tolower(cl)) {
430 case 'a' : res = isalpha(c);
break;
431 case 'c' : res = iscntrl(c);
break;
432 case 'd' : res = isdigit(c);
break;
433 case 'g' : res = isgraph(c);
break;
434 case 'l' : res = islower(c);
break;
435 case 'p' : res = ispunct(c);
break;
436 case 's' : res = isspace(c);
break;
437 case 'u' : res = isupper(c);
break;
438 case 'w' : res = isalnum(c);
break;
439 case 'x' : res = isxdigit(c);
break;
440 case 'z' : res = (c == 0);
break;
441 default:
return (cl == c);
443 return (islower(cl) ? res : !res);
459 else if ((*(p+1) ==
'-') && (p+2 < ec)) {
464 else if (
uchar(*p) == c)
return sig;
480 default:
return (
uchar(*p) == c);
488 if (l_unlikely(p >= ms->
p_end - 1))
489 luaL_error(ms->
L,
"malformed pattern (missing arguments to '%%b')");
490 if (*
s != *p)
return NULL;
495 while (++s < ms->src_end) {
497 if (--cont == 0)
return s+1;
499 else if (*
s == b) cont++;
507 const char *p,
const char *ep) {
513 const char *res =
match(ms, (
s+i), ep+1);
522 const char *p,
const char *ep) {
524 const char *res =
match(ms,
s, ep+1);
535 const char *p,
int what) {
537 int level = ms->
level;
542 if ((res=
match(ms,
s, p)) == NULL)
553 if ((res =
match(ms,
s, p)) == NULL)
563 if ((
size_t)(ms->
src_end-
s) >= len &&
574 if (p != ms->
p_end) {
588 if ((p + 1) != ms->
p_end)
603 const char *ep;
char previous;
605 if (l_unlikely(*p !=
'['))
606 luaL_error(ms->
L,
"missing '[' after '%%f' in pattern");
608 previous = (
s == ms->
src_init) ?
'\0' : *(
s - 1);
616 case '0':
case '1':
case '2':
case '3':
617 case '4':
case '5':
case '6':
case '7':
618 case '8':
case '9': {
633 if (*ep ==
'*' || *ep ==
'?' || *ep ==
'-') {
634 p = ep + 1;
goto init;
643 if ((res =
match(ms,
s + 1, ep + 1)) != NULL)
646 p = ep + 1;
goto init;
660 s++; p = ep;
goto init;
673 static const char *
lmemfind (
const char *s1,
size_t l1,
674 const char *s2,
size_t l2) {
675 if (l2 == 0)
return s1;
676 else if (l2 > l1)
return NULL;
681 while (l1 > 0 && (
init = (
const char *)memchr(s1, *s2, l1)) != NULL) {
683 if (memcmp(
init, s2+1, l2) == 0)
703 const char *e,
const char **cap) {
704 if (i >= ms->
level) {
705 if (l_unlikely(i != 0))
706 luaL_error(ms->
L,
"invalid capture index %%%d", i + 1);
737 int nlevels = (ms->
level == 0 &&
s) ? 1 : ms->
level;
739 for (i = 0; i < nlevels; i++)
751 upto += strlen(p + upto) + 1;
758 const char *s,
size_t ls,
const char *p,
size_t lp) {
794 const char *s1 =
s +
init;
803 if ((res=
match(&ms, s1, p)) != NULL) {
842 for (src = gm->
src; src <= gm->ms.src_end; src++) {
877 while ((p = (
char *)memchr(news,
L_ESC, l)) != NULL) {
884 else if (isdigit(
uchar(*p))) {
907 const char *e,
int tr) {
933 return luaL_error(L,
"invalid replacement value (a %s)",
946 const char *lastmatch = NULL;
956 "string/function/table");
965 if ((e =
match(&ms, src, p)) != NULL && e != lastmatch) {
967 changed =
add_value(&ms, &b, src, e, tr) | changed;
995 #if !defined(lua_number2strx)
1001 #define SIZELENMOD (sizeof(LUA_NUMBER_FRMLEN)/sizeof(char))
1010 #define L_NBFD ((l_floatatt(MANT_DIG) - 1)%4 + 1)
1019 buff[n] = (
d < 10 ?
d +
'0' :
d - 10 +
'a');
1024 static int num2straux (
char *buff,
int sz,
lua_Number x) {
1027 return l_sprintf(buff, sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)
x);
1030 return l_sprintf(buff, sz, LUA_NUMBER_FMT
"x0p+0", (LUAI_UACNUMBER)
x);
1040 buff[n++] =
'0'; buff[n++] =
'x';
1041 m = adddigit(buff, n++, m * (1 << L_NBFD));
1046 m = adddigit(buff, n++, m * 16);
1049 n +=
l_sprintf(buff + n, sz - n,
"p%+d", e);
1058 int n = num2straux(buff, sz,
x);
1059 if (fmt[SIZELENMOD] ==
'A') {
1061 for (i = 0; i < n; i++)
1062 buff[i] = toupper(
uchar(buff[i]));
1064 else if (l_unlikely(fmt[SIZELENMOD] !=
'a'))
1065 return luaL_error(L,
"modifiers for format '%%a'/'%%A' not implemented");
1078 #define MAX_ITEMF (110 + l_floatatt(MAX_10_EXP))
1089 #define MAX_ITEM 120
1093 #if !defined(L_FMTFLAGS)
1094 #define L_FMTFLAGS "-+ #0"
1101 #define MAX_FORMAT 32
1107 if (*
s ==
'"' || *
s ==
'\\' || *
s ==
'\n') {
1111 else if (iscntrl(
uchar(*
s))) {
1113 if (!isdigit(
uchar(*(
s+1))))
1143 "%" LUA_NUMBER_FRMLEN
"a", n);
1145 if (memchr(buff,
'.', nb) == NULL) {
1147 char *ppoint = (
char *)memchr(buff, point, nb);
1148 if (ppoint) *ppoint =
'.';
1172 const char *
format = (n == LUA_MININTEGER)
1173 ?
"0x%" LUA_INTEGER_FRMLEN
"x"
1193 const char *p = strfrmt;
1194 while (*p !=
'\0' && strchr(
L_FMTFLAGS, *p) != NULL) p++;
1195 if ((
size_t)(p - strfrmt) >=
sizeof(
L_FMTFLAGS)/
sizeof(
char))
1196 luaL_error(L,
"invalid format (repeated flags)");
1197 if (isdigit(
uchar(*p))) p++;
1198 if (isdigit(
uchar(*p))) p++;
1201 if (isdigit(
uchar(*p))) p++;
1202 if (isdigit(
uchar(*p))) p++;
1204 if (isdigit(
uchar(*p)))
1205 luaL_error(L,
"invalid format (width or precision too long)");
1207 memcpy(form, strfrmt, ((p - strfrmt) + 1) *
sizeof(
char));
1208 form += (p - strfrmt) + 1;
1218 size_t l = strlen(form);
1219 size_t lm = strlen(lenmod);
1220 char spec = form[l - 1];
1221 strcpy(form + l - 1, lenmod);
1222 form[l + lm - 1] = spec;
1223 form[l + lm] =
'\0';
1232 const char *strfrmt_end = strfrmt+sfl;
1235 while (strfrmt < strfrmt_end) {
1236 if (*strfrmt !=
L_ESC)
1238 else if (*++strfrmt ==
L_ESC)
1248 switch (*strfrmt++) {
1254 case 'o':
case 'u':
case 'x':
case 'X': {
1269 case 'e':
case 'E':
case 'g':
case 'G': {
1272 nb =
l_sprintf(buff, maxitem, form, (LUAI_UACNUMBER)n);
1279 form[strlen(form) - 1] =
's';
1285 if (form[2] !=
'\0')
1286 return luaL_error(L,
"specifier '%%q' cannot have modifiers");
1293 if (form[2] ==
'\0')
1297 if (!strchr(form,
'.') && l >= 100) {
1309 return luaL_error(L,
"invalid conversion '%s' to 'format'", form);
1331 #if !defined(LUAL_PACKPADBYTE)
1332 #define LUAL_PACKPADBYTE 0x00
1336 #define MAXINTSIZE 16
1342 #define MC ((1 << NB) - 1)
1345 #define SZINT ((int)sizeof(lua_Integer))
1349 static const union {
1361 #define MAXALIGN (offsetof(struct cD, u))
1396 static int digit (
int c) {
return '0' <= c && c <=
'9'; }
1398 static int getnum (
const char **fmt,
int df) {
1404 a = a*10 + (*((*fmt)++) -
'0');
1416 int sz =
getnum(fmt, df);
1418 return luaL_error(h->
L,
"integral size (%d) out of limits [1,%d]",
1438 int opt = *((*fmt)++);
1441 case 'b': *
size =
sizeof(char);
return Kint;
1442 case 'B': *
size =
sizeof(char);
return Kuint;
1443 case 'h': *
size =
sizeof(short);
return Kint;
1444 case 'H': *
size =
sizeof(short);
return Kuint;
1445 case 'l': *
size =
sizeof(long);
return Kint;
1446 case 'L': *
size =
sizeof(long);
return Kuint;
1450 case 'f': *
size =
sizeof(float);
return Kfloat;
1458 if (l_unlikely(*
size == -1))
1459 luaL_error(h->
L,
"missing size for format option 'c'");
1461 case 'z':
return Kzstr;
1469 default:
luaL_error(h->
L,
"invalid format option '%c'", opt);
1485 const char **fmt,
int *psize,
int *ntoalign) {
1498 luaL_argerror(h->
L, 1,
"format asks for alignment not power of 2");
1512 int islittle,
int size,
int neg) {
1515 buff[islittle ? 0 :
size - 1] = (char)(n &
MC);
1516 for (i = 1; i <
size; i++) {
1518 buff[islittle ? i :
size - 1 - i] = (char)(n &
MC);
1522 buff[islittle ? i :
size - 1 - i] = (
char)
MC;
1533 int size,
int islittle) {
1539 *(
dest--) = *(src++);
1549 size_t totalsize = 0;
1553 while (*fmt !=
'\0') {
1556 totalsize += ntoalign +
size;
1557 while (ntoalign-- > 0)
1574 arg,
"unsigned overflow");
1606 "string longer than given size");
1608 while (len++ < (
size_t)
size)
1616 len < ((
size_t)1 << (
size *
NB)),
1617 arg,
"string length does not fit in given size");
1629 totalsize += len + 1;
1646 size_t totalsize = 0;
1648 while (*fmt !=
'\0') {
1652 "variable-length format");
1655 "format result too large");
1672 int islittle,
int size,
int issigned) {
1676 for (i = limit - 1; i >= 0; i--) {
1683 res = ((res ^ mask) - mask);
1688 for (i = limit; i <
size; i++) {
1689 if (l_unlikely((
unsigned char)str[islittle ? i :
size - 1 - i] != mask))
1690 luaL_error(L,
"%d-byte integer does not fit into Lua Integer",
size);
1704 luaL_argcheck(L, pos <= ld, 3,
"initial position out of string");
1706 while (*fmt !=
'\0') {
1710 "data string too short");
1753 size_t len = strlen(
data + pos);
1755 "unfinished string for format 'z'");
static size_t posrelatI(lua_Integer pos, size_t len)
static int arith(lua_State *L, int op, const char *mtname)
static void createmetatable(lua_State *L)
static int getnum(const char **fmt, int df)
#define lua_pushliteral(L, s)
static void addquoted(luaL_Buffer *b, const char *s, size_t len)
LUALIB_API int luaL_getmetafield(lua_State *L, int obj, const char *event)
LUA_UNSIGNED lua_Unsigned
LUALIB_API void luaL_addstring(luaL_Buffer *B, const char *s)
static int str_char(lua_State *L)
static void packint(luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg)
static int arith_idiv(lua_State *L)
static int arith_add(lua_State *L)
static KOption getdetails(Header *h, size_t totalsize, const char **fmt, int *psize, int *ntoalign)
static int str_byte(lua_State *L)
static int str_dump(lua_State *L)
static int arith_div(lua_State *L)
#define luaL_checkstring(L, n)
LUALIB_API void luaL_pushresultsize(luaL_Buffer *B, size_t sz)
span_CONFIG_SIZE_TYPE size_t
LUALIB_API void luaL_buffinit(lua_State *L, luaL_Buffer *B)
static const char * match_capture(MatchState *ms, const char *s, int l)
static const char * end_capture(MatchState *ms, const char *s, const char *p)
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
static const luaL_Reg stringmetamethods[]
static int str_format(lua_State *L)
static int arith_sub(lua_State *L)
static const char * max_expand(MatchState *ms, const char *s, const char *p, const char *ep)
#define luaL_newlibtable(L, l)
#define luaL_typename(L, i)
static int gmatch_aux(lua_State *L)
static int str_match(lua_State *L)
auto arg(const Char *name, const T &arg) -> detail::named_arg< Char, T >
LUA_API int lua_gettable(lua_State *L, int idx)
static int add_value(MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr)
LUALIB_API void luaL_addlstring(luaL_Buffer *B, const char *s, size_t l)
static int writer(lua_State *L, const void *b, size_t size, void *ud)
static lua_Integer unpackint(lua_State *L, const char *str, int islittle, int size, int issigned)
static int singlematch(MatchState *ms, const char *s, const char *p, const char *ep)
LUA_API int lua_isstring(lua_State *L, int idx)
static int str_gsub(lua_State *L)
static const char * start_capture(MatchState *ms, const char *s, const char *p, int what)
LUA_API int lua_type(lua_State *L, int idx)
const LUALIB_API char * luaL_checklstring(lua_State *L, int arg, size_t *len)
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
LUA_API int lua_setmetatable(lua_State *L, int objindex)
#define lua_number2strx(L, b, sz, f, n)
static void copywithendian(char *dest, const char *src, int size, int islittle)
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
LUA_API int lua_dump(lua_State *L, lua_Writer writer, void *data, int strip)
static int str_find(lua_State *L)
LUA_API int lua_isinteger(lua_State *L, int idx)
LUALIB_API void luaL_pushresult(luaL_Buffer *B)
#define luaL_argexpected(L, cond, arg, tname)
static int tonum(lua_State *L, int arg)
const LUALIB_API char * luaL_optlstring(lua_State *L, int arg, const char *def, size_t *len)
static void addliteral(lua_State *L, luaL_Buffer *b, int arg)
static int check_capture(MatchState *ms, int l)
LUA_API void lua_arith(lua_State *L, int op)
span_constexpr std::size_t size(span< T, Extent > const &spn)
static const char * matchbalance(MatchState *ms, const char *s, const char *p)
static int arith_mod(lua_State *L)
#define lua_insert(L, idx)
static void prepstate(MatchState *ms, lua_State *L, const char *s, size_t ls, const char *p, size_t lp)
#define lua_tointeger(L, i)
LUA_API void lua_setfield(lua_State *L, int idx, const char *k)
LUA_API void lua_pushcclosure(lua_State *L, lua_CFunction fn, int n)
static void initheader(lua_State *L, Header *h)
static int arith_mul(lua_State *L)
static const luaL_Reg strlib[]
static int match_class(int c, int cl)
static void trymt(lua_State *L, const char *mtname)
static int arith_pow(lua_State *L)
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
static int str_unpack(lua_State *L)
static const char * lmemfind(const char *s1, size_t l1, const char *s2, size_t l2)
LUALIB_API char * luaL_buffinitsize(lua_State *L, luaL_Buffer *B, size_t sz)
LUA_API void * lua_touserdata(lua_State *L, int idx)
static int push_captures(MatchState *ms, const char *s, const char *e)
#define luaL_argcheck(L, cond, arg, extramsg)
#define luaL_addchar(B, c)
LUA_API int lua_gettop(lua_State *L)
struct GMatchState GMatchState
LUA_API void lua_settop(lua_State *L, int idx)
LUAMOD_API int luaopen_string(lua_State *L)
struct MatchState MatchState
static int str_pack(lua_State *L)
LUA_API void lua_pushvalue(lua_State *L, int idx)
static int getnumlimit(Header *h, const char **fmt, int df)
auto format(const text_style &ts, const S &format_str, const Args &... args) -> std::basic_string< Char >
static int str_packsize(lua_State *L)
LUALIB_API void luaL_checktype(lua_State *L, int arg, int t)
static const union @17 nativeendian
static int str_lower(lua_State *L)
static int matchbracketclass(int c, const char *p, const char *ec)
#define lua_call(L, n, r)
static int str_find_aux(lua_State *L, int find)
#define lua_getlocaledecpoint()
LUALIB_API char * luaL_prepbuffsize(luaL_Buffer *B, size_t sz)
static KOption getoption(Header *h, const char **fmt, int *size)
static const char * min_expand(MatchState *ms, const char *s, const char *p, const char *ep)
static size_t getendpos(lua_State *L, int arg, lua_Integer def, size_t len)
const LUA_API char * lua_pushlstring(lua_State *L, const char *s, size_t len)
static int str_upper(lua_State *L)
static int capture_to_close(MatchState *ms)
static void add_s(MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
static void reprepstate(MatchState *ms)
static int str_len(lua_State *L)
static int gmatch(lua_State *L)
static const char * classend(MatchState *ms, const char *p)
LUALIB_API void luaL_setfuncs(lua_State *L, const luaL_Reg *l, int nup)
LUALIB_API lua_Integer luaL_optinteger(lua_State *L, int arg, lua_Integer def)
LUALIB_API void luaL_checkstack(lua_State *L, int space, const char *msg)
#define luaL_newlib(L, l)
#define lua_tonumber(L, i)
LUALIB_API lua_Number luaL_checknumber(lua_State *L, int arg)
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
static int quotefloat(lua_State *L, char *buff, lua_Number n)
static int str_reverse(lua_State *L)
LUA_API void * lua_newuserdatauv(lua_State *L, size_t size, int nuvalue)
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
void init(const M_string &remappings)
LUA_API size_t lua_stringtonumber(lua_State *L, const char *s)
const LUA_API char * lua_tolstring(lua_State *L, int idx, size_t *len)
static void addlenmod(char *form, const char *lenmod)
LUA_API void lua_pushnil(lua_State *L)
struct MatchState::@18 capture[LUA_MAXCAPTURES]
LUA_API int lua_toboolean(lua_State *L, int idx)
#define l_sprintf(s, sz, f, i)
static const char * match(MatchState *ms, const char *s, const char *p)
#define lua_upvalueindex(i)
LUALIB_API void luaL_addvalue(luaL_Buffer *B)
static const char * scanformat(lua_State *L, const char *strfrmt, char *form)
static int nospecials(const char *p, size_t l)
#define luaL_addsize(B, s)
static int str_sub(lua_State *L)
static int arith_unm(lua_State *L)
static size_t get_onecapture(MatchState *ms, int i, const char *s, const char *e, const char **cap)
const LUALIB_API char * luaL_tolstring(lua_State *L, int idx, size_t *len)
static int str_rep(lua_State *L)
const LUA_API void * lua_topointer(lua_State *L, int idx)
plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:45