annotate lib/lua/lua-5.2.2/lstrlib.c @ 1917:4ecc099ef876

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