commit c65605151c5a335baf0a9ea251b19df5b2d3a905
parent 3da34a5fa70a51f0cf06d677a4f07b470693260c
Author: Roberto Ierusalimschy <roberto@inf.puc-rio.br>
Date: Wed, 24 Apr 2019 14:41:14 -0300
New function 'luaL_addgsub'
Added a new function 'luaL_addgsub', similar to 'luaL_gsub' but that
adds its result directly to a preexisting buffer, avoiding the creation
of one extra intermediate string. Also added two simple macros,
'luaL_bufflen' and 'luaL_buffaddr', to query the current length
and the contents address of a buffer.
Diffstat:
3 files changed, 58 insertions(+), 13 deletions(-)
diff --git a/lauxlib.c b/lauxlib.c
@@ -951,18 +951,24 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname,
}
-LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p,
- const char *r) {
+LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
+ const char *p, const char *r) {
const char *wild;
size_t l = strlen(p);
- luaL_Buffer b;
- luaL_buffinit(L, &b);
while ((wild = strstr(s, p)) != NULL) {
- luaL_addlstring(&b, s, wild - s); /* push prefix */
- luaL_addstring(&b, r); /* push replacement in place of pattern */
+ luaL_addlstring(b, s, wild - s); /* push prefix */
+ luaL_addstring(b, r); /* push replacement in place of pattern */
s = wild + l; /* continue after 'p' */
}
- luaL_addstring(&b, s); /* push last suffix */
+ luaL_addstring(b, s); /* push last suffix */
+}
+
+
+LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
+ const char *p, const char *r) {
+ luaL_Buffer b;
+ luaL_buffinit(L, &b);
+ luaL_addgsub(&b, s, p, r);
luaL_pushresult(&b);
return lua_tostring(L, -1);
}
diff --git a/lauxlib.h b/lauxlib.h
@@ -19,6 +19,8 @@
#define LUA_GNAME "_G"
+typedef struct luaL_Buffer luaL_Buffer;
+
/* extra error code for 'luaL_loadfilex' */
#define LUA_ERRFILE (LUA_ERRERR+1)
@@ -99,8 +101,10 @@ LUALIB_API lua_State *(luaL_newstate) (void);
LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx);
-LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
- const char *r);
+LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s,
+ const char *p, const char *r);
+LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s,
+ const char *p, const char *r);
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
@@ -155,7 +159,7 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname,
** =======================================================
*/
-typedef struct luaL_Buffer {
+struct luaL_Buffer {
char *b; /* buffer address */
size_t size; /* buffer size */
size_t n; /* number of characters in buffer */
@@ -164,7 +168,11 @@ typedef struct luaL_Buffer {
LUAI_MAXALIGN; /* ensure maximum alignment for buffer */
char b[LUAL_BUFFERSIZE]; /* initial buffer */
} init;
-} luaL_Buffer;
+};
+
+
+#define luaL_bufflen(bf) ((bf)->n)
+#define luaL_buffaddr(bf) ((bf)->b)
#define luaL_addchar(B,c) \
diff --git a/manual/manual.of b/manual/manual.of
@@ -591,7 +591,7 @@ controls how long the collector waits before starting a new cycle.
The collector starts a new cycle when the use of memory
hits @M{n%} of the use after the previous collection.
Larger values make the collector less aggressive.
-Values less than 100 mean the collector will not wait to
+Values equal to or less than 100 mean the collector will not wait to
start a new cycle.
A value of 200 means that the collector waits for the total memory in use
to double before starting a new cycle.
@@ -4928,6 +4928,18 @@ Adds the byte @id{c} to the buffer @id{B}
}
+@APIEntry{
+const void luaL_addgsub (luaL_Buffer *B, const char *s,
+ const char *p, const char *r);|
+@apii{0,0,m}
+
+Adds a copy of the string @id{s} to the buffer @id{B},
+replacing any occurrence of the string @id{p}
+with the string @id{r}.
+@seeC{luaL_Buffer}.
+
+}
+
@APIEntry{void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);|
@apii{?,?,m}
@@ -5070,6 +5082,15 @@ plus the final string on its top.
}
+@APIEntry{char *luaL_buffaddr (luaL_Buffer *B);|
+@apii{0,0,-}
+
+Returns the address of the current contents of buffer @id{B}.
+Note that any addition to the buffer may invalidate this address.
+@seeC{luaL_Buffer}.
+
+}
+
@APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);|
@apii{0,0,-}
@@ -5080,6 +5101,14 @@ the buffer must be declared as a variable
}
+@APIEntry{size_t luaL_bufflen (luaL_Buffer *B);|
+@apii{0,0,-}
+
+Returns the length of the current contents of buffer @id{B}.
+@seeC{luaL_Buffer}.
+
+}
+
@APIEntry{char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);|
@apii{?,?,m}
@@ -5935,6 +5964,7 @@ This option can be followed by three numbers:
the garbage-collector pause,
the step multiplier,
and the step size.
+A zero means to not change that value.
}
@item{@St{generational}|
@@ -5942,6 +5972,7 @@ Change the collector mode to generational.
This option can be followed by two numbers:
the garbage-collector minor multiplier
and the major multiplier.
+A zero means to not change that value.
}
@item{@St{isrunning}|
@@ -6552,7 +6583,7 @@ the value of the environment variable @defid{LUA_PATH_5_4} or
the environment variable @defid{LUA_PATH} or
with a default path defined in @id{luaconf.h},
if those environment variables are not defined.
-Any @St{;;} in the value of the environment variable
+A @St{;;} in the value of the environment variable
is replaced by the default path.
}