aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-11-20 11:47:59 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-11-20 11:47:59 -0200
commit78e454d864a83b1f4c99b7deb3c1c10d6e91fdc6 (patch)
treee4d8368e283fd9c75b0da77c3e654b0f63478be7
parentdbfe28e1995d6ecd73b5048e71d72c7e1e7d5462 (diff)
downloadlua-78e454d864a83b1f4c99b7deb3c1c10d6e91fdc6.tar.gz
lua-78e454d864a83b1f4c99b7deb3c1c10d6e91fdc6.tar.bz2
lua-78e454d864a83b1f4c99b7deb3c1c10d6e91fdc6.zip
BUG: ISO chars are negative, ISO ints are not.
new "balanced" pattern.
-rw-r--r--strlib.c36
1 files changed, 30 insertions, 6 deletions
diff --git a/strlib.c b/strlib.c
index d7b9d73f..cb5537c2 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,7 +3,7 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.31 1996/10/31 20:18:05 roberto Exp roberto $"; 6char *rcs_strlib="$Id: strlib.c,v 1.32 1996/11/07 20:26:19 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
@@ -238,7 +238,7 @@ static int matchclass (int c, int cl)
238 238
239int singlematch (int c, char *p) 239int singlematch (int c, char *p)
240{ 240{
241 if (c <= 0) return 0; /* \0, EOF or other strange flags */ 241 if (c == 0) return 0;
242 switch (*p) { 242 switch (*p) {
243 case '.': return 1; 243 case '.': return 1;
244 case ESC: return matchclass(c, *(p+1)); 244 case ESC: return matchclass(c, *(p+1));
@@ -300,6 +300,21 @@ static int capture_to_close (int level)
300 return 0; /* to avoid warnings */ 300 return 0; /* to avoid warnings */
301} 301}
302 302
303static char *matchbalance (char *s, int b, int e)
304{
305 if (*s != b) return NULL;
306 else {
307 int cont = 1;
308 while (*(++s)) {
309 if (*s == e) {
310 if (--cont == 0) return s+1;
311 }
312 else if (*s == b) cont++;
313 }
314 }
315 return NULL; /* string ends out of balance */
316}
317
303static char *match (char *s, char *p, int level) 318static char *match (char *s, char *p, int level)
304{ 319{
305 init: /* using goto's to optimize tail recursion */ 320 init: /* using goto's to optimize tail recursion */
@@ -317,16 +332,25 @@ static char *match (char *s, char *p, int level)
317 capture[l].len = -1; /* undo capture */ 332 capture[l].len = -1; /* undo capture */
318 return res; 333 return res;
319 } 334 }
320 case ESC: /* possibly a capture (if followed by a digit) */ 335 case ESC:
321 if (!isdigit(*(p+1))) goto dflt; 336 if (isdigit(*(p+1))) { /* capture */
322 else {
323 int l = check_cap(*(p+1), level); 337 int l = check_cap(*(p+1), level);
324 if (strncmp(capture[l].init, s, capture[l].len) == 0) { 338 if (strncmp(capture[l].init, s, capture[l].len) == 0) {
325 /* return match(p+2, s+capture[l].len, level); */ 339 /* return match(p+2, s+capture[l].len, level); */
326 p+=2; s+=capture[l].len; goto init; 340 p+=2; s+=capture[l].len; goto init;
327 } 341 }
328 else return NULL; 342 else return NULL;
329 } 343 }
344 else if (*(p+1) == 'b') { /* balanced string */
345 if (*(p+2) == 0 || *(p+3) == 0)
346 lua_error("bad balanced pattern specification");
347 s = matchbalance(s, *(p+2), *(p+3));
348 if (s == NULL) return NULL;
349 else { /* return match(p+4, s, level); */
350 p+=4; goto init;
351 }
352 }
353 else goto dflt;
330 case '\0': case '$': /* (possibly) end of pattern */ 354 case '\0': case '$': /* (possibly) end of pattern */
331 if (*p == 0 || (*(p+1) == 0 && *s == 0)) { 355 if (*p == 0 || (*(p+1) == 0 && *s == 0)) {
332 num_captures = level; 356 num_captures = level;