27#if !defined(LUA_MAXCAPTURES)
28#define LUA_MAXCAPTURES 32
33#define uchar(c) ((unsigned char)(c))
46static size_t posrelat (ptrdiff_t pos,
size_t len) {
47 if (pos >= 0)
return (
size_t)pos;
48 else if (0u - (
size_t)pos > len)
return 0;
49 else return len - ((size_t)-pos) + 1;
58 if (start < 1) start = 1;
72 for (i = 0; i < l; i++)
86 p[i] = tolower(
uchar(
s[i]));
99 p[i] = toupper(
uchar(
s[i]));
106#define MAXSIZE ((~(size_t)0) >> 1)
114 else if (l + lsep < l || l + lsep >=
MAXSIZE / n)
115 return luaL_error(L,
"resulting string too large");
117 size_t totallen = n * l + (n - 1) * lsep;
121 memcpy(p,
s, l *
sizeof(
char)); p += l;
123 memcpy(p, sep, lsep *
sizeof(
char)); p += lsep;
126 memcpy(p,
s, l *
sizeof(
char));
139 if (posi < 1) posi = 1;
140 if (pose > l) pose = l;
141 if (posi > pose)
return 0;
142 n = (int)(pose - posi + 1);
143 if (posi + n <= pose)
144 return luaL_error(L,
"string slice too long");
157 for (i=1; i<=n; i++) {
180 return luaL_error(L,
"unable to dump given function");
194#define CAP_UNFINISHED (-1)
195#define CAP_POSITION (-2)
217#if !defined(MAXCCALLS)
223#define SPECIALS "^$*+?.([%-"
229 return luaL_error(ms->
L,
"invalid capture index %%%d", l + 1);
235 int level = ms->
level;
236 for (level--; level>=0; level--)
238 return luaL_error(ms->
L,
"invalid pattern capture");
254 if (*(p++) ==
L_ESC && p < ms->p_end)
268 switch (tolower(cl)) {
269 case 'a' : res = isalpha(c);
break;
270 case 'c' : res = iscntrl(c);
break;
271 case 'd' : res = isdigit(c);
break;
272 case 'g' : res = isgraph(c);
break;
273 case 'l' : res = islower(c);
break;
274 case 'p' : res = ispunct(c);
break;
275 case 's' : res = isspace(c);
break;
276 case 'u' : res = isupper(c);
break;
277 case 'w' : res = isalnum(c);
break;
278 case 'x' : res = isxdigit(c);
break;
279 case 'z' : res = (c == 0);
break;
280 default:
return (cl == c);
282 return (islower(cl) ? res : !res);
298 else if ((*(p+1) ==
'-') && (p+2 < ec)) {
303 else if (
uchar(*p) == c)
return sig;
319 default:
return (
uchar(*p) == c);
327 if (p >= ms->
p_end - 1)
329 "(missing arguments to " LUA_QL(
"%%b")
")");
330 if (*
s != *p)
return NULL;
335 while (++s < ms->src_end) {
337 if (--cont == 0)
return s+1;
339 else if (*
s == b) cont++;
347 const char *p,
const char *ep) {
353 const char *res =
match(ms, (
s+i), ep+1);
362 const char *p,
const char *ep) {
364 const char *res =
match(ms,
s, ep+1);
375 const char *p,
int what) {
377 int level = ms->
level;
403 if ((
size_t)(ms->
src_end-
s) >= len &&
414 if (p != ms->
p_end) {
428 if ((p + 1) != ms->
p_end)
443 const char *ep;
char previous;
447 LUA_QL(
"%%f")
" in pattern");
449 previous = (
s == ms->
src_init) ?
'\0' : *(
s - 1);
457 case '0':
case '1':
case '2':
case '3':
458 case '4':
case '5':
case '6':
case '7':
459 case '8':
case '9': {
474 if (*ep ==
'*' || *ep ==
'?' || *ep ==
'-') {
475 p = ep + 1;
goto init;
487 p = ep + 1;
goto init;
501 s++; p = ep;
goto init;
514static const char *
lmemfind (
const char *s1,
size_t l1,
515 const char *s2,
size_t l2) {
516 if (l2 == 0)
return s1;
517 else if (l2 > l1)
return NULL;
522 while (l1 > 0 && (init = (
const char *)memchr(s1, *s2, l1)) !=
NULL) {
524 if (memcmp(init, s2+1, l2) == 0)
538 if (i >= ms->
level) {
557 int nlevels = (ms->
level == 0 &&
s) ? 1 : ms->
level;
559 for (i = 0; i < nlevels; i++)
571 upto += strlen(p + upto) + 1;
582 if (init < 1) init = 1;
583 else if (init > ls + 1) {
590 const char *s2 =
lmemfind(
s + init - 1, ls - init + 1, p, lp);
599 const char *s1 =
s + init - 1;
600 int anchor = (*p ==
'^');
622 }
while (s1++ < ms.
src_end && !anchor);
658 if (e == src) newstart++;
682 for (i = 0; i < l; i++) {
683 if (news[i] !=
L_ESC)
687 if (!isdigit(
uchar(news[i]))) {
688 if (news[i] !=
L_ESC)
690 " in replacement string",
L_ESC);
693 else if (news[i] ==
'0')
705 const char *e,
int tr) {
741 int anchor = (*p ==
'^');
747 "string/function/table expected");
761 e =
match(&ms, src, p);
794#if !defined(LUA_INTFRMLEN)
795#if defined(LUA_USE_LONGLONG)
797#define LUA_INTFRMLEN "ll"
798#define LUA_INTFRM_T long long
802#define LUA_INTFRMLEN "l"
803#define LUA_INTFRM_T long
814#if !defined(LUA_FLTFRMLEN)
816#define LUA_FLTFRMLEN ""
817#define LUA_FLTFRM_T double
830#define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
838 if (*
s ==
'"' || *
s ==
'\\' || *
s ==
'\n') {
842 else if (*
s ==
'\0' || iscntrl(
uchar(*
s))) {
844 if (!isdigit(
uchar(*(
s+1))))
845 sprintf(buff,
"\\%d", (
int)
uchar(*
s));
847 sprintf(buff,
"\\%03d", (
int)
uchar(*
s));
858 const char *p = strfrmt;
859 while (*p !=
'\0' && strchr(
FLAGS, *p) !=
NULL) p++;
860 if ((
size_t)(p - strfrmt) >=
sizeof(
FLAGS)/
sizeof(
char))
861 luaL_error(L,
"invalid format (repeated flags)");
862 if (isdigit(
uchar(*p))) p++;
863 if (isdigit(
uchar(*p))) p++;
866 if (isdigit(
uchar(*p))) p++;
867 if (isdigit(
uchar(*p))) p++;
869 if (isdigit(
uchar(*p)))
870 luaL_error(L,
"invalid format (width or precision too long)");
872 memcpy(form, strfrmt, (p - strfrmt + 1) *
sizeof(
char));
873 form += p - strfrmt + 1;
883 size_t l = strlen(form);
884 size_t lm = strlen(lenmod);
885 char spec = form[l - 1];
886 strcpy(form + l - 1, lenmod);
887 form[l + lm - 1] = spec;
897 const char *strfrmt_end = strfrmt+sfl;
900 while (strfrmt < strfrmt_end) {
901 if (*strfrmt !=
L_ESC)
903 else if (*++strfrmt ==
L_ESC)
912 switch (*strfrmt++) {
917 case 'd':
case 'i': {
922 "not a number in proper range");
924 nb = sprintf(buff, form, ni);
927 case 'o':
case 'u':
case 'x':
case 'X': {
932 "not a non-negative number in proper range");
934 nb = sprintf(buff, form, ni);
937 case 'e':
case 'E':
case 'f':
938#if defined(LUA_USE_AFORMAT)
941 case 'g':
case 'G': {
953 if (!strchr(form,
'.') && l >= 100) {
960 nb = sprintf(buff, form,
s);
967 LUA_QL(
"format"), *(strfrmt - 1));
static size_t find(const char *s, const char *pattern, size_t start)
CURL_EXTERN int void * arg
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_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_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_checkint(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)
static int str_sub(lua_State *L)
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 const char * max_expand(MatchState *ms, const char *s, const char *p, const char *ep)
static void push_onecapture(MatchState *ms, int i, const char *s, const char *e)
static int str_len(lua_State *L)
static int str_find(lua_State *L)
static int str_upper(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)
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 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 addquoted(lua_State *L, luaL_Buffer *b, int arg)
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 size_t posrelat(ptrdiff_t pos, size_t len)
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_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 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_tointeger(L, i)
#define lua_call(L, n, r)
#define lua_replace(L, idx)
CURL_EXTERN CURLMcode curl_socket_t s
struct MatchState::@54 capture[LUA_MAXCAPTURES]