1*61145dc2SMartin Matuska // SPDX-License-Identifier: MIT
2eda14cbcSMatt Macy /*
3eda14cbcSMatt Macy ** $Id: lstrlib.c,v 1.178.1.1 2013/04/12 18:48:47 roberto Exp $
4eda14cbcSMatt Macy ** Standard library for string operations and pattern-matching
5eda14cbcSMatt Macy ** See Copyright Notice in lua.h
6eda14cbcSMatt Macy */
7eda14cbcSMatt Macy
8eda14cbcSMatt Macy
9eda14cbcSMatt Macy #define lstrlib_c
10eda14cbcSMatt Macy #define LUA_LIB
11eda14cbcSMatt Macy
12eda14cbcSMatt Macy #include <sys/lua/lua.h>
13eda14cbcSMatt Macy
14eda14cbcSMatt Macy #include <sys/lua/lauxlib.h>
15eda14cbcSMatt Macy #include <sys/lua/lualib.h>
16eda14cbcSMatt Macy
17eda14cbcSMatt Macy
18eda14cbcSMatt Macy /*
19eda14cbcSMatt Macy ** maximum number of captures that a pattern can do during
20eda14cbcSMatt Macy ** pattern-matching. This limit is arbitrary.
21eda14cbcSMatt Macy */
22eda14cbcSMatt Macy #if !defined(LUA_MAXCAPTURES)
23eda14cbcSMatt Macy #define LUA_MAXCAPTURES 16
24eda14cbcSMatt Macy #endif
25eda14cbcSMatt Macy
26eda14cbcSMatt Macy
27eda14cbcSMatt Macy /* macro to `unsign' a character */
28eda14cbcSMatt Macy #define uchar(c) ((unsigned char)(c))
29eda14cbcSMatt Macy
30eda14cbcSMatt Macy /*
31eda14cbcSMatt Macy * The provided version of sprintf returns a char *, but str_format expects
32eda14cbcSMatt Macy * it to return the number of characters printed. This version has the expected
33eda14cbcSMatt Macy * behavior.
34eda14cbcSMatt Macy */
str_sprintf(char * buf,const char * fmt,...)35eda14cbcSMatt Macy static size_t str_sprintf(char *buf, const char *fmt, ...) {
36eda14cbcSMatt Macy va_list args;
37eda14cbcSMatt Macy size_t len;
38eda14cbcSMatt Macy
39eda14cbcSMatt Macy va_start(args, fmt);
40eda14cbcSMatt Macy len = vsnprintf(buf, INT_MAX, fmt, args);
41eda14cbcSMatt Macy va_end(args);
42eda14cbcSMatt Macy
43eda14cbcSMatt Macy return len;
44eda14cbcSMatt Macy }
45eda14cbcSMatt Macy
46eda14cbcSMatt Macy
str_len(lua_State * L)47eda14cbcSMatt Macy static int str_len (lua_State *L) {
48eda14cbcSMatt Macy size_t l;
49eda14cbcSMatt Macy luaL_checklstring(L, 1, &l);
50eda14cbcSMatt Macy lua_pushinteger(L, (lua_Integer)l);
51eda14cbcSMatt Macy return 1;
52eda14cbcSMatt Macy }
53eda14cbcSMatt Macy
54eda14cbcSMatt Macy
55eda14cbcSMatt Macy /* translate a relative string position: negative means back from end */
posrelat(ptrdiff_t pos,size_t len)56eda14cbcSMatt Macy static size_t posrelat (ptrdiff_t pos, size_t len) {
57eda14cbcSMatt Macy if (pos >= 0) return (size_t)pos;
58eda14cbcSMatt Macy else if (0u - (size_t)pos > len) return 0;
59eda14cbcSMatt Macy else return len - ((size_t)-pos) + 1;
60eda14cbcSMatt Macy }
61eda14cbcSMatt Macy
62eda14cbcSMatt Macy
str_sub(lua_State * L)63eda14cbcSMatt Macy static int str_sub (lua_State *L) {
64eda14cbcSMatt Macy size_t l;
65eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l);
66eda14cbcSMatt Macy size_t start = posrelat(luaL_checkinteger(L, 2), l);
67eda14cbcSMatt Macy size_t end = posrelat(luaL_optinteger(L, 3, -1), l);
68eda14cbcSMatt Macy if (start < 1) start = 1;
69eda14cbcSMatt Macy if (end > l) end = l;
70eda14cbcSMatt Macy if (start <= end)
71eda14cbcSMatt Macy lua_pushlstring(L, s + start - 1, end - start + 1);
72eda14cbcSMatt Macy else lua_pushliteral(L, "");
73eda14cbcSMatt Macy return 1;
74eda14cbcSMatt Macy }
75eda14cbcSMatt Macy
76eda14cbcSMatt Macy
str_reverse(lua_State * L)77eda14cbcSMatt Macy static int str_reverse (lua_State *L) {
78eda14cbcSMatt Macy size_t l, i;
79eda14cbcSMatt Macy luaL_Buffer b;
80eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l);
81eda14cbcSMatt Macy char *p = luaL_buffinitsize(L, &b, l);
82eda14cbcSMatt Macy for (i = 0; i < l; i++)
83eda14cbcSMatt Macy p[i] = s[l - i - 1];
84eda14cbcSMatt Macy luaL_pushresultsize(&b, l);
85eda14cbcSMatt Macy return 1;
86eda14cbcSMatt Macy }
87eda14cbcSMatt Macy
88eda14cbcSMatt Macy
str_lower(lua_State * L)89eda14cbcSMatt Macy static int str_lower (lua_State *L) {
90eda14cbcSMatt Macy size_t l;
91eda14cbcSMatt Macy size_t i;
92eda14cbcSMatt Macy luaL_Buffer b;
93eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l);
94eda14cbcSMatt Macy char *p = luaL_buffinitsize(L, &b, l);
95eda14cbcSMatt Macy for (i=0; i<l; i++)
96eda14cbcSMatt Macy p[i] = tolower(uchar(s[i]));
97eda14cbcSMatt Macy luaL_pushresultsize(&b, l);
98eda14cbcSMatt Macy return 1;
99eda14cbcSMatt Macy }
100eda14cbcSMatt Macy
101eda14cbcSMatt Macy
str_upper(lua_State * L)102eda14cbcSMatt Macy static int str_upper (lua_State *L) {
103eda14cbcSMatt Macy size_t l;
104eda14cbcSMatt Macy size_t i;
105eda14cbcSMatt Macy luaL_Buffer b;
106eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l);
107eda14cbcSMatt Macy char *p = luaL_buffinitsize(L, &b, l);
108eda14cbcSMatt Macy for (i=0; i<l; i++)
109eda14cbcSMatt Macy p[i] = toupper(uchar(s[i]));
110eda14cbcSMatt Macy luaL_pushresultsize(&b, l);
111eda14cbcSMatt Macy return 1;
112eda14cbcSMatt Macy }
113eda14cbcSMatt Macy
114eda14cbcSMatt Macy
115eda14cbcSMatt Macy /* reasonable limit to avoid arithmetic overflow */
116eda14cbcSMatt Macy #define MAXSIZE ((~(size_t)0) >> 1)
117eda14cbcSMatt Macy
str_rep(lua_State * L)118eda14cbcSMatt Macy static int str_rep (lua_State *L) {
119eda14cbcSMatt Macy size_t l, lsep;
120eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l);
121eda14cbcSMatt Macy int n = luaL_checkint(L, 2);
122eda14cbcSMatt Macy const char *sep = luaL_optlstring(L, 3, "", &lsep);
123eda14cbcSMatt Macy if (n <= 0) lua_pushliteral(L, "");
124eda14cbcSMatt Macy else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
125eda14cbcSMatt Macy return luaL_error(L, "resulting string too large");
126eda14cbcSMatt Macy else {
127eda14cbcSMatt Macy size_t totallen = n * l + (n - 1) * lsep;
128eda14cbcSMatt Macy luaL_Buffer b;
129eda14cbcSMatt Macy char *p = luaL_buffinitsize(L, &b, totallen);
130eda14cbcSMatt Macy while (n-- > 1) { /* first n-1 copies (followed by separator) */
131eda14cbcSMatt Macy memcpy(p, s, l * sizeof(char)); p += l;
132eda14cbcSMatt Macy if (lsep > 0) { /* avoid empty 'memcpy' (may be expensive) */
133eda14cbcSMatt Macy memcpy(p, sep, lsep * sizeof(char)); p += lsep;
134eda14cbcSMatt Macy }
135eda14cbcSMatt Macy }
136eda14cbcSMatt Macy memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
137eda14cbcSMatt Macy luaL_pushresultsize(&b, totallen);
138eda14cbcSMatt Macy }
139eda14cbcSMatt Macy return 1;
140eda14cbcSMatt Macy }
141eda14cbcSMatt Macy
142eda14cbcSMatt Macy
str_byte(lua_State * L)143eda14cbcSMatt Macy static int str_byte (lua_State *L) {
144eda14cbcSMatt Macy size_t l;
145eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &l);
146eda14cbcSMatt Macy size_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
147eda14cbcSMatt Macy size_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
148eda14cbcSMatt Macy int n, i;
149eda14cbcSMatt Macy if (posi < 1) posi = 1;
150eda14cbcSMatt Macy if (pose > l) pose = l;
151eda14cbcSMatt Macy if (posi > pose) return 0; /* empty interval; return no values */
152eda14cbcSMatt Macy n = (int)(pose - posi + 1);
153eda14cbcSMatt Macy if (posi + n <= pose) /* (size_t -> int) overflow? */
154eda14cbcSMatt Macy return luaL_error(L, "string slice too long");
155eda14cbcSMatt Macy luaL_checkstack(L, n, "string slice too long");
156eda14cbcSMatt Macy for (i=0; i<n; i++)
157eda14cbcSMatt Macy lua_pushinteger(L, uchar(s[posi+i-1]));
158eda14cbcSMatt Macy return n;
159eda14cbcSMatt Macy }
160eda14cbcSMatt Macy
161eda14cbcSMatt Macy
str_char(lua_State * L)162eda14cbcSMatt Macy static int str_char (lua_State *L) {
163eda14cbcSMatt Macy int n = lua_gettop(L); /* number of arguments */
164eda14cbcSMatt Macy int i;
165eda14cbcSMatt Macy luaL_Buffer b;
166eda14cbcSMatt Macy char *p = luaL_buffinitsize(L, &b, n);
167eda14cbcSMatt Macy for (i=1; i<=n; i++) {
168eda14cbcSMatt Macy int c = luaL_checkint(L, i);
169eda14cbcSMatt Macy luaL_argcheck(L, uchar(c) == c, i, "value out of range");
170eda14cbcSMatt Macy p[i - 1] = uchar(c);
171eda14cbcSMatt Macy }
172eda14cbcSMatt Macy luaL_pushresultsize(&b, n);
173eda14cbcSMatt Macy return 1;
174eda14cbcSMatt Macy }
175eda14cbcSMatt Macy
176eda14cbcSMatt Macy
177eda14cbcSMatt Macy #if defined(LUA_USE_DUMP)
writer(lua_State * L,const void * b,size_t size,void * B)178eda14cbcSMatt Macy static int writer (lua_State *L, const void* b, size_t size, void* B) {
179eda14cbcSMatt Macy (void)L;
180eda14cbcSMatt Macy luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
181eda14cbcSMatt Macy return 0;
182eda14cbcSMatt Macy }
183eda14cbcSMatt Macy
184eda14cbcSMatt Macy
str_dump(lua_State * L)185eda14cbcSMatt Macy static int str_dump (lua_State *L) {
186eda14cbcSMatt Macy luaL_Buffer b;
187eda14cbcSMatt Macy luaL_checktype(L, 1, LUA_TFUNCTION);
188eda14cbcSMatt Macy lua_settop(L, 1);
189eda14cbcSMatt Macy luaL_buffinit(L,&b);
190eda14cbcSMatt Macy if (lua_dump(L, writer, &b) != 0)
191eda14cbcSMatt Macy return luaL_error(L, "unable to dump given function");
192eda14cbcSMatt Macy luaL_pushresult(&b);
193eda14cbcSMatt Macy return 1;
194eda14cbcSMatt Macy }
195eda14cbcSMatt Macy #endif
196eda14cbcSMatt Macy
197eda14cbcSMatt Macy
198eda14cbcSMatt Macy /*
199eda14cbcSMatt Macy ** {======================================================
200eda14cbcSMatt Macy ** PATTERN MATCHING
201eda14cbcSMatt Macy ** =======================================================
202eda14cbcSMatt Macy */
203eda14cbcSMatt Macy
204eda14cbcSMatt Macy
205eda14cbcSMatt Macy #define CAP_UNFINISHED (-1)
206eda14cbcSMatt Macy #define CAP_POSITION (-2)
207eda14cbcSMatt Macy
208eda14cbcSMatt Macy
209eda14cbcSMatt Macy typedef struct MatchState {
210eda14cbcSMatt Macy int matchdepth; /* control for recursive depth (to avoid C stack overflow) */
211eda14cbcSMatt Macy const char *src_init; /* init of source string */
212eda14cbcSMatt Macy const char *src_end; /* end ('\0') of source string */
213eda14cbcSMatt Macy const char *p_end; /* end ('\0') of pattern */
214eda14cbcSMatt Macy lua_State *L;
215eda14cbcSMatt Macy int level; /* total number of captures (finished or unfinished) */
216eda14cbcSMatt Macy struct {
217eda14cbcSMatt Macy const char *init;
218eda14cbcSMatt Macy ptrdiff_t len;
219eda14cbcSMatt Macy } capture[LUA_MAXCAPTURES];
220eda14cbcSMatt Macy } MatchState;
221eda14cbcSMatt Macy
222eda14cbcSMatt Macy
223eda14cbcSMatt Macy /* recursive function */
224eda14cbcSMatt Macy static const char *match (MatchState *ms, const char *s, const char *p);
225eda14cbcSMatt Macy
226eda14cbcSMatt Macy
227eda14cbcSMatt Macy /* maximum recursion depth for 'match' */
228eda14cbcSMatt Macy #if !defined(MAXCCALLS)
229eda14cbcSMatt Macy #define MAXCCALLS 200
230eda14cbcSMatt Macy #endif
231eda14cbcSMatt Macy
232eda14cbcSMatt Macy
233eda14cbcSMatt Macy #define L_ESC '%'
234eda14cbcSMatt Macy #define SPECIALS "^$*+?.([%-"
235eda14cbcSMatt Macy
236eda14cbcSMatt Macy
check_capture(MatchState * ms,int l)237eda14cbcSMatt Macy static int check_capture (MatchState *ms, int l) {
238eda14cbcSMatt Macy l -= '1';
239eda14cbcSMatt Macy if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
240eda14cbcSMatt Macy return luaL_error(ms->L, "invalid capture index %%%d", l + 1);
241eda14cbcSMatt Macy return l;
242eda14cbcSMatt Macy }
243eda14cbcSMatt Macy
244eda14cbcSMatt Macy
capture_to_close(MatchState * ms)245eda14cbcSMatt Macy static int capture_to_close (MatchState *ms) {
246eda14cbcSMatt Macy int level = ms->level;
247eda14cbcSMatt Macy for (level--; level>=0; level--)
248eda14cbcSMatt Macy if (ms->capture[level].len == CAP_UNFINISHED) return level;
249eda14cbcSMatt Macy return luaL_error(ms->L, "invalid pattern capture");
250eda14cbcSMatt Macy }
251eda14cbcSMatt Macy
252eda14cbcSMatt Macy
classend(MatchState * ms,const char * p)253eda14cbcSMatt Macy static const char *classend (MatchState *ms, const char *p) {
254eda14cbcSMatt Macy switch (*p++) {
255eda14cbcSMatt Macy case L_ESC: {
256eda14cbcSMatt Macy if (p == ms->p_end)
257eda14cbcSMatt Macy luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
258eda14cbcSMatt Macy return p+1;
259eda14cbcSMatt Macy }
260eda14cbcSMatt Macy case '[': {
261eda14cbcSMatt Macy if (*p == '^') p++;
262eda14cbcSMatt Macy do { /* look for a `]' */
263eda14cbcSMatt Macy if (p == ms->p_end)
264eda14cbcSMatt Macy luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
265eda14cbcSMatt Macy if (*(p++) == L_ESC && p < ms->p_end)
266eda14cbcSMatt Macy p++; /* skip escapes (e.g. `%]') */
267eda14cbcSMatt Macy } while (*p != ']');
268eda14cbcSMatt Macy return p+1;
269eda14cbcSMatt Macy }
270eda14cbcSMatt Macy default: {
271eda14cbcSMatt Macy return p;
272eda14cbcSMatt Macy }
273eda14cbcSMatt Macy }
274eda14cbcSMatt Macy }
275eda14cbcSMatt Macy
276eda14cbcSMatt Macy
match_class(int c,int cl)277eda14cbcSMatt Macy static int match_class (int c, int cl) {
278eda14cbcSMatt Macy int res;
279eda14cbcSMatt Macy switch (tolower(cl)) {
280eda14cbcSMatt Macy case 'a' : res = isalpha(c); break;
281eda14cbcSMatt Macy case 'c' : res = iscntrl(c); break;
282eda14cbcSMatt Macy case 'd' : res = isdigit(c); break;
283eda14cbcSMatt Macy case 'g' : res = isgraph(c); break;
284eda14cbcSMatt Macy case 'l' : res = islower(c); break;
285eda14cbcSMatt Macy case 'p' : res = ispunct(c); break;
286eda14cbcSMatt Macy case 's' : res = isspace(c); break;
287eda14cbcSMatt Macy case 'u' : res = isupper(c); break;
288eda14cbcSMatt Macy case 'w' : res = isalnum(c); break;
289eda14cbcSMatt Macy case 'x' : res = isxdigit(c); break;
290eda14cbcSMatt Macy case 'z' : res = (c == 0); break; /* deprecated option */
291eda14cbcSMatt Macy default: return (cl == c);
292eda14cbcSMatt Macy }
293eda14cbcSMatt Macy return (islower(cl) ? res : !res);
294eda14cbcSMatt Macy }
295eda14cbcSMatt Macy
296eda14cbcSMatt Macy
matchbracketclass(int c,const char * p,const char * ec)297eda14cbcSMatt Macy static int matchbracketclass (int c, const char *p, const char *ec) {
298eda14cbcSMatt Macy int sig = 1;
299eda14cbcSMatt Macy if (*(p+1) == '^') {
300eda14cbcSMatt Macy sig = 0;
301eda14cbcSMatt Macy p++; /* skip the `^' */
302eda14cbcSMatt Macy }
303eda14cbcSMatt Macy while (++p < ec) {
304eda14cbcSMatt Macy if (*p == L_ESC) {
305eda14cbcSMatt Macy p++;
306eda14cbcSMatt Macy if (match_class(c, uchar(*p)))
307eda14cbcSMatt Macy return sig;
308eda14cbcSMatt Macy }
309eda14cbcSMatt Macy else if ((*(p+1) == '-') && (p+2 < ec)) {
310eda14cbcSMatt Macy p+=2;
311eda14cbcSMatt Macy if (uchar(*(p-2)) <= c && c <= uchar(*p))
312eda14cbcSMatt Macy return sig;
313eda14cbcSMatt Macy }
314eda14cbcSMatt Macy else if (uchar(*p) == c) return sig;
315eda14cbcSMatt Macy }
316eda14cbcSMatt Macy return !sig;
317eda14cbcSMatt Macy }
318eda14cbcSMatt Macy
319eda14cbcSMatt Macy
singlematch(MatchState * ms,const char * s,const char * p,const char * ep)320eda14cbcSMatt Macy static int singlematch (MatchState *ms, const char *s, const char *p,
321eda14cbcSMatt Macy const char *ep) {
322eda14cbcSMatt Macy if (s >= ms->src_end)
323eda14cbcSMatt Macy return 0;
324eda14cbcSMatt Macy else {
325eda14cbcSMatt Macy int c = uchar(*s);
326eda14cbcSMatt Macy switch (*p) {
327eda14cbcSMatt Macy case '.': return 1; /* matches any char */
328eda14cbcSMatt Macy case L_ESC: return match_class(c, uchar(*(p+1)));
329eda14cbcSMatt Macy case '[': return matchbracketclass(c, p, ep-1);
330eda14cbcSMatt Macy default: return (uchar(*p) == c);
331eda14cbcSMatt Macy }
332eda14cbcSMatt Macy }
333eda14cbcSMatt Macy }
334eda14cbcSMatt Macy
335eda14cbcSMatt Macy
matchbalance(MatchState * ms,const char * s,const char * p)336eda14cbcSMatt Macy static const char *matchbalance (MatchState *ms, const char *s,
337eda14cbcSMatt Macy const char *p) {
338eda14cbcSMatt Macy if (p >= ms->p_end - 1)
339eda14cbcSMatt Macy luaL_error(ms->L, "malformed pattern "
340eda14cbcSMatt Macy "(missing arguments to " LUA_QL("%%b") ")");
341eda14cbcSMatt Macy if (*s != *p) return NULL;
342eda14cbcSMatt Macy else {
343eda14cbcSMatt Macy int b = *p;
344eda14cbcSMatt Macy int e = *(p+1);
345eda14cbcSMatt Macy int cont = 1;
346eda14cbcSMatt Macy while (++s < ms->src_end) {
347eda14cbcSMatt Macy if (*s == e) {
348eda14cbcSMatt Macy if (--cont == 0) return s+1;
349eda14cbcSMatt Macy }
350eda14cbcSMatt Macy else if (*s == b) cont++;
351eda14cbcSMatt Macy }
352eda14cbcSMatt Macy }
353eda14cbcSMatt Macy return NULL; /* string ends out of balance */
354eda14cbcSMatt Macy }
355eda14cbcSMatt Macy
356eda14cbcSMatt Macy
max_expand(MatchState * ms,const char * s,const char * p,const char * ep)357eda14cbcSMatt Macy static const char *max_expand (MatchState *ms, const char *s,
358eda14cbcSMatt Macy const char *p, const char *ep) {
359eda14cbcSMatt Macy ptrdiff_t i = 0; /* counts maximum expand for item */
360eda14cbcSMatt Macy while (singlematch(ms, s + i, p, ep))
361eda14cbcSMatt Macy i++;
362eda14cbcSMatt Macy /* keeps trying to match with the maximum repetitions */
363eda14cbcSMatt Macy while (i>=0) {
364eda14cbcSMatt Macy const char *res = match(ms, (s+i), ep+1);
365eda14cbcSMatt Macy if (res) return res;
366eda14cbcSMatt Macy i--; /* else didn't match; reduce 1 repetition to try again */
367eda14cbcSMatt Macy }
368eda14cbcSMatt Macy return NULL;
369eda14cbcSMatt Macy }
370eda14cbcSMatt Macy
371eda14cbcSMatt Macy
min_expand(MatchState * ms,const char * s,const char * p,const char * ep)372eda14cbcSMatt Macy static const char *min_expand (MatchState *ms, const char *s,
373eda14cbcSMatt Macy const char *p, const char *ep) {
374eda14cbcSMatt Macy for (;;) {
375eda14cbcSMatt Macy const char *res = match(ms, s, ep+1);
376eda14cbcSMatt Macy if (res != NULL)
377eda14cbcSMatt Macy return res;
378eda14cbcSMatt Macy else if (singlematch(ms, s, p, ep))
379eda14cbcSMatt Macy s++; /* try with one more repetition */
380eda14cbcSMatt Macy else return NULL;
381eda14cbcSMatt Macy }
382eda14cbcSMatt Macy }
383eda14cbcSMatt Macy
384eda14cbcSMatt Macy
start_capture(MatchState * ms,const char * s,const char * p,int what)385eda14cbcSMatt Macy static const char *start_capture (MatchState *ms, const char *s,
386eda14cbcSMatt Macy const char *p, int what) {
387eda14cbcSMatt Macy const char *res;
388eda14cbcSMatt Macy int level = ms->level;
389eda14cbcSMatt Macy if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
390eda14cbcSMatt Macy ms->capture[level].init = s;
391eda14cbcSMatt Macy ms->capture[level].len = what;
392eda14cbcSMatt Macy ms->level = level+1;
393eda14cbcSMatt Macy if ((res=match(ms, s, p)) == NULL) /* match failed? */
394eda14cbcSMatt Macy ms->level--; /* undo capture */
395eda14cbcSMatt Macy return res;
396eda14cbcSMatt Macy }
397eda14cbcSMatt Macy
398eda14cbcSMatt Macy
end_capture(MatchState * ms,const char * s,const char * p)399eda14cbcSMatt Macy static const char *end_capture (MatchState *ms, const char *s,
400eda14cbcSMatt Macy const char *p) {
401eda14cbcSMatt Macy int l = capture_to_close(ms);
402eda14cbcSMatt Macy const char *res;
403eda14cbcSMatt Macy ms->capture[l].len = s - ms->capture[l].init; /* close capture */
404eda14cbcSMatt Macy if ((res = match(ms, s, p)) == NULL) /* match failed? */
405eda14cbcSMatt Macy ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
406eda14cbcSMatt Macy return res;
407eda14cbcSMatt Macy }
408eda14cbcSMatt Macy
409eda14cbcSMatt Macy
match_capture(MatchState * ms,const char * s,int l)410eda14cbcSMatt Macy static const char *match_capture (MatchState *ms, const char *s, int l) {
411eda14cbcSMatt Macy size_t len;
412eda14cbcSMatt Macy l = check_capture(ms, l);
413eda14cbcSMatt Macy len = ms->capture[l].len;
414eda14cbcSMatt Macy if ((size_t)(ms->src_end-s) >= len &&
415eda14cbcSMatt Macy memcmp(ms->capture[l].init, s, len) == 0)
416eda14cbcSMatt Macy return s+len;
417eda14cbcSMatt Macy else return NULL;
418eda14cbcSMatt Macy }
419eda14cbcSMatt Macy
420eda14cbcSMatt Macy
match(MatchState * ms,const char * s,const char * p)421eda14cbcSMatt Macy static const char *match (MatchState *ms, const char *s, const char *p) {
422eda14cbcSMatt Macy if (ms->matchdepth-- == 0)
423eda14cbcSMatt Macy luaL_error(ms->L, "pattern too complex");
424eda14cbcSMatt Macy init: /* using goto's to optimize tail recursion */
425eda14cbcSMatt Macy if (p != ms->p_end) { /* end of pattern? */
426eda14cbcSMatt Macy switch (*p) {
427eda14cbcSMatt Macy case '(': { /* start capture */
428eda14cbcSMatt Macy if (*(p + 1) == ')') /* position capture? */
429eda14cbcSMatt Macy s = start_capture(ms, s, p + 2, CAP_POSITION);
430eda14cbcSMatt Macy else
431eda14cbcSMatt Macy s = start_capture(ms, s, p + 1, CAP_UNFINISHED);
432eda14cbcSMatt Macy break;
433eda14cbcSMatt Macy }
434eda14cbcSMatt Macy case ')': { /* end capture */
435eda14cbcSMatt Macy s = end_capture(ms, s, p + 1);
436eda14cbcSMatt Macy break;
437eda14cbcSMatt Macy }
438eda14cbcSMatt Macy case '$': {
439eda14cbcSMatt Macy if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */
440eda14cbcSMatt Macy goto dflt; /* no; go to default */
441eda14cbcSMatt Macy s = (s == ms->src_end) ? s : NULL; /* check end of string */
442eda14cbcSMatt Macy break;
443eda14cbcSMatt Macy }
444eda14cbcSMatt Macy case L_ESC: { /* escaped sequences not in the format class[*+?-]? */
445eda14cbcSMatt Macy switch (*(p + 1)) {
446eda14cbcSMatt Macy case 'b': { /* balanced string? */
447eda14cbcSMatt Macy s = matchbalance(ms, s, p + 2);
448eda14cbcSMatt Macy if (s != NULL) {
449eda14cbcSMatt Macy p += 4; goto init; /* return match(ms, s, p + 4); */
450eda14cbcSMatt Macy } /* else fail (s == NULL) */
451eda14cbcSMatt Macy break;
452eda14cbcSMatt Macy }
453eda14cbcSMatt Macy case 'f': { /* frontier? */
454eda14cbcSMatt Macy const char *ep; char previous;
455eda14cbcSMatt Macy p += 2;
456eda14cbcSMatt Macy if (*p != '[')
457eda14cbcSMatt Macy luaL_error(ms->L, "missing " LUA_QL("[") " after "
458eda14cbcSMatt Macy LUA_QL("%%f") " in pattern");
459eda14cbcSMatt Macy ep = classend(ms, p); /* points to what is next */
460eda14cbcSMatt Macy previous = (s == ms->src_init) ? '\0' : *(s - 1);
461eda14cbcSMatt Macy if (!matchbracketclass(uchar(previous), p, ep - 1) &&
462eda14cbcSMatt Macy matchbracketclass(uchar(*s), p, ep - 1)) {
463eda14cbcSMatt Macy p = ep; goto init; /* return match(ms, s, ep); */
464eda14cbcSMatt Macy }
465eda14cbcSMatt Macy s = NULL; /* match failed */
466eda14cbcSMatt Macy break;
467eda14cbcSMatt Macy }
468eda14cbcSMatt Macy case '0': case '1': case '2': case '3':
469eda14cbcSMatt Macy case '4': case '5': case '6': case '7':
470eda14cbcSMatt Macy case '8': case '9': { /* capture results (%0-%9)? */
471eda14cbcSMatt Macy s = match_capture(ms, s, uchar(*(p + 1)));
472eda14cbcSMatt Macy if (s != NULL) {
473eda14cbcSMatt Macy p += 2; goto init; /* return match(ms, s, p + 2) */
474eda14cbcSMatt Macy }
475eda14cbcSMatt Macy break;
476eda14cbcSMatt Macy }
477eda14cbcSMatt Macy default: goto dflt;
478eda14cbcSMatt Macy }
479eda14cbcSMatt Macy break;
480eda14cbcSMatt Macy }
481eda14cbcSMatt Macy default: dflt: { /* pattern class plus optional suffix */
482eda14cbcSMatt Macy const char *ep = classend(ms, p); /* points to optional suffix */
483eda14cbcSMatt Macy /* does not match at least once? */
484eda14cbcSMatt Macy if (!singlematch(ms, s, p, ep)) {
485eda14cbcSMatt Macy if (*ep == '*' || *ep == '?' || *ep == '-') { /* accept empty? */
486eda14cbcSMatt Macy p = ep + 1; goto init; /* return match(ms, s, ep + 1); */
487eda14cbcSMatt Macy }
488eda14cbcSMatt Macy else /* '+' or no suffix */
489eda14cbcSMatt Macy s = NULL; /* fail */
490eda14cbcSMatt Macy }
491eda14cbcSMatt Macy else { /* matched once */
492eda14cbcSMatt Macy switch (*ep) { /* handle optional suffix */
493eda14cbcSMatt Macy case '?': { /* optional */
494eda14cbcSMatt Macy const char *res;
495eda14cbcSMatt Macy if ((res = match(ms, s + 1, ep + 1)) != NULL)
496eda14cbcSMatt Macy s = res;
497eda14cbcSMatt Macy else {
498eda14cbcSMatt Macy p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */
499eda14cbcSMatt Macy }
500eda14cbcSMatt Macy break;
501eda14cbcSMatt Macy }
502eda14cbcSMatt Macy case '+': /* 1 or more repetitions */
503eda14cbcSMatt Macy s++; /* 1 match already done */
504c03c5b1cSMartin Matuska zfs_fallthrough;
505eda14cbcSMatt Macy case '*': /* 0 or more repetitions */
506eda14cbcSMatt Macy s = max_expand(ms, s, p, ep);
507eda14cbcSMatt Macy break;
508eda14cbcSMatt Macy case '-': /* 0 or more repetitions (minimum) */
509eda14cbcSMatt Macy s = min_expand(ms, s, p, ep);
510eda14cbcSMatt Macy break;
511eda14cbcSMatt Macy default: /* no suffix */
512eda14cbcSMatt Macy s++; p = ep; goto init; /* return match(ms, s + 1, ep); */
513eda14cbcSMatt Macy }
514eda14cbcSMatt Macy }
515eda14cbcSMatt Macy break;
516eda14cbcSMatt Macy }
517eda14cbcSMatt Macy }
518eda14cbcSMatt Macy }
519eda14cbcSMatt Macy ms->matchdepth++;
520eda14cbcSMatt Macy return s;
521eda14cbcSMatt Macy }
522eda14cbcSMatt Macy
523eda14cbcSMatt Macy
524eda14cbcSMatt Macy
lmemfind(const char * s1,size_t l1,const char * s2,size_t l2)525eda14cbcSMatt Macy static const char *lmemfind (const char *s1, size_t l1,
526eda14cbcSMatt Macy const char *s2, size_t l2) {
527eda14cbcSMatt Macy if (l2 == 0) return s1; /* empty strings are everywhere */
528eda14cbcSMatt Macy else if (l2 > l1) return NULL; /* avoids a negative `l1' */
529eda14cbcSMatt Macy else {
530eda14cbcSMatt Macy const char *init; /* to search for a `*s2' inside `s1' */
531eda14cbcSMatt Macy l2--; /* 1st char will be checked by `memchr' */
532eda14cbcSMatt Macy l1 = l1-l2; /* `s2' cannot be found after that */
533eda14cbcSMatt Macy while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
534eda14cbcSMatt Macy init++; /* 1st char is already checked */
535eda14cbcSMatt Macy if (memcmp(init, s2+1, l2) == 0)
536eda14cbcSMatt Macy return init-1;
537eda14cbcSMatt Macy else { /* correct `l1' and `s1' to try again */
538eda14cbcSMatt Macy l1 -= init-s1;
539eda14cbcSMatt Macy s1 = init;
540eda14cbcSMatt Macy }
541eda14cbcSMatt Macy }
542eda14cbcSMatt Macy return NULL; /* not found */
543eda14cbcSMatt Macy }
544eda14cbcSMatt Macy }
545eda14cbcSMatt Macy
546eda14cbcSMatt Macy
push_onecapture(MatchState * ms,int i,const char * s,const char * e)547eda14cbcSMatt Macy static void push_onecapture (MatchState *ms, int i, const char *s,
548eda14cbcSMatt Macy const char *e) {
549eda14cbcSMatt Macy if (i >= ms->level) {
550eda14cbcSMatt Macy if (i == 0) /* ms->level == 0, too */
551eda14cbcSMatt Macy lua_pushlstring(ms->L, s, e - s); /* add whole match */
552eda14cbcSMatt Macy else
553eda14cbcSMatt Macy luaL_error(ms->L, "invalid capture index");
554eda14cbcSMatt Macy }
555eda14cbcSMatt Macy else {
556eda14cbcSMatt Macy ptrdiff_t l = ms->capture[i].len;
557eda14cbcSMatt Macy if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
558eda14cbcSMatt Macy if (l == CAP_POSITION)
559eda14cbcSMatt Macy lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
560eda14cbcSMatt Macy else
561eda14cbcSMatt Macy lua_pushlstring(ms->L, ms->capture[i].init, l);
562eda14cbcSMatt Macy }
563eda14cbcSMatt Macy }
564eda14cbcSMatt Macy
565eda14cbcSMatt Macy
push_captures(MatchState * ms,const char * s,const char * e)566eda14cbcSMatt Macy static int push_captures (MatchState *ms, const char *s, const char *e) {
567eda14cbcSMatt Macy int i;
568eda14cbcSMatt Macy int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
569eda14cbcSMatt Macy luaL_checkstack(ms->L, nlevels, "too many captures");
570eda14cbcSMatt Macy for (i = 0; i < nlevels; i++)
571eda14cbcSMatt Macy push_onecapture(ms, i, s, e);
572eda14cbcSMatt Macy return nlevels; /* number of strings pushed */
573eda14cbcSMatt Macy }
574eda14cbcSMatt Macy
575eda14cbcSMatt Macy
576eda14cbcSMatt Macy /* check whether pattern has no special characters */
nospecials(const char * p,size_t l)577eda14cbcSMatt Macy static int nospecials (const char *p, size_t l) {
578eda14cbcSMatt Macy size_t upto = 0;
579eda14cbcSMatt Macy do {
580eda14cbcSMatt Macy if (strpbrk(p + upto, SPECIALS))
581eda14cbcSMatt Macy return 0; /* pattern has a special character */
582eda14cbcSMatt Macy upto += strlen(p + upto) + 1; /* may have more after \0 */
583eda14cbcSMatt Macy } while (upto <= l);
584eda14cbcSMatt Macy return 1; /* no special chars found */
585eda14cbcSMatt Macy }
586eda14cbcSMatt Macy
587eda14cbcSMatt Macy
str_find_aux(lua_State * L,int find)588eda14cbcSMatt Macy static int str_find_aux (lua_State *L, int find) {
589eda14cbcSMatt Macy size_t ls, lp;
590eda14cbcSMatt Macy const char *s = luaL_checklstring(L, 1, &ls);
591eda14cbcSMatt Macy const char *p = luaL_checklstring(L, 2, &lp);
592eda14cbcSMatt Macy size_t init = posrelat(luaL_optinteger(L, 3, 1), ls);
593eda14cbcSMatt Macy if (init < 1) init = 1;
594eda14cbcSMatt Macy else if (init > ls + 1) { /* start after string's end? */
595eda14cbcSMatt Macy lua_pushnil(L); /* cannot find anything */
596eda14cbcSMatt Macy return 1;
597eda14cbcSMatt Macy }
598eda14cbcSMatt Macy /* explicit request or no special characters? */
599eda14cbcSMatt Macy if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
600eda14cbcSMatt Macy /* do a plain search */
601eda14cbcSMatt Macy const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
602eda14cbcSMatt Macy if (s2) {
603eda14cbcSMatt Macy lua_pushinteger(L, s2 - s + 1);
604eda14cbcSMatt Macy lua_pushinteger(L, s2 - s + lp);
605eda14cbcSMatt Macy return 2;
606eda14cbcSMatt Macy }
607eda14cbcSMatt Macy }
608eda14cbcSMatt Macy else {
609eda14cbcSMatt Macy MatchState ms;
610eda14cbcSMatt Macy const char *s1 = s + init - 1;
611eda14cbcSMatt Macy int anchor = (*p == '^');
612eda14cbcSMatt Macy if (anchor) {
613eda14cbcSMatt Macy p++; lp--; /* skip anchor character */
614eda14cbcSMatt Macy }
615eda14cbcSMatt Macy ms.L = L;
616eda14cbcSMatt Macy ms.matchdepth = MAXCCALLS;
617eda14cbcSMatt Macy ms.src_init = s;
618eda14cbcSMatt Macy ms.src_end = s + ls;
619eda14cbcSMatt Macy ms.p_end = p + lp;
620eda14cbcSMatt Macy do {
621eda14cbcSMatt Macy const char *res;
622eda14cbcSMatt Macy ms.level = 0;
623eda14cbcSMatt Macy lua_assert(ms.matchdepth == MAXCCALLS);
624eda14cbcSMatt Macy if ((res=match(&ms, s1, p)) != NULL) {
625eda14cbcSMatt Macy if (find) {
626eda14cbcSMatt Macy lua_pushinteger(L, s1 - s + 1); /* start */
627eda14cbcSMatt Macy lua_pushinteger(L, res - s); /* end */
628eda14cbcSMatt Macy return push_captures(&ms, NULL, 0) + 2;
629eda14cbcSMatt Macy }
630eda14cbcSMatt Macy else
631eda14cbcSMatt Macy return push_captures(&ms, s1, res);
632eda14cbcSMatt Macy }
633eda14cbcSMatt Macy } while (s1++ < ms.src_end && !anchor);
634eda14cbcSMatt Macy }
635eda14cbcSMatt Macy lua_pushnil(L); /* not found */
636eda14cbcSMatt Macy return 1;
637eda14cbcSMatt Macy }
638eda14cbcSMatt Macy
639eda14cbcSMatt Macy
str_find(lua_State * L)640eda14cbcSMatt Macy static int str_find (lua_State *L) {
641eda14cbcSMatt Macy return str_find_aux(L, 1);
642eda14cbcSMatt Macy }
643eda14cbcSMatt Macy
644eda14cbcSMatt Macy
str_match(lua_State * L)645eda14cbcSMatt Macy static int str_match (lua_State *L) {
646eda14cbcSMatt Macy return str_find_aux(L, 0);
647eda14cbcSMatt Macy }
648eda14cbcSMatt Macy
649eda14cbcSMatt Macy
gmatch_aux(lua_State * L)650eda14cbcSMatt Macy static int gmatch_aux (lua_State *L) {
651eda14cbcSMatt Macy MatchState ms;
652eda14cbcSMatt Macy size_t ls, lp;
653eda14cbcSMatt Macy const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
654eda14cbcSMatt Macy const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
655eda14cbcSMatt Macy const char *src;
656eda14cbcSMatt Macy ms.L = L;
657eda14cbcSMatt Macy ms.matchdepth = MAXCCALLS;
658eda14cbcSMatt Macy ms.src_init = s;
659eda14cbcSMatt Macy ms.src_end = s+ls;
660eda14cbcSMatt Macy ms.p_end = p + lp;
661eda14cbcSMatt Macy for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
662eda14cbcSMatt Macy src <= ms.src_end;
663eda14cbcSMatt Macy src++) {
664eda14cbcSMatt Macy const char *e;
665eda14cbcSMatt Macy ms.level = 0;
666eda14cbcSMatt Macy lua_assert(ms.matchdepth == MAXCCALLS);
667eda14cbcSMatt Macy if ((e = match(&ms, src, p)) != NULL) {
668eda14cbcSMatt Macy lua_Integer newstart = e-s;
669eda14cbcSMatt Macy if (e == src) newstart++; /* empty match? go at least one position */
670eda14cbcSMatt Macy lua_pushinteger(L, newstart);
671eda14cbcSMatt Macy lua_replace(L, lua_upvalueindex(3));
672eda14cbcSMatt Macy return push_captures(&ms, src, e);
673eda14cbcSMatt Macy }
674eda14cbcSMatt Macy }
675eda14cbcSMatt Macy return 0; /* not found */
676eda14cbcSMatt Macy }
677eda14cbcSMatt Macy
678eda14cbcSMatt Macy
str_gmatch(lua_State * L)679eda14cbcSMatt Macy static int str_gmatch (lua_State *L) {
680eda14cbcSMatt Macy luaL_checkstring(L, 1);
681eda14cbcSMatt Macy luaL_checkstring(L, 2);
682eda14cbcSMatt Macy lua_settop(L, 2);
683eda14cbcSMatt Macy lua_pushinteger(L, 0);
684eda14cbcSMatt Macy lua_pushcclosure(L, gmatch_aux, 3);
685eda14cbcSMatt Macy return 1;
686eda14cbcSMatt Macy }
687eda14cbcSMatt Macy
688eda14cbcSMatt Macy
add_s(MatchState * ms,luaL_Buffer * b,const char * s,const char * e)689eda14cbcSMatt Macy static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
690eda14cbcSMatt Macy const char *e) {
691eda14cbcSMatt Macy size_t l, i;
692eda14cbcSMatt Macy const char *news = lua_tolstring(ms->L, 3, &l);
693eda14cbcSMatt Macy for (i = 0; i < l; i++) {
694eda14cbcSMatt Macy if (news[i] != L_ESC)
695eda14cbcSMatt Macy luaL_addchar(b, news[i]);
696eda14cbcSMatt Macy else {
697eda14cbcSMatt Macy i++; /* skip ESC */
698eda14cbcSMatt Macy if (!isdigit(uchar(news[i]))) {
699eda14cbcSMatt Macy if (news[i] != L_ESC)
700eda14cbcSMatt Macy luaL_error(ms->L, "invalid use of " LUA_QL("%c")
701eda14cbcSMatt Macy " in replacement string", L_ESC);
702eda14cbcSMatt Macy luaL_addchar(b, news[i]);
703eda14cbcSMatt Macy }
704eda14cbcSMatt Macy else if (news[i] == '0')
705eda14cbcSMatt Macy luaL_addlstring(b, s, e - s);
706eda14cbcSMatt Macy else {
707eda14cbcSMatt Macy push_onecapture(ms, news[i] - '1', s, e);
708eda14cbcSMatt Macy luaL_addvalue(b); /* add capture to accumulated result */
709eda14cbcSMatt Macy }
710eda14cbcSMatt Macy }
711eda14cbcSMatt Macy }
712eda14cbcSMatt Macy }
713eda14cbcSMatt Macy
714eda14cbcSMatt Macy
add_value(MatchState * ms,luaL_Buffer * b,const char * s,const char * e,int tr)715eda14cbcSMatt Macy static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
716eda14cbcSMatt Macy const char *e, int tr) {
717eda14cbcSMatt Macy lua_State *L = ms->L;
718eda14cbcSMatt Macy switch (tr) {
719eda14cbcSMatt Macy case LUA_TFUNCTION: {
720eda14cbcSMatt Macy int n;
721eda14cbcSMatt Macy lua_pushvalue(L, 3);
722eda14cbcSMatt Macy n = push_captures(ms, s, e);
723eda14cbcSMatt Macy lua_call(L, n, 1);
724eda14cbcSMatt Macy break;
725eda14cbcSMatt Macy }
726eda14cbcSMatt Macy case LUA_TTABLE: {
727eda14cbcSMatt Macy push_onecapture(ms, 0, s, e);
728eda14cbcSMatt Macy lua_gettable(L, 3);
729eda14cbcSMatt Macy break;
730eda14cbcSMatt Macy }
731eda14cbcSMatt Macy default: { /* LUA_TNUMBER or LUA_TSTRING */
732eda14cbcSMatt Macy add_s(ms, b, s, e);
733eda14cbcSMatt Macy return;
734eda14cbcSMatt Macy }
735eda14cbcSMatt Macy }
736eda14cbcSMatt Macy if (!lua_toboolean(L, -1)) { /* nil or false? */
737eda14cbcSMatt Macy lua_pop(L, 1);
738eda14cbcSMatt Macy lua_pushlstring(L, s, e - s); /* keep original text */
739eda14cbcSMatt Macy }
740eda14cbcSMatt Macy else if (!lua_isstring(L, -1))
741eda14cbcSMatt Macy luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
742eda14cbcSMatt Macy luaL_addvalue(b); /* add result to accumulator */
743eda14cbcSMatt Macy }
744eda14cbcSMatt Macy
745eda14cbcSMatt Macy
str_gsub(lua_State * L)746eda14cbcSMatt Macy static int str_gsub (lua_State *L) {
747eda14cbcSMatt Macy size_t srcl, lp;
748eda14cbcSMatt Macy const char *src = luaL_checklstring(L, 1, &srcl);
749eda14cbcSMatt Macy const char *p = luaL_checklstring(L, 2, &lp);
750eda14cbcSMatt Macy int tr = lua_type(L, 3);
751eda14cbcSMatt Macy size_t max_s = luaL_optinteger(L, 4, srcl+1);
752eda14cbcSMatt Macy int anchor = (*p == '^');
753eda14cbcSMatt Macy size_t n = 0;
754eda14cbcSMatt Macy MatchState ms;
755eda14cbcSMatt Macy luaL_Buffer b;
756eda14cbcSMatt Macy luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
757eda14cbcSMatt Macy tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
758eda14cbcSMatt Macy "string/function/table expected");
759eda14cbcSMatt Macy luaL_buffinit(L, &b);
760eda14cbcSMatt Macy if (anchor) {
761eda14cbcSMatt Macy p++; lp--; /* skip anchor character */
762eda14cbcSMatt Macy }
763eda14cbcSMatt Macy ms.L = L;
764eda14cbcSMatt Macy ms.matchdepth = MAXCCALLS;
765eda14cbcSMatt Macy ms.src_init = src;
766eda14cbcSMatt Macy ms.src_end = src+srcl;
767eda14cbcSMatt Macy ms.p_end = p + lp;
768eda14cbcSMatt Macy while (n < max_s) {
769eda14cbcSMatt Macy const char *e;
770eda14cbcSMatt Macy ms.level = 0;
771eda14cbcSMatt Macy lua_assert(ms.matchdepth == MAXCCALLS);
772eda14cbcSMatt Macy e = match(&ms, src, p);
773eda14cbcSMatt Macy if (e) {
774eda14cbcSMatt Macy n++;
775eda14cbcSMatt Macy add_value(&ms, &b, src, e, tr);
776eda14cbcSMatt Macy }
777eda14cbcSMatt Macy if (e && e>src) /* non empty match? */
778eda14cbcSMatt Macy src = e; /* skip it */
779eda14cbcSMatt Macy else if (src < ms.src_end)
780eda14cbcSMatt Macy luaL_addchar(&b, *src++);
781eda14cbcSMatt Macy else break;
782eda14cbcSMatt Macy if (anchor) break;
783eda14cbcSMatt Macy }
784eda14cbcSMatt Macy luaL_addlstring(&b, src, ms.src_end-src);
785eda14cbcSMatt Macy luaL_pushresult(&b);
786eda14cbcSMatt Macy lua_pushinteger(L, n); /* number of substitutions */
787eda14cbcSMatt Macy return 2;
788eda14cbcSMatt Macy }
789eda14cbcSMatt Macy
790eda14cbcSMatt Macy /* }====================================================== */
791eda14cbcSMatt Macy
792eda14cbcSMatt Macy
793eda14cbcSMatt Macy
794eda14cbcSMatt Macy /*
795eda14cbcSMatt Macy ** {======================================================
796eda14cbcSMatt Macy ** STRING FORMAT
797eda14cbcSMatt Macy ** =======================================================
798eda14cbcSMatt Macy */
799eda14cbcSMatt Macy
800eda14cbcSMatt Macy /*
801eda14cbcSMatt Macy ** LUA_INTFRMLEN is the length modifier for integer conversions in
802eda14cbcSMatt Macy ** 'string.format'; LUA_INTFRM_T is the integer type corresponding to
803eda14cbcSMatt Macy ** the previous length
804eda14cbcSMatt Macy */
805eda14cbcSMatt Macy #if !defined(LUA_INTFRMLEN) /* { */
806eda14cbcSMatt Macy #if defined(LUA_USE_LONGLONG)
807eda14cbcSMatt Macy
808eda14cbcSMatt Macy #define LUA_INTFRMLEN "ll"
809eda14cbcSMatt Macy #define LUA_INTFRM_T long long
810eda14cbcSMatt Macy
811eda14cbcSMatt Macy #else
812eda14cbcSMatt Macy
813eda14cbcSMatt Macy #define LUA_INTFRMLEN "l"
814eda14cbcSMatt Macy #define LUA_INTFRM_T long
815eda14cbcSMatt Macy
816eda14cbcSMatt Macy #endif
817eda14cbcSMatt Macy #endif /* } */
818eda14cbcSMatt Macy
819eda14cbcSMatt Macy
820eda14cbcSMatt Macy /*
821eda14cbcSMatt Macy ** LUA_FLTFRMLEN is the length modifier for float conversions in
822eda14cbcSMatt Macy ** 'string.format'; LUA_FLTFRM_T is the float type corresponding to
823eda14cbcSMatt Macy ** the previous length
824eda14cbcSMatt Macy */
825eda14cbcSMatt Macy #if !defined(LUA_FLTFRMLEN)
826eda14cbcSMatt Macy
827eda14cbcSMatt Macy #define LUA_FLTFRMLEN ""
828eda14cbcSMatt Macy #define LUA_FLTFRM_T double
829eda14cbcSMatt Macy
830eda14cbcSMatt Macy #endif
831eda14cbcSMatt Macy
832eda14cbcSMatt Macy
833eda14cbcSMatt Macy /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
834eda14cbcSMatt Macy #define MAX_ITEM 512
835eda14cbcSMatt Macy /* valid flags in a format specification */
836eda14cbcSMatt Macy #define FLAGS "-+ #0"
837eda14cbcSMatt Macy /*
838eda14cbcSMatt Macy ** maximum size of each format specification (such as '%-099.99d')
839eda14cbcSMatt Macy ** (+10 accounts for %99.99x plus margin of error)
840eda14cbcSMatt Macy */
841eda14cbcSMatt Macy #define MAX_FORMAT (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
842eda14cbcSMatt Macy
843eda14cbcSMatt Macy
addquoted(lua_State * L,luaL_Buffer * b,int arg)844eda14cbcSMatt Macy static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
845eda14cbcSMatt Macy size_t l;
846eda14cbcSMatt Macy const char *s = luaL_checklstring(L, arg, &l);
847eda14cbcSMatt Macy luaL_addchar(b, '"');
848eda14cbcSMatt Macy while (l--) {
849eda14cbcSMatt Macy if (*s == '"' || *s == '\\' || *s == '\n') {
850eda14cbcSMatt Macy luaL_addchar(b, '\\');
851eda14cbcSMatt Macy luaL_addchar(b, *s);
852eda14cbcSMatt Macy }
853eda14cbcSMatt Macy else if (*s == '\0' || iscntrl(uchar(*s))) {
854eda14cbcSMatt Macy char buff[10];
855eda14cbcSMatt Macy if (!isdigit(uchar(*(s+1))))
856eda14cbcSMatt Macy snprintf(buff, sizeof(buff), "\\%d", (int)uchar(*s));
857eda14cbcSMatt Macy else
858eda14cbcSMatt Macy snprintf(buff, sizeof(buff), "\\%03d", (int)uchar(*s));
859eda14cbcSMatt Macy luaL_addstring(b, buff);
860eda14cbcSMatt Macy }
861eda14cbcSMatt Macy else
862eda14cbcSMatt Macy luaL_addchar(b, *s);
863eda14cbcSMatt Macy s++;
864eda14cbcSMatt Macy }
865eda14cbcSMatt Macy luaL_addchar(b, '"');
866eda14cbcSMatt Macy }
867eda14cbcSMatt Macy
scanformat(lua_State * L,const char * strfrmt,char * form)868eda14cbcSMatt Macy static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
869eda14cbcSMatt Macy const char *p = strfrmt;
870eda14cbcSMatt Macy while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
871eda14cbcSMatt Macy if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
872eda14cbcSMatt Macy luaL_error(L, "invalid format (repeated flags)");
873eda14cbcSMatt Macy if (isdigit(uchar(*p))) p++; /* skip width */
874eda14cbcSMatt Macy if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
875eda14cbcSMatt Macy if (*p == '.') {
876eda14cbcSMatt Macy p++;
877eda14cbcSMatt Macy if (isdigit(uchar(*p))) p++; /* skip precision */
878eda14cbcSMatt Macy if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
879eda14cbcSMatt Macy }
880eda14cbcSMatt Macy if (isdigit(uchar(*p)))
881eda14cbcSMatt Macy luaL_error(L, "invalid format (width or precision too long)");
882eda14cbcSMatt Macy *(form++) = '%';
883eda14cbcSMatt Macy memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
884eda14cbcSMatt Macy form += p - strfrmt + 1;
885eda14cbcSMatt Macy *form = '\0';
886eda14cbcSMatt Macy return p;
887eda14cbcSMatt Macy }
888eda14cbcSMatt Macy
889eda14cbcSMatt Macy
890eda14cbcSMatt Macy /*
891eda14cbcSMatt Macy ** add length modifier into formats
892eda14cbcSMatt Macy */
addlenmod(char * form,const char * lenmod,size_t size)893eda14cbcSMatt Macy static void addlenmod (char *form, const char *lenmod, size_t size) {
894eda14cbcSMatt Macy size_t l = strlen(form);
895eda14cbcSMatt Macy size_t lm = strlen(lenmod);
896eda14cbcSMatt Macy char spec = form[l - 1];
897eda14cbcSMatt Macy strlcpy(form + l - 1, lenmod, size - (l - 1));
898eda14cbcSMatt Macy form[l + lm - 1] = spec;
899eda14cbcSMatt Macy form[l + lm] = '\0';
900eda14cbcSMatt Macy }
901eda14cbcSMatt Macy
902eda14cbcSMatt Macy
str_format(lua_State * L)903eda14cbcSMatt Macy static int str_format (lua_State *L) {
904eda14cbcSMatt Macy int top = lua_gettop(L);
905eda14cbcSMatt Macy int arg = 1;
906eda14cbcSMatt Macy size_t sfl;
907eda14cbcSMatt Macy const char *strfrmt = luaL_checklstring(L, arg, &sfl);
908eda14cbcSMatt Macy const char *strfrmt_end = strfrmt+sfl;
909eda14cbcSMatt Macy luaL_Buffer b;
910eda14cbcSMatt Macy luaL_buffinit(L, &b);
911eda14cbcSMatt Macy while (strfrmt < strfrmt_end) {
912eda14cbcSMatt Macy if (*strfrmt != L_ESC)
913eda14cbcSMatt Macy luaL_addchar(&b, *strfrmt++);
914eda14cbcSMatt Macy else if (*++strfrmt == L_ESC)
915eda14cbcSMatt Macy luaL_addchar(&b, *strfrmt++); /* %% */
916eda14cbcSMatt Macy else { /* format item */
917eda14cbcSMatt Macy char form[MAX_FORMAT]; /* to store the format (`%...') */
918eda14cbcSMatt Macy char *buff = luaL_prepbuffsize(&b, MAX_ITEM); /* to put formatted item */
919eda14cbcSMatt Macy int nb = 0; /* number of bytes in added item */
920eda14cbcSMatt Macy if (++arg > top)
921eda14cbcSMatt Macy luaL_argerror(L, arg, "no value");
922eda14cbcSMatt Macy strfrmt = scanformat(L, strfrmt, form);
923eda14cbcSMatt Macy switch (*strfrmt++) {
924eda14cbcSMatt Macy case 'c': {
925eda14cbcSMatt Macy nb = str_sprintf(buff, form, luaL_checkint(L, arg));
926eda14cbcSMatt Macy break;
927eda14cbcSMatt Macy }
928eda14cbcSMatt Macy case 'd': case 'i': {
929eda14cbcSMatt Macy lua_Number n = luaL_checknumber(L, arg);
930eda14cbcSMatt Macy LUA_INTFRM_T ni = (LUA_INTFRM_T)n;
931eda14cbcSMatt Macy lua_Number diff = n - (lua_Number)ni;
932eda14cbcSMatt Macy luaL_argcheck(L, -1 < diff && diff < 1, arg,
933eda14cbcSMatt Macy "not a number in proper range");
934eda14cbcSMatt Macy addlenmod(form, LUA_INTFRMLEN, MAX_FORMAT);
935eda14cbcSMatt Macy nb = str_sprintf(buff, form, ni);
936eda14cbcSMatt Macy break;
937eda14cbcSMatt Macy }
938eda14cbcSMatt Macy case 'o': case 'u': case 'x': case 'X': {
939eda14cbcSMatt Macy lua_Number n = luaL_checknumber(L, arg);
940eda14cbcSMatt Macy unsigned LUA_INTFRM_T ni = (unsigned LUA_INTFRM_T)n;
941eda14cbcSMatt Macy lua_Number diff = n - (lua_Number)ni;
942eda14cbcSMatt Macy luaL_argcheck(L, -1 < diff && diff < 1, arg,
943eda14cbcSMatt Macy "not a non-negative number in proper range");
944eda14cbcSMatt Macy addlenmod(form, LUA_INTFRMLEN, MAX_FORMAT);
945eda14cbcSMatt Macy nb = str_sprintf(buff, form, ni);
946eda14cbcSMatt Macy break;
947eda14cbcSMatt Macy }
948eda14cbcSMatt Macy #if defined(LUA_USE_FLOAT_FORMATS)
949eda14cbcSMatt Macy case 'e': case 'E': case 'f':
950eda14cbcSMatt Macy #if defined(LUA_USE_AFORMAT)
951eda14cbcSMatt Macy case 'a': case 'A':
952eda14cbcSMatt Macy #endif
953eda14cbcSMatt Macy case 'g': case 'G': {
954eda14cbcSMatt Macy addlenmod(form, LUA_FLTFRMLEN, MAX_FORMAT);
955eda14cbcSMatt Macy nb = str_sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
956eda14cbcSMatt Macy break;
957eda14cbcSMatt Macy }
958eda14cbcSMatt Macy #endif
959eda14cbcSMatt Macy case 'q': {
960eda14cbcSMatt Macy addquoted(L, &b, arg);
961eda14cbcSMatt Macy break;
962eda14cbcSMatt Macy }
963eda14cbcSMatt Macy case 's': {
964eda14cbcSMatt Macy size_t l;
965eda14cbcSMatt Macy const char *s = luaL_tolstring(L, arg, &l);
966eda14cbcSMatt Macy if (!strchr(form, '.') && l >= 100) {
967eda14cbcSMatt Macy /* no precision and string is too long to be formatted;
968eda14cbcSMatt Macy keep original string */
969eda14cbcSMatt Macy luaL_addvalue(&b);
970eda14cbcSMatt Macy break;
971eda14cbcSMatt Macy }
972eda14cbcSMatt Macy else {
973eda14cbcSMatt Macy nb = str_sprintf(buff, form, s);
974eda14cbcSMatt Macy lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
975eda14cbcSMatt Macy break;
976eda14cbcSMatt Macy }
977eda14cbcSMatt Macy }
978eda14cbcSMatt Macy default: { /* also treat cases `pnLlh' */
979eda14cbcSMatt Macy return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
980eda14cbcSMatt Macy LUA_QL("format"), *(strfrmt - 1));
981eda14cbcSMatt Macy }
982eda14cbcSMatt Macy }
983eda14cbcSMatt Macy luaL_addsize(&b, nb);
984eda14cbcSMatt Macy }
985eda14cbcSMatt Macy }
986eda14cbcSMatt Macy luaL_pushresult(&b);
987eda14cbcSMatt Macy return 1;
988eda14cbcSMatt Macy }
989eda14cbcSMatt Macy
990eda14cbcSMatt Macy /* }====================================================== */
991eda14cbcSMatt Macy
992eda14cbcSMatt Macy
993eda14cbcSMatt Macy static const luaL_Reg strlib[] = {
994eda14cbcSMatt Macy {"byte", str_byte},
995eda14cbcSMatt Macy {"char", str_char},
996eda14cbcSMatt Macy #if defined(LUA_USE_DUMP)
997eda14cbcSMatt Macy {"dump", str_dump},
998eda14cbcSMatt Macy #endif
999eda14cbcSMatt Macy {"find", str_find},
1000eda14cbcSMatt Macy {"format", str_format},
1001eda14cbcSMatt Macy {"gmatch", str_gmatch},
1002eda14cbcSMatt Macy {"gsub", str_gsub},
1003eda14cbcSMatt Macy {"len", str_len},
1004eda14cbcSMatt Macy {"lower", str_lower},
1005eda14cbcSMatt Macy {"match", str_match},
1006eda14cbcSMatt Macy {"rep", str_rep},
1007eda14cbcSMatt Macy {"reverse", str_reverse},
1008eda14cbcSMatt Macy {"sub", str_sub},
1009eda14cbcSMatt Macy {"upper", str_upper},
1010eda14cbcSMatt Macy {NULL, NULL}
1011eda14cbcSMatt Macy };
1012eda14cbcSMatt Macy
1013eda14cbcSMatt Macy
createmetatable(lua_State * L)1014eda14cbcSMatt Macy static void createmetatable (lua_State *L) {
1015eda14cbcSMatt Macy lua_createtable(L, 0, 1); /* table to be metatable for strings */
1016eda14cbcSMatt Macy lua_pushliteral(L, ""); /* dummy string */
1017eda14cbcSMatt Macy lua_pushvalue(L, -2); /* copy table */
1018eda14cbcSMatt Macy lua_setmetatable(L, -2); /* set table as metatable for strings */
1019eda14cbcSMatt Macy lua_pop(L, 1); /* pop dummy string */
1020eda14cbcSMatt Macy lua_pushvalue(L, -2); /* get string library */
1021eda14cbcSMatt Macy lua_setfield(L, -2, "__index"); /* metatable.__index = string */
1022eda14cbcSMatt Macy lua_pop(L, 1); /* pop metatable */
1023eda14cbcSMatt Macy }
1024eda14cbcSMatt Macy
1025eda14cbcSMatt Macy
1026eda14cbcSMatt Macy /*
1027eda14cbcSMatt Macy ** Open string library
1028eda14cbcSMatt Macy */
luaopen_string(lua_State * L)1029eda14cbcSMatt Macy LUAMOD_API int luaopen_string (lua_State *L) {
1030eda14cbcSMatt Macy luaL_newlib(L, strlib);
1031eda14cbcSMatt Macy createmetatable(L);
1032eda14cbcSMatt Macy return 1;
1033eda14cbcSMatt Macy }
1034eda14cbcSMatt Macy
1035eda14cbcSMatt Macy #if defined(_KERNEL)
1036eda14cbcSMatt Macy
1037eda14cbcSMatt Macy EXPORT_SYMBOL(luaopen_string);
1038eda14cbcSMatt Macy
1039eda14cbcSMatt Macy #endif
1040