aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-11-28 18:13:13 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-11-28 18:13:13 -0200
commit72659a06050632da1a9b4c492302be46ac283f6b (patch)
treebac06b4ea523ba5443564d0869e392180d4b7b77 /lstrlib.c
parentdfaf8c5291fa8aef5bedbfa375853475364ac76e (diff)
downloadlua-72659a06050632da1a9b4c492302be46ac283f6b.tar.gz
lua-72659a06050632da1a9b4c492302be46ac283f6b.tar.bz2
lua-72659a06050632da1a9b4c492302be46ac283f6b.zip
no more explicit support for wide-chars; too much troble...
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c277
1 files changed, 141 insertions, 136 deletions
diff --git a/lstrlib.c b/lstrlib.c
index 9a6f04d1..8ce5e9ba 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.72 2001/10/16 17:41:43 roberto Exp $ 2** $Id: lstrlib.c,v 1.73 2001/10/26 17:33:30 roberto Exp $
3** Standard library for string operations and pattern-matching 3** Standard library for string operations and pattern-matching
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -11,13 +11,18 @@
11#include <stdlib.h> 11#include <stdlib.h>
12#include <string.h> 12#include <string.h>
13 13
14#define LUA_PRIVATE
15#include "lua.h" 14#include "lua.h"
16 15
17#include "lauxlib.h" 16#include "lauxlib.h"
18#include "lualib.h" 17#include "lualib.h"
19 18
20 19
20/* macro to `unsign' a character */
21#ifndef uchar
22#define uchar(c) ((unsigned char)(c))
23#endif
24
25
21typedef long sint32; /* a signed version for size_t */ 26typedef long sint32; /* a signed version for size_t */
22 27
23 28
@@ -37,14 +42,14 @@ static sint32 posrelat (sint32 pos, size_t len) {
37 42
38static int str_sub (lua_State *L) { 43static int str_sub (lua_State *L) {
39 size_t l; 44 size_t l;
40 const l_char *s = luaL_check_lstr(L, 1, &l); 45 const char *s = luaL_check_lstr(L, 1, &l);
41 sint32 start = posrelat(luaL_check_long(L, 2), l); 46 sint32 start = posrelat(luaL_check_long(L, 2), l);
42 sint32 end = posrelat(luaL_opt_long(L, 3, -1), l); 47 sint32 end = posrelat(luaL_opt_long(L, 3, -1), l);
43 if (start < 1) start = 1; 48 if (start < 1) start = 1;
44 if (end > (sint32)l) end = l; 49 if (end > (sint32)l) end = l;
45 if (start <= end) 50 if (start <= end)
46 lua_pushlstring(L, s+start-1, end-start+1); 51 lua_pushlstring(L, s+start-1, end-start+1);
47 else lua_pushliteral(L, l_s("")); 52 else lua_pushliteral(L, "");
48 return 1; 53 return 1;
49} 54}
50 55
@@ -53,7 +58,7 @@ static int str_lower (lua_State *L) {
53 size_t l; 58 size_t l;
54 size_t i; 59 size_t i;
55 luaL_Buffer b; 60 luaL_Buffer b;
56 const l_char *s = luaL_check_lstr(L, 1, &l); 61 const char *s = luaL_check_lstr(L, 1, &l);
57 luaL_buffinit(L, &b); 62 luaL_buffinit(L, &b);
58 for (i=0; i<l; i++) 63 for (i=0; i<l; i++)
59 luaL_putchar(&b, tolower(uchar(s[i]))); 64 luaL_putchar(&b, tolower(uchar(s[i])));
@@ -66,7 +71,7 @@ static int str_upper (lua_State *L) {
66 size_t l; 71 size_t l;
67 size_t i; 72 size_t i;
68 luaL_Buffer b; 73 luaL_Buffer b;
69 const l_char *s = luaL_check_lstr(L, 1, &l); 74 const char *s = luaL_check_lstr(L, 1, &l);
70 luaL_buffinit(L, &b); 75 luaL_buffinit(L, &b);
71 for (i=0; i<l; i++) 76 for (i=0; i<l; i++)
72 luaL_putchar(&b, toupper(uchar(s[i]))); 77 luaL_putchar(&b, toupper(uchar(s[i])));
@@ -77,7 +82,7 @@ static int str_upper (lua_State *L) {
77static int str_rep (lua_State *L) { 82static int str_rep (lua_State *L) {
78 size_t l; 83 size_t l;
79 luaL_Buffer b; 84 luaL_Buffer b;
80 const l_char *s = luaL_check_lstr(L, 1, &l); 85 const char *s = luaL_check_lstr(L, 1, &l);
81 int n = luaL_check_int(L, 2); 86 int n = luaL_check_int(L, 2);
82 luaL_buffinit(L, &b); 87 luaL_buffinit(L, &b);
83 while (n-- > 0) 88 while (n-- > 0)
@@ -108,9 +113,9 @@ static int str_concat (lua_State *L) {
108 113
109static int str_byte (lua_State *L) { 114static int str_byte (lua_State *L) {
110 size_t l; 115 size_t l;
111 const l_char *s = luaL_check_lstr(L, 1, &l); 116 const char *s = luaL_check_lstr(L, 1, &l);
112 sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l); 117 sint32 pos = posrelat(luaL_opt_long(L, 2, 1), l);
113 luaL_arg_check(L, 0 < pos && (size_t)(pos) <= l, 2, l_s("out of range")); 118 luaL_arg_check(L, 0 < pos && (size_t)(pos) <= l, 2, "out of range");
114 lua_pushnumber(L, uchar(s[pos-1])); 119 lua_pushnumber(L, uchar(s[pos-1]));
115 return 1; 120 return 1;
116} 121}
@@ -123,7 +128,7 @@ static int str_char (lua_State *L) {
123 luaL_buffinit(L, &b); 128 luaL_buffinit(L, &b);
124 for (i=1; i<=n; i++) { 129 for (i=1; i<=n; i++) {
125 int c = luaL_check_int(L, i); 130 int c = luaL_check_int(L, i);
126 luaL_arg_check(L, uchar(c) == c, i, l_s("invalid value")); 131 luaL_arg_check(L, uchar(c) == c, i, "invalid value");
127 luaL_putchar(&b, uchar(c)); 132 luaL_putchar(&b, uchar(c));
128 } 133 }
129 luaL_pushresult(&b); 134 luaL_pushresult(&b);
@@ -147,25 +152,25 @@ static int str_char (lua_State *L) {
147#define CAP_POSITION (-2) 152#define CAP_POSITION (-2)
148 153
149typedef struct MatchState { 154typedef struct MatchState {
150 const l_char *src_init; /* init of source string */ 155 const char *src_init; /* init of source string */
151 const l_char *src_end; /* end (`\0') of source string */ 156 const char *src_end; /* end (`\0') of source string */
152 int level; /* total number of captures (finished or unfinished) */ 157 int level; /* total number of captures (finished or unfinished) */
153 struct { 158 struct {
154 const l_char *init; 159 const char *init;
155 sint32 len; 160 sint32 len;
156 } capture[MAX_CAPTURES]; 161 } capture[MAX_CAPTURES];
157 lua_State *L; 162 lua_State *L;
158} MatchState; 163} MatchState;
159 164
160 165
161#define ESC l_c('%') 166#define ESC '%'
162#define SPECIALS l_s("^$*+?.([%-") 167#define SPECIALS "^$*+?.([%-"
163 168
164 169
165static int check_capture (MatchState *ms, int l) { 170static int check_capture (MatchState *ms, int l) {
166 l -= l_c('1'); 171 l -= '1';
167 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) 172 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
168 lua_error(ms->L, l_s("invalid capture index")); 173 lua_error(ms->L, "invalid capture index");
169 return l; 174 return l;
170} 175}
171 176
@@ -174,25 +179,25 @@ static int capture_to_close (MatchState *ms) {
174 int level = ms->level; 179 int level = ms->level;
175 for (level--; level>=0; level--) 180 for (level--; level>=0; level--)
176 if (ms->capture[level].len == CAP_UNFINISHED) return level; 181 if (ms->capture[level].len == CAP_UNFINISHED) return level;
177 lua_error(ms->L, l_s("invalid pattern capture")); 182 lua_error(ms->L, "invalid pattern capture");
178 return 0; /* to avoid warnings */ 183 return 0; /* to avoid warnings */
179} 184}
180 185
181 186
182static const l_char *luaI_classend (MatchState *ms, const l_char *p) { 187static const char *luaI_classend (MatchState *ms, const char *p) {
183 switch (*p++) { 188 switch (*p++) {
184 case ESC: 189 case ESC:
185 if (*p == l_c('\0')) 190 if (*p == '\0')
186 lua_error(ms->L, l_s("malformed pattern (ends with `%')")); 191 lua_error(ms->L, "malformed pattern (ends with `%')");
187 return p+1; 192 return p+1;
188 case l_c('['): 193 case '[':
189 if (*p == l_c('^')) p++; 194 if (*p == '^') p++;
190 do { /* look for a `]' */ 195 do { /* look for a `]' */
191 if (*p == l_c('\0')) 196 if (*p == '\0')
192 lua_error(ms->L, l_s("malformed pattern (missing `]')")); 197 lua_error(ms->L, "malformed pattern (missing `]')");
193 if (*(p++) == ESC && *p != l_c('\0')) 198 if (*(p++) == ESC && *p != '\0')
194 p++; /* skip escapes (e.g. `%]') */ 199 p++; /* skip escapes (e.g. `%]') */
195 } while (*p != l_c(']')); 200 } while (*p != ']');
196 return p+1; 201 return p+1;
197 default: 202 default:
198 return p; 203 return p;
@@ -200,28 +205,28 @@ static const l_char *luaI_classend (MatchState *ms, const l_char *p) {
200} 205}
201 206
202 207
203static int match_class (l_charint c, l_charint cl) { 208static int match_class (int c, int cl) {
204 int res; 209 int res;
205 switch (tolower(cl)) { 210 switch (tolower(cl)) {
206 case l_c('a') : res = isalpha(c); break; 211 case 'a' : res = isalpha(c); break;
207 case l_c('c') : res = iscntrl(c); break; 212 case 'c' : res = iscntrl(c); break;
208 case l_c('d') : res = isdigit(c); break; 213 case 'd' : res = isdigit(c); break;
209 case l_c('l') : res = islower(c); break; 214 case 'l' : res = islower(c); break;
210 case l_c('p') : res = ispunct(c); break; 215 case 'p' : res = ispunct(c); break;
211 case l_c('s') : res = isspace(c); break; 216 case 's' : res = isspace(c); break;
212 case l_c('u') : res = isupper(c); break; 217 case 'u' : res = isupper(c); break;
213 case l_c('w') : res = isalnum(c); break; 218 case 'w' : res = isalnum(c); break;
214 case l_c('x') : res = isxdigit(c); break; 219 case 'x' : res = isxdigit(c); break;
215 case l_c('z') : res = (c == 0); break; 220 case 'z' : res = (c == 0); break;
216 default: return (cl == c); 221 default: return (cl == c);
217 } 222 }
218 return (islower(cl) ? res : !res); 223 return (islower(cl) ? res : !res);
219} 224}
220 225
221 226
222static int matchbracketclass (l_charint c, const l_char *p, const l_char *ec) { 227static int matchbracketclass (int c, const char *p, const char *ec) {
223 int sig = 1; 228 int sig = 1;
224 if (*(p+1) == l_c('^')) { 229 if (*(p+1) == '^') {
225 sig = 0; 230 sig = 0;
226 p++; /* skip the `^' */ 231 p++; /* skip the `^' */
227 } 232 }
@@ -231,7 +236,7 @@ static int matchbracketclass (l_charint c, const l_char *p, const l_char *ec) {
231 if (match_class(c, *p)) 236 if (match_class(c, *p))
232 return sig; 237 return sig;
233 } 238 }
234 else if ((*(p+1) == l_c('-')) && (p+2 < ec)) { 239 else if ((*(p+1) == '-') && (p+2 < ec)) {
235 p+=2; 240 p+=2;
236 if (uchar(*(p-2)) <= c && c <= uchar(*p)) 241 if (uchar(*(p-2)) <= c && c <= uchar(*p))
237 return sig; 242 return sig;
@@ -242,13 +247,13 @@ static int matchbracketclass (l_charint c, const l_char *p, const l_char *ec) {
242} 247}
243 248
244 249
245static int luaI_singlematch (l_charint c, const l_char *p, const l_char *ep) { 250static int luaI_singlematch (int c, const char *p, const char *ep) {
246 switch (*p) { 251 switch (*p) {
247 case l_c('.'): /* matches any char */ 252 case '.': /* matches any char */
248 return 1; 253 return 1;
249 case ESC: 254 case ESC:
250 return match_class(c, *(p+1)); 255 return match_class(c, *(p+1));
251 case l_c('['): 256 case '[':
252 return matchbracketclass(c, p, ep-1); 257 return matchbracketclass(c, p, ep-1);
253 default: 258 default:
254 return (uchar(*p) == c); 259 return (uchar(*p) == c);
@@ -256,13 +261,13 @@ static int luaI_singlematch (l_charint c, const l_char *p, const l_char *ep) {
256} 261}
257 262
258 263
259static const l_char *match (MatchState *ms, const l_char *s, const l_char *p); 264static const char *match (MatchState *ms, const char *s, const char *p);
260 265
261 266
262static const l_char *matchbalance (MatchState *ms, const l_char *s, 267static const char *matchbalance (MatchState *ms, const char *s,
263 const l_char *p) { 268 const char *p) {
264 if (*p == 0 || *(p+1) == 0) 269 if (*p == 0 || *(p+1) == 0)
265 lua_error(ms->L, l_s("unbalanced pattern")); 270 lua_error(ms->L, "unbalanced pattern");
266 if (*s != *p) return NULL; 271 if (*s != *p) return NULL;
267 else { 272 else {
268 int b = *p; 273 int b = *p;
@@ -279,14 +284,14 @@ static const l_char *matchbalance (MatchState *ms, const l_char *s,
279} 284}
280 285
281 286
282static const l_char *max_expand (MatchState *ms, const l_char *s, 287static const char *max_expand (MatchState *ms, const char *s,
283 const l_char *p, const l_char *ep) { 288 const char *p, const char *ep) {
284 sint32 i = 0; /* counts maximum expand for item */ 289 sint32 i = 0; /* counts maximum expand for item */
285 while ((s+i)<ms->src_end && luaI_singlematch(uchar(*(s+i)), p, ep)) 290 while ((s+i)<ms->src_end && luaI_singlematch(uchar(*(s+i)), p, ep))
286 i++; 291 i++;
287 /* keeps trying to match with the maximum repetitions */ 292 /* keeps trying to match with the maximum repetitions */
288 while (i>=0) { 293 while (i>=0) {
289 const l_char *res = match(ms, (s+i), ep+1); 294 const char *res = match(ms, (s+i), ep+1);
290 if (res) return res; 295 if (res) return res;
291 i--; /* else didn't match; reduce 1 repetition to try again */ 296 i--; /* else didn't match; reduce 1 repetition to try again */
292 } 297 }
@@ -294,10 +299,10 @@ static const l_char *max_expand (MatchState *ms, const l_char *s,
294} 299}
295 300
296 301
297static const l_char *min_expand (MatchState *ms, const l_char *s, 302static const char *min_expand (MatchState *ms, const char *s,
298 const l_char *p, const l_char *ep) { 303 const char *p, const char *ep) {
299 for (;;) { 304 for (;;) {
300 const l_char *res = match(ms, s, ep+1); 305 const char *res = match(ms, s, ep+1);
301 if (res != NULL) 306 if (res != NULL)
302 return res; 307 return res;
303 else if (s<ms->src_end && luaI_singlematch(uchar(*s), p, ep)) 308 else if (s<ms->src_end && luaI_singlematch(uchar(*s), p, ep))
@@ -307,11 +312,11 @@ static const l_char *min_expand (MatchState *ms, const l_char *s,
307} 312}
308 313
309 314
310static const l_char *start_capture (MatchState *ms, const l_char *s, 315static const char *start_capture (MatchState *ms, const char *s,
311 const l_char *p, int what) { 316 const char *p, int what) {
312 const l_char *res; 317 const char *res;
313 int level = ms->level; 318 int level = ms->level;
314 if (level >= MAX_CAPTURES) lua_error(ms->L, l_s("too many captures")); 319 if (level >= MAX_CAPTURES) lua_error(ms->L, "too many captures");
315 ms->capture[level].init = s; 320 ms->capture[level].init = s;
316 ms->capture[level].len = what; 321 ms->capture[level].len = what;
317 ms->level = level+1; 322 ms->level = level+1;
@@ -321,10 +326,10 @@ static const l_char *start_capture (MatchState *ms, const l_char *s,
321} 326}
322 327
323 328
324static const l_char *end_capture (MatchState *ms, const l_char *s, 329static const char *end_capture (MatchState *ms, const char *s,
325 const l_char *p) { 330 const char *p) {
326 int l = capture_to_close(ms); 331 int l = capture_to_close(ms);
327 const l_char *res; 332 const char *res;
328 ms->capture[l].len = s - ms->capture[l].init; /* close capture */ 333 ms->capture[l].len = s - ms->capture[l].init; /* close capture */
329 if ((res = match(ms, s, p)) == NULL) /* match failed? */ 334 if ((res = match(ms, s, p)) == NULL) /* match failed? */
330 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ 335 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */
@@ -332,7 +337,7 @@ static const l_char *end_capture (MatchState *ms, const l_char *s,
332} 337}
333 338
334 339
335static const l_char *match_capture (MatchState *ms, const l_char *s, int l) { 340static const char *match_capture (MatchState *ms, const char *s, int l) {
336 size_t len; 341 size_t len;
337 l = check_capture(ms, l); 342 l = check_capture(ms, l);
338 len = ms->capture[l].len; 343 len = ms->capture[l].len;
@@ -343,15 +348,15 @@ static const l_char *match_capture (MatchState *ms, const l_char *s, int l) {
343} 348}
344 349
345 350
346static const l_char *match (MatchState *ms, const l_char *s, const l_char *p) { 351static const char *match (MatchState *ms, const char *s, const char *p) {
347 init: /* using goto's to optimize tail recursion */ 352 init: /* using goto's to optimize tail recursion */
348 switch (*p) { 353 switch (*p) {
349 case l_c('('): /* start capture */ 354 case '(': /* start capture */
350 if (*(p+1) == l_c(')')) /* position capture? */ 355 if (*(p+1) == ')') /* position capture? */
351 return start_capture(ms, s, p+2, CAP_POSITION); 356 return start_capture(ms, s, p+2, CAP_POSITION);
352 else 357 else
353 return start_capture(ms, s, p+1, CAP_UNFINISHED); 358 return start_capture(ms, s, p+1, CAP_UNFINISHED);
354 case l_c(')'): /* end capture */ 359 case ')': /* end capture */
355 return end_capture(ms, s, p+1); 360 return end_capture(ms, s, p+1);
356 case ESC: /* may be %[0-9] or %b */ 361 case ESC: /* may be %[0-9] or %b */
357 if (isdigit(uchar(*(p+1)))) { /* capture? */ 362 if (isdigit(uchar(*(p+1)))) { /* capture? */
@@ -359,33 +364,33 @@ static const l_char *match (MatchState *ms, const l_char *s, const l_char *p) {
359 if (s == NULL) return NULL; 364 if (s == NULL) return NULL;
360 p+=2; goto init; /* else return match(ms, s, p+2) */ 365 p+=2; goto init; /* else return match(ms, s, p+2) */
361 } 366 }
362 else if (*(p+1) == l_c('b')) { /* balanced string? */ 367 else if (*(p+1) == 'b') { /* balanced string? */
363 s = matchbalance(ms, s, p+2); 368 s = matchbalance(ms, s, p+2);
364 if (s == NULL) return NULL; 369 if (s == NULL) return NULL;
365 p+=4; goto init; /* else return match(ms, s, p+4); */ 370 p+=4; goto init; /* else return match(ms, s, p+4); */
366 } 371 }
367 else goto dflt; /* case default */ 372 else goto dflt; /* case default */
368 case l_c('\0'): /* end of pattern */ 373 case '\0': /* end of pattern */
369 return s; /* match succeeded */ 374 return s; /* match succeeded */
370 case l_c('$'): 375 case '$':
371 if (*(p+1) == l_c('\0')) /* is the `$' the last char in pattern? */ 376 if (*(p+1) == '\0') /* is the `$' the last char in pattern? */
372 return (s == ms->src_end) ? s : NULL; /* check end of string */ 377 return (s == ms->src_end) ? s : NULL; /* check end of string */
373 else goto dflt; 378 else goto dflt;
374 default: dflt: { /* it is a pattern item */ 379 default: dflt: { /* it is a pattern item */
375 const l_char *ep = luaI_classend(ms, p); /* points to what is next */ 380 const char *ep = luaI_classend(ms, p); /* points to what is next */
376 int m = s<ms->src_end && luaI_singlematch(uchar(*s), p, ep); 381 int m = s<ms->src_end && luaI_singlematch(uchar(*s), p, ep);
377 switch (*ep) { 382 switch (*ep) {
378 case l_c('?'): { /* optional */ 383 case '?': { /* optional */
379 const l_char *res; 384 const char *res;
380 if (m && ((res=match(ms, s+1, ep+1)) != NULL)) 385 if (m && ((res=match(ms, s+1, ep+1)) != NULL))
381 return res; 386 return res;
382 p=ep+1; goto init; /* else return match(ms, s, ep+1); */ 387 p=ep+1; goto init; /* else return match(ms, s, ep+1); */
383 } 388 }
384 case l_c('*'): /* 0 or more repetitions */ 389 case '*': /* 0 or more repetitions */
385 return max_expand(ms, s, p, ep); 390 return max_expand(ms, s, p, ep);
386 case l_c('+'): /* 1 or more repetitions */ 391 case '+': /* 1 or more repetitions */
387 return (m ? max_expand(ms, s+1, p, ep) : NULL); 392 return (m ? max_expand(ms, s+1, p, ep) : NULL);
388 case l_c('-'): /* 0 or more repetitions (minimum) */ 393 case '-': /* 0 or more repetitions (minimum) */
389 return min_expand(ms, s, p, ep); 394 return min_expand(ms, s, p, ep);
390 default: 395 default:
391 if (!m) return NULL; 396 if (!m) return NULL;
@@ -397,15 +402,15 @@ static const l_char *match (MatchState *ms, const l_char *s, const l_char *p) {
397 402
398 403
399 404
400static const l_char *lmemfind (const l_char *s1, size_t l1, 405static const char *lmemfind (const char *s1, size_t l1,
401 const l_char *s2, size_t l2) { 406 const char *s2, size_t l2) {
402 if (l2 == 0) return s1; /* empty strings are everywhere */ 407 if (l2 == 0) return s1; /* empty strings are everywhere */
403 else if (l2 > l1) return NULL; /* avoids a negative `l1' */ 408 else if (l2 > l1) return NULL; /* avoids a negative `l1' */
404 else { 409 else {
405 const l_char *init; /* to search for a `*s2' inside `s1' */ 410 const char *init; /* to search for a `*s2' inside `s1' */
406 l2--; /* 1st char will be checked by `memchr' */ 411 l2--; /* 1st char will be checked by `memchr' */
407 l1 = l1-l2; /* `s2' cannot be found after that */ 412 l1 = l1-l2; /* `s2' cannot be found after that */
408 while (l1 > 0 && (init = (const l_char *)memchr(s1, *s2, l1)) != NULL) { 413 while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
409 init++; /* 1st char is already checked */ 414 init++; /* 1st char is already checked */
410 if (memcmp(init, s2+1, l2) == 0) 415 if (memcmp(init, s2+1, l2) == 0)
411 return init-1; 416 return init-1;
@@ -421,7 +426,7 @@ static const l_char *lmemfind (const l_char *s1, size_t l1,
421 426
422static void push_onecapture (MatchState *ms, int i) { 427static void push_onecapture (MatchState *ms, int i) {
423 int l = ms->capture[i].len; 428 int l = ms->capture[i].len;
424 if (l == CAP_UNFINISHED) lua_error(ms->L, l_s("unfinished capture")); 429 if (l == CAP_UNFINISHED) lua_error(ms->L, "unfinished capture");
425 if (l == CAP_POSITION) 430 if (l == CAP_POSITION)
426 lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1); 431 lua_pushnumber(ms->L, ms->capture[i].init - ms->src_init + 1);
427 else 432 else
@@ -431,7 +436,7 @@ static void push_onecapture (MatchState *ms, int i) {
431 436
432static int push_captures (MatchState *ms) { 437static int push_captures (MatchState *ms) {
433 int i; 438 int i;
434 luaL_check_stack(ms->L, ms->level, l_s("too many captures")); 439 luaL_check_stack(ms->L, ms->level, "too many captures");
435 for (i=0; i<ms->level; i++) 440 for (i=0; i<ms->level; i++)
436 push_onecapture(ms, i); 441 push_onecapture(ms, i);
437 return ms->level; /* number of strings pushed */ 442 return ms->level; /* number of strings pushed */
@@ -440,13 +445,13 @@ static int push_captures (MatchState *ms) {
440 445
441static int str_find (lua_State *L) { 446static int str_find (lua_State *L) {
442 size_t l1, l2; 447 size_t l1, l2;
443 const l_char *s = luaL_check_lstr(L, 1, &l1); 448 const char *s = luaL_check_lstr(L, 1, &l1);
444 const l_char *p = luaL_check_lstr(L, 2, &l2); 449 const char *p = luaL_check_lstr(L, 2, &l2);
445 sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1; 450 sint32 init = posrelat(luaL_opt_long(L, 3, 1), l1) - 1;
446 luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, l_s("out of range")); 451 luaL_arg_check(L, 0 <= init && (size_t)(init) <= l1, 3, "out of range");
447 if (lua_gettop(L) > 3 || /* extra argument? */ 452 if (lua_gettop(L) > 3 || /* extra argument? */
448 strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */ 453 strpbrk(p, SPECIALS) == NULL) { /* or no special characters? */
449 const l_char *s2 = lmemfind(s+init, l1-init, p, l2); 454 const char *s2 = lmemfind(s+init, l1-init, p, l2);
450 if (s2) { 455 if (s2) {
451 lua_pushnumber(L, s2-s+1); 456 lua_pushnumber(L, s2-s+1);
452 lua_pushnumber(L, s2-s+l2); 457 lua_pushnumber(L, s2-s+l2);
@@ -455,13 +460,13 @@ static int str_find (lua_State *L) {
455 } 460 }
456 else { 461 else {
457 MatchState ms; 462 MatchState ms;
458 int anchor = (*p == l_c('^')) ? (p++, 1) : 0; 463 int anchor = (*p == '^') ? (p++, 1) : 0;
459 const l_char *s1=s+init; 464 const char *s1=s+init;
460 ms.L = L; 465 ms.L = L;
461 ms.src_init = s; 466 ms.src_init = s;
462 ms.src_end = s+l1; 467 ms.src_end = s+l1;
463 do { 468 do {
464 const l_char *res; 469 const char *res;
465 ms.level = 0; 470 ms.level = 0;
466 if ((res=match(&ms, s1, p)) != NULL) { 471 if ((res=match(&ms, s1, p)) != NULL) {
467 lua_pushnumber(L, s1-s+1); /* start */ 472 lua_pushnumber(L, s1-s+1); /* start */
@@ -478,7 +483,7 @@ static int str_find (lua_State *L) {
478static void add_s (MatchState *ms, luaL_Buffer *b) { 483static void add_s (MatchState *ms, luaL_Buffer *b) {
479 lua_State *L = ms->L; 484 lua_State *L = ms->L;
480 if (lua_isstring(L, 3)) { 485 if (lua_isstring(L, 3)) {
481 const l_char *news = lua_tostring(L, 3); 486 const char *news = lua_tostring(L, 3);
482 size_t l = lua_strlen(L, 3); 487 size_t l = lua_strlen(L, 3);
483 size_t i; 488 size_t i;
484 for (i=0; i<l; i++) { 489 for (i=0; i<l; i++) {
@@ -511,22 +516,22 @@ static void add_s (MatchState *ms, luaL_Buffer *b) {
511 516
512static int str_gsub (lua_State *L) { 517static int str_gsub (lua_State *L) {
513 size_t srcl; 518 size_t srcl;
514 const l_char *src = luaL_check_lstr(L, 1, &srcl); 519 const char *src = luaL_check_lstr(L, 1, &srcl);
515 const l_char *p = luaL_check_string(L, 2); 520 const char *p = luaL_check_string(L, 2);
516 int max_s = luaL_opt_int(L, 4, srcl+1); 521 int max_s = luaL_opt_int(L, 4, srcl+1);
517 int anchor = (*p == l_c('^')) ? (p++, 1) : 0; 522 int anchor = (*p == '^') ? (p++, 1) : 0;
518 int n = 0; 523 int n = 0;
519 MatchState ms; 524 MatchState ms;
520 luaL_Buffer b; 525 luaL_Buffer b;
521 luaL_arg_check(L, 526 luaL_arg_check(L,
522 lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)), 527 lua_gettop(L) >= 3 && (lua_isstring(L, 3) || lua_isfunction(L, 3)),
523 3, l_s("string or function expected")); 528 3, "string or function expected");
524 luaL_buffinit(L, &b); 529 luaL_buffinit(L, &b);
525 ms.L = L; 530 ms.L = L;
526 ms.src_init = src; 531 ms.src_init = src;
527 ms.src_end = src+srcl; 532 ms.src_end = src+srcl;
528 while (n < max_s) { 533 while (n < max_s) {
529 const l_char *e; 534 const char *e;
530 ms.level = 0; 535 ms.level = 0;
531 e = match(&ms, src, p); 536 e = match(&ms, src, p);
532 if (e) { 537 if (e) {
@@ -557,40 +562,40 @@ static int str_gsub (lua_State *L) {
557 562
558static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) { 563static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) {
559 size_t l; 564 size_t l;
560 const l_char *s = luaL_check_lstr(L, arg, &l); 565 const char *s = luaL_check_lstr(L, arg, &l);
561 luaL_putchar(b, l_c('"')); 566 luaL_putchar(b, '"');
562 while (l--) { 567 while (l--) {
563 switch (*s) { 568 switch (*s) {
564 case l_c('"'): case l_c('\\'): case l_c('\n'): 569 case '"': case '\\': case '\n':
565 luaL_putchar(b, l_c('\\')); 570 luaL_putchar(b, '\\');
566 luaL_putchar(b, *s); 571 luaL_putchar(b, *s);
567 break; 572 break;
568 case l_c('\0'): luaL_addlstring(b, l_s("\\000"), 4); break; 573 case '\0': luaL_addlstring(b, "\\000", 4); break;
569 default: luaL_putchar(b, *s); 574 default: luaL_putchar(b, *s);
570 } 575 }
571 s++; 576 s++;
572 } 577 }
573 luaL_putchar(b, l_c('"')); 578 luaL_putchar(b, '"');
574} 579}
575 580
576 581
577static const l_char *scanformat (lua_State *L, const l_char *strfrmt, 582static const char *scanformat (lua_State *L, const char *strfrmt,
578 l_char *form, int *hasprecision) { 583 char *form, int *hasprecision) {
579 const l_char *p = strfrmt; 584 const char *p = strfrmt;
580 while (strchr(l_s("-+ #0"), *p)) p++; /* skip flags */ 585 while (strchr("-+ #0", *p)) p++; /* skip flags */
581 if (isdigit(uchar(*p))) p++; /* skip width */ 586 if (isdigit(uchar(*p))) p++; /* skip width */
582 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 587 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
583 if (*p == l_c('.')) { 588 if (*p == '.') {
584 p++; 589 p++;
585 *hasprecision = 1; 590 *hasprecision = 1;
586 if (isdigit(uchar(*p))) p++; /* skip precision */ 591 if (isdigit(uchar(*p))) p++; /* skip precision */
587 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */ 592 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
588 } 593 }
589 if (isdigit(uchar(*p))) 594 if (isdigit(uchar(*p)))
590 lua_error(L, l_s("invalid format (width or precision too long)")); 595 lua_error(L, "invalid format (width or precision too long)");
591 if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */ 596 if (p-strfrmt+2 > MAX_FORMAT) /* +2 to include `%' and the specifier */
592 lua_error(L, l_s("invalid format (too long)")); 597 lua_error(L, "invalid format (too long)");
593 form[0] = l_c('%'); 598 form[0] = '%';
594 strncpy(form+1, strfrmt, p-strfrmt+1); 599 strncpy(form+1, strfrmt, p-strfrmt+1);
595 form[p-strfrmt+2] = 0; 600 form[p-strfrmt+2] = 0;
596 return p; 601 return p;
@@ -600,40 +605,40 @@ static const l_char *scanformat (lua_State *L, const l_char *strfrmt,
600static int str_format (lua_State *L) { 605static int str_format (lua_State *L) {
601 int arg = 1; 606 int arg = 1;
602 size_t sfl; 607 size_t sfl;
603 const l_char *strfrmt = luaL_check_lstr(L, arg, &sfl); 608 const char *strfrmt = luaL_check_lstr(L, arg, &sfl);
604 const l_char *strfrmt_end = strfrmt+sfl; 609 const char *strfrmt_end = strfrmt+sfl;
605 luaL_Buffer b; 610 luaL_Buffer b;
606 luaL_buffinit(L, &b); 611 luaL_buffinit(L, &b);
607 while (strfrmt < strfrmt_end) { 612 while (strfrmt < strfrmt_end) {
608 if (*strfrmt != l_c('%')) 613 if (*strfrmt != '%')
609 luaL_putchar(&b, *strfrmt++); 614 luaL_putchar(&b, *strfrmt++);
610 else if (*++strfrmt == l_c('%')) 615 else if (*++strfrmt == '%')
611 luaL_putchar(&b, *strfrmt++); /* %% */ 616 luaL_putchar(&b, *strfrmt++); /* %% */
612 else { /* format item */ 617 else { /* format item */
613 l_char form[MAX_FORMAT]; /* to store the format (`%...') */ 618 char form[MAX_FORMAT]; /* to store the format (`%...') */
614 l_char buff[MAX_ITEM]; /* to store the formatted item */ 619 char buff[MAX_ITEM]; /* to store the formatted item */
615 int hasprecision = 0; 620 int hasprecision = 0;
616 if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == l_c('$')) 621 if (isdigit(uchar(*strfrmt)) && *(strfrmt+1) == '$')
617 lua_error(L, l_s("obsolete `format' option (d$)")); 622 lua_error(L, "obsolete `format' option (d$)");
618 arg++; 623 arg++;
619 strfrmt = scanformat(L, strfrmt, form, &hasprecision); 624 strfrmt = scanformat(L, strfrmt, form, &hasprecision);
620 switch (*strfrmt++) { 625 switch (*strfrmt++) {
621 case l_c('c'): case l_c('d'): case l_c('i'): 626 case 'c': case 'd': case 'i':
622 sprintf(buff, form, luaL_check_int(L, arg)); 627 sprintf(buff, form, luaL_check_int(L, arg));
623 break; 628 break;
624 case l_c('o'): case l_c('u'): case l_c('x'): case l_c('X'): 629 case 'o': case 'u': case 'x': case 'X':
625 sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg))); 630 sprintf(buff, form, (unsigned int)(luaL_check_number(L, arg)));
626 break; 631 break;
627 case l_c('e'): case l_c('E'): case l_c('f'): 632 case 'e': case 'E': case 'f':
628 case l_c('g'): case l_c('G'): 633 case 'g': case 'G':
629 sprintf(buff, form, luaL_check_number(L, arg)); 634 sprintf(buff, form, luaL_check_number(L, arg));
630 break; 635 break;
631 case l_c('q'): 636 case 'q':
632 luaI_addquoted(L, &b, arg); 637 luaI_addquoted(L, &b, arg);
633 continue; /* skip the `addsize' at the end */ 638 continue; /* skip the `addsize' at the end */
634 case l_c('s'): { 639 case 's': {
635 size_t l; 640 size_t l;
636 const l_char *s = luaL_check_lstr(L, arg, &l); 641 const char *s = luaL_check_lstr(L, arg, &l);
637 if (!hasprecision && l >= 100) { 642 if (!hasprecision && l >= 100) {
638 /* no precision and string is too long to be formatted; 643 /* no precision and string is too long to be formatted;
639 keep original string */ 644 keep original string */
@@ -647,7 +652,7 @@ static int str_format (lua_State *L) {
647 } 652 }
648 } 653 }
649 default: /* also treat cases `pnLlh' */ 654 default: /* also treat cases `pnLlh' */
650 lua_error(L, l_s("invalid option in `format'")); 655 lua_error(L, "invalid option in `format'");
651 } 656 }
652 luaL_addlstring(&b, buff, strlen(buff)); 657 luaL_addlstring(&b, buff, strlen(buff));
653 } 658 }
@@ -658,17 +663,17 @@ static int str_format (lua_State *L) {
658 663
659 664
660static const luaL_reg strlib[] = { 665static const luaL_reg strlib[] = {
661{l_s("strlen"), str_len}, 666{"strlen", str_len},
662{l_s("strsub"), str_sub}, 667{"strsub", str_sub},
663{l_s("strlower"), str_lower}, 668{"strlower", str_lower},
664{l_s("strupper"), str_upper}, 669{"strupper", str_upper},
665{l_s("strchar"), str_char}, 670{"strchar", str_char},
666{l_s("strrep"), str_rep}, 671{"strrep", str_rep},
667{l_s("strbyte"), str_byte}, 672{"strbyte", str_byte},
668{l_s("concat"), str_concat}, 673{"concat", str_concat},
669{l_s("format"), str_format}, 674{"format", str_format},
670{l_s("strfind"), str_find}, 675{"strfind", str_find},
671{l_s("gsub"), str_gsub} 676{"gsub", str_gsub}
672}; 677};
673 678
674 679