diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-24 14:41:41 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2019-04-24 14:41:41 -0300 |
commit | c65605151c5a335baf0a9ea251b19df5b2d3a905 (patch) | |
tree | e85f4f108e452abe87f0f30e137e4f0fa5bb45f3 /lauxlib.c | |
parent | 3da34a5fa70a51f0cf06d677a4f07b470693260c (diff) | |
download | lua-c65605151c5a335baf0a9ea251b19df5b2d3a905.tar.gz lua-c65605151c5a335baf0a9ea251b19df5b2d3a905.tar.bz2 lua-c65605151c5a335baf0a9ea251b19df5b2d3a905.zip |
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 (limited to 'lauxlib.c')
-rw-r--r-- | lauxlib.c | 20 |
1 files changed, 13 insertions, 7 deletions
@@ -951,18 +951,24 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, | |||
951 | } | 951 | } |
952 | 952 | ||
953 | 953 | ||
954 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | 954 | LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, |
955 | const char *r) { | 955 | const char *p, const char *r) { |
956 | const char *wild; | 956 | const char *wild; |
957 | size_t l = strlen(p); | 957 | size_t l = strlen(p); |
958 | luaL_Buffer b; | ||
959 | luaL_buffinit(L, &b); | ||
960 | while ((wild = strstr(s, p)) != NULL) { | 958 | while ((wild = strstr(s, p)) != NULL) { |
961 | luaL_addlstring(&b, s, wild - s); /* push prefix */ | 959 | luaL_addlstring(b, s, wild - s); /* push prefix */ |
962 | luaL_addstring(&b, r); /* push replacement in place of pattern */ | 960 | luaL_addstring(b, r); /* push replacement in place of pattern */ |
963 | s = wild + l; /* continue after 'p' */ | 961 | s = wild + l; /* continue after 'p' */ |
964 | } | 962 | } |
965 | luaL_addstring(&b, s); /* push last suffix */ | 963 | luaL_addstring(b, s); /* push last suffix */ |
964 | } | ||
965 | |||
966 | |||
967 | LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, | ||
968 | const char *p, const char *r) { | ||
969 | luaL_Buffer b; | ||
970 | luaL_buffinit(L, &b); | ||
971 | luaL_addgsub(&b, s, p, r); | ||
966 | luaL_pushresult(&b); | 972 | luaL_pushresult(&b); |
967 | return lua_tostring(L, -1); | 973 | return lua_tostring(L, -1); |
968 | } | 974 | } |