aboutsummaryrefslogtreecommitdiff
path: root/lstrlib.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-03-23 15:08:26 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2016-03-23 15:08:26 -0300
commite747491b9670683bee11ad5443d0362f9fda22a4 (patch)
treed87bb5381055ed97e30ea904f2e7478dd30398ac /lstrlib.c
parentacff3ad88db13bb6d74170a3db475a4e45ddb51f (diff)
downloadlua-e747491b9670683bee11ad5443d0362f9fda22a4.tar.gz
lua-e747491b9670683bee11ad5443d0362f9fda22a4.tar.bz2
lua-e747491b9670683bee11ad5443d0362f9fda22a4.zip
comments
Diffstat (limited to 'lstrlib.c')
-rw-r--r--lstrlib.c24
1 files changed, 12 insertions, 12 deletions
diff --git a/lstrlib.c b/lstrlib.c
index c07cdbb7..ec757284 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lstrlib.c,v 1.240 2016/02/25 19:42:55 roberto Exp roberto $ 2** $Id: lstrlib.c,v 1.241 2016/03/23 17:12:17 roberto Exp roberto $
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*/
@@ -777,12 +777,12 @@ static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
777 777
778static int str_gsub (lua_State *L) { 778static int str_gsub (lua_State *L) {
779 size_t srcl, lp; 779 size_t srcl, lp;
780 const char *src = luaL_checklstring(L, 1, &srcl); 780 const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
781 const char *p = luaL_checklstring(L, 2, &lp); 781 const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
782 int tr = lua_type(L, 3); 782 int tr = lua_type(L, 3); /* replacement type */
783 lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); 783 lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
784 int anchor = (*p == '^'); 784 int anchor = (*p == '^');
785 lua_Integer n = 0; 785 lua_Integer n = 0; /* replacement count */
786 MatchState ms; 786 MatchState ms;
787 luaL_Buffer b; 787 luaL_Buffer b;
788 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || 788 luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
@@ -795,16 +795,16 @@ static int str_gsub (lua_State *L) {
795 prepstate(&ms, L, src, srcl, p, lp); 795 prepstate(&ms, L, src, srcl, p, lp);
796 while (n < max_s) { 796 while (n < max_s) {
797 const char *e; 797 const char *e;
798 reprepstate(&ms); 798 reprepstate(&ms); /* (re)prepare state for new match */
799 if ((e = match(&ms, src, p)) != NULL) { 799 if ((e = match(&ms, src, p)) != NULL) { /* match? */
800 n++; 800 n++;
801 add_value(&ms, &b, src, e, tr); 801 add_value(&ms, &b, src, e, tr); /* add replacement to buffer */
802 } 802 }
803 if (e && e>src) /* non empty match? */ 803 if (e && e>src) /* non empty match? */
804 src = e; /* skip it */ 804 src = e; /* skip it */
805 else if (src < ms.src_end) 805 else if (src < ms.src_end) /* otherwise, skip one character */
806 luaL_addchar(&b, *src++); 806 luaL_addchar(&b, *src++);
807 else break; 807 else break; /* end of subject */
808 if (anchor) break; 808 if (anchor) break;
809 } 809 }
810 luaL_addlstring(&b, src, ms.src_end-src); 810 luaL_addlstring(&b, src, ms.src_end-src);