diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-18 17:15:47 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1997-06-18 17:15:47 -0300 |
commit | ea98620d98a51f3732a82b2d0fe3c4e4452f1b8a (patch) | |
tree | 4ea4125d50d8a64549d94c46a6bdb6702341b14f | |
parent | 6b78040840bbd457cc325f7a31bb5fbaf549f23f (diff) | |
download | lua-ea98620d98a51f3732a82b2d0fe3c4e4452f1b8a.tar.gz lua-ea98620d98a51f3732a82b2d0fe3c4e4452f1b8a.tar.bz2 lua-ea98620d98a51f3732a82b2d0fe3c4e4452f1b8a.zip |
new arguments for gsub
-rw-r--r-- | strlib.c | 37 |
1 files changed, 9 insertions, 28 deletions
@@ -3,7 +3,7 @@ | |||
3 | ** String library to LUA | 3 | ** String library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_strlib="$Id: strlib.c,v 1.41 1997/04/06 14:17:06 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.42 1997/06/16 20:29:59 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <stdio.h> | 9 | #include <stdio.h> |
@@ -69,28 +69,6 @@ static void addstr (char *s) | |||
69 | addnchar(s, strlen(s)); | 69 | addnchar(s, strlen(s)); |
70 | } | 70 | } |
71 | 71 | ||
72 | /* | ||
73 | ** Interface to strtok | ||
74 | */ | ||
75 | static void str_tok (void) | ||
76 | { | ||
77 | char *s1 = luaL_check_string(1); | ||
78 | char *del = luaL_check_string(2); | ||
79 | lua_Object t = lua_createtable(); | ||
80 | int i = 1; | ||
81 | /* As strtok changes s1, and s1 is "constant", make a copy of it */ | ||
82 | s1 = strcpy(strbuffer(strlen(s1+1)), s1); | ||
83 | while ((s1 = strtok(s1, del)) != NULL) { | ||
84 | lua_pushobject(t); | ||
85 | lua_pushnumber(i++); | ||
86 | lua_pushstring(s1); | ||
87 | lua_settable(); | ||
88 | s1 = NULL; /* prepare for next strtok */ | ||
89 | } | ||
90 | lua_pushobject(t); | ||
91 | lua_pushnumber(i-1); /* total number of tokens */ | ||
92 | } | ||
93 | |||
94 | 72 | ||
95 | /* | 73 | /* |
96 | ** Return the string length | 74 | ** Return the string length |
@@ -306,7 +284,7 @@ static char *match (char *s, char *p, int level) | |||
306 | return res; | 284 | return res; |
307 | } | 285 | } |
308 | case ESC: | 286 | case ESC: |
309 | if (isdigit((unsigned char)*(p+1))) { /* capture */ | 287 | if (isdigit((unsigned char)(*(p+1)))) { /* capture */ |
310 | int l = check_cap(*(p+1), level); | 288 | int l = check_cap(*(p+1), level); |
311 | if (strncmp(capture[l].init, s, capture[l].len) == 0) { | 289 | if (strncmp(capture[l].init, s, capture[l].len) == 0) { |
312 | /* return match(p+2, s+capture[l].len, level); */ | 290 | /* return match(p+2, s+capture[l].len, level); */ |
@@ -394,7 +372,7 @@ static void str_find (void) | |||
394 | } | 372 | } |
395 | } | 373 | } |
396 | 374 | ||
397 | static void add_s (lua_Object newp) | 375 | static void add_s (lua_Object newp, lua_Object table, int n) |
398 | { | 376 | { |
399 | if (lua_isstring(newp)) { | 377 | if (lua_isstring(newp)) { |
400 | char *news = lua_getstring(newp); | 378 | char *news = lua_getstring(newp); |
@@ -411,7 +389,10 @@ static void add_s (lua_Object newp) | |||
411 | lua_Object res; | 389 | lua_Object res; |
412 | struct lbuff oldbuff; | 390 | struct lbuff oldbuff; |
413 | lua_beginblock(); | 391 | lua_beginblock(); |
392 | if (lua_istable(table)) | ||
393 | lua_pushobject(table); | ||
414 | push_captures(); | 394 | push_captures(); |
395 | lua_pushnumber(n); | ||
415 | /* function may use lbuffer, so save it and create a new one */ | 396 | /* function may use lbuffer, so save it and create a new one */ |
416 | oldbuff = lbuffer; | 397 | oldbuff = lbuffer; |
417 | lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0; | 398 | lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0; |
@@ -431,15 +412,16 @@ static void str_gsub (void) | |||
431 | char *src = luaL_check_string(1); | 412 | char *src = luaL_check_string(1); |
432 | char *p = luaL_check_string(2); | 413 | char *p = luaL_check_string(2); |
433 | lua_Object newp = lua_getparam(3); | 414 | lua_Object newp = lua_getparam(3); |
434 | int max_s = (int)luaL_opt_number(4, strlen(src)+1); | 415 | lua_Object table = lua_getparam(4); |
416 | int max_s = (int)luaL_opt_number(lua_istable(table)?5:4, strlen(src)+1); | ||
435 | int anchor = (*p == '^') ? (p++, 1) : 0; | 417 | int anchor = (*p == '^') ? (p++, 1) : 0; |
436 | int n = 0; | 418 | int n = 0; |
437 | luaI_emptybuff(); | 419 | luaI_emptybuff(); |
438 | while (n < max_s) { | 420 | while (n < max_s) { |
439 | char *e = match(src, p, 0); | 421 | char *e = match(src, p, 0); |
440 | if (e) { | 422 | if (e) { |
441 | add_s(newp); | ||
442 | n++; | 423 | n++; |
424 | add_s(newp, table, n); | ||
443 | } | 425 | } |
444 | if (e && e>src) /* non empty match? */ | 426 | if (e && e>src) /* non empty match? */ |
445 | src = e; /* skip it */ | 427 | src = e; /* skip it */ |
@@ -527,7 +509,6 @@ static void str_format (void) | |||
527 | 509 | ||
528 | 510 | ||
529 | static struct luaL_reg strlib[] = { | 511 | static struct luaL_reg strlib[] = { |
530 | {"strtok", str_tok}, | ||
531 | {"strlen", str_len}, | 512 | {"strlen", str_len}, |
532 | {"strsub", str_sub}, | 513 | {"strsub", str_sub}, |
533 | {"strset", str_set}, | 514 | {"strset", str_set}, |