mirror of
https://github.com/lua/lua.git
synced 2025-01-14 05:43:00 +08:00
new pattern item ".-";
empty patterns may be used in gsub.
This commit is contained in:
parent
bc323435ee
commit
8b7f271ea2
32
strlib.c
32
strlib.c
@ -3,7 +3,7 @@
|
|||||||
** String library to LUA
|
** String library to LUA
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char *rcs_strlib="$Id: strlib.c,v 1.33 1996/11/20 13:47:59 roberto Exp roberto $";
|
char *rcs_strlib="$Id: strlib.c,v 1.34 1996/11/22 13:08:02 roberto Exp roberto $";
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@ -195,7 +195,7 @@ static void str_ascii (void)
|
|||||||
/* pattern matching */
|
/* pattern matching */
|
||||||
|
|
||||||
#define ESC '%'
|
#define ESC '%'
|
||||||
#define SPECIALS "^$*?.([%"
|
#define SPECIALS "^$*?.([%-"
|
||||||
|
|
||||||
static char *bracket_end (char *p)
|
static char *bracket_end (char *p)
|
||||||
{
|
{
|
||||||
@ -367,6 +367,17 @@ static char *match (char *s, char *p, int level)
|
|||||||
return res;
|
return res;
|
||||||
p=ep+1; goto init; /* else return match(s, ep+1, level); */
|
p=ep+1; goto init; /* else return match(s, ep+1, level); */
|
||||||
}
|
}
|
||||||
|
case '-': { /* repetition */
|
||||||
|
char *res;
|
||||||
|
if ((res = match(s, ep+1, level)) != 0)
|
||||||
|
return res;
|
||||||
|
else if (m) {
|
||||||
|
s++;
|
||||||
|
goto init; /* return match(s+1, p, level); */
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
case '?': { /* optional */
|
case '?': { /* optional */
|
||||||
char *res;
|
char *res;
|
||||||
if (m && (res = match(s+1, ep+1, level)))
|
if (m && (res = match(s+1, ep+1, level)))
|
||||||
@ -447,20 +458,21 @@ static void str_gsub (void)
|
|||||||
char *src = lua_check_string(1, "gsub");
|
char *src = lua_check_string(1, "gsub");
|
||||||
char *p = lua_check_string(2, "gsub");
|
char *p = lua_check_string(2, "gsub");
|
||||||
lua_Object newp = lua_getparam(3);
|
lua_Object newp = lua_getparam(3);
|
||||||
int max_s = lua_opt_number(4, strlen(src), "gsub");
|
int max_s = lua_opt_number(4, strlen(src)+1, "gsub");
|
||||||
int anchor = (*p == '^') ? (p++, 1) : 0;
|
int anchor = (*p == '^') ? (p++, 1) : 0;
|
||||||
int n = 0;
|
int n = 0;
|
||||||
luaI_addchar(0);
|
luaI_addchar(0);
|
||||||
while (*src && n < max_s) {
|
while (n < max_s) {
|
||||||
char *e;
|
char *e = match(src, p, 0);
|
||||||
if ((e=match(src, p, 0)) == NULL)
|
if (e) {
|
||||||
luaI_addchar(*src++);
|
|
||||||
else {
|
|
||||||
if (e == src) lua_error("empty pattern in substitution");
|
|
||||||
add_s(newp);
|
add_s(newp);
|
||||||
src = e;
|
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
|
if (e && e>src) /* non empty match? */
|
||||||
|
src = e; /* skip it */
|
||||||
|
else if (*src)
|
||||||
|
luaI_addchar(*src++);
|
||||||
|
else break;
|
||||||
if (anchor) break;
|
if (anchor) break;
|
||||||
}
|
}
|
||||||
addstr(src);
|
addstr(src);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user