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 | |
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.
-rw-r--r-- | lauxlib.c | 20 | ||||
-rw-r--r-- | lauxlib.h | 16 | ||||
-rw-r--r-- | manual/manual.of | 35 |
3 files changed, 58 insertions, 13 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 | } |
@@ -19,6 +19,8 @@ | |||
19 | #define LUA_GNAME "_G" | 19 | #define LUA_GNAME "_G" |
20 | 20 | ||
21 | 21 | ||
22 | typedef struct luaL_Buffer luaL_Buffer; | ||
23 | |||
22 | 24 | ||
23 | /* extra error code for 'luaL_loadfilex' */ | 25 | /* extra error code for 'luaL_loadfilex' */ |
24 | #define LUA_ERRFILE (LUA_ERRERR+1) | 26 | #define LUA_ERRFILE (LUA_ERRERR+1) |
@@ -99,8 +101,10 @@ LUALIB_API lua_State *(luaL_newstate) (void); | |||
99 | 101 | ||
100 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); | 102 | LUALIB_API lua_Integer (luaL_len) (lua_State *L, int idx); |
101 | 103 | ||
102 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, | 104 | LUALIB_API void luaL_addgsub (luaL_Buffer *b, const char *s, |
103 | const char *r); | 105 | const char *p, const char *r); |
106 | LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, | ||
107 | const char *p, const char *r); | ||
104 | 108 | ||
105 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); | 109 | LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup); |
106 | 110 | ||
@@ -155,7 +159,7 @@ LUALIB_API void (luaL_requiref) (lua_State *L, const char *modname, | |||
155 | ** ======================================================= | 159 | ** ======================================================= |
156 | */ | 160 | */ |
157 | 161 | ||
158 | typedef struct luaL_Buffer { | 162 | struct luaL_Buffer { |
159 | char *b; /* buffer address */ | 163 | char *b; /* buffer address */ |
160 | size_t size; /* buffer size */ | 164 | size_t size; /* buffer size */ |
161 | size_t n; /* number of characters in buffer */ | 165 | size_t n; /* number of characters in buffer */ |
@@ -164,7 +168,11 @@ typedef struct luaL_Buffer { | |||
164 | LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ | 168 | LUAI_MAXALIGN; /* ensure maximum alignment for buffer */ |
165 | char b[LUAL_BUFFERSIZE]; /* initial buffer */ | 169 | char b[LUAL_BUFFERSIZE]; /* initial buffer */ |
166 | } init; | 170 | } init; |
167 | } luaL_Buffer; | 171 | }; |
172 | |||
173 | |||
174 | #define luaL_bufflen(bf) ((bf)->n) | ||
175 | #define luaL_buffaddr(bf) ((bf)->b) | ||
168 | 176 | ||
169 | 177 | ||
170 | #define luaL_addchar(B,c) \ | 178 | #define luaL_addchar(B,c) \ |
diff --git a/manual/manual.of b/manual/manual.of index 24ac45ae..5f265708 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
@@ -591,7 +591,7 @@ controls how long the collector waits before starting a new cycle. | |||
591 | The collector starts a new cycle when the use of memory | 591 | The collector starts a new cycle when the use of memory |
592 | hits @M{n%} of the use after the previous collection. | 592 | hits @M{n%} of the use after the previous collection. |
593 | Larger values make the collector less aggressive. | 593 | Larger values make the collector less aggressive. |
594 | Values less than 100 mean the collector will not wait to | 594 | Values equal to or less than 100 mean the collector will not wait to |
595 | start a new cycle. | 595 | start a new cycle. |
596 | A value of 200 means that the collector waits for the total memory in use | 596 | A value of 200 means that the collector waits for the total memory in use |
597 | to double before starting a new cycle. | 597 | to double before starting a new cycle. |
@@ -4928,6 +4928,18 @@ Adds the byte @id{c} to the buffer @id{B} | |||
4928 | 4928 | ||
4929 | } | 4929 | } |
4930 | 4930 | ||
4931 | @APIEntry{ | ||
4932 | const void luaL_addgsub (luaL_Buffer *B, const char *s, | ||
4933 | const char *p, const char *r);| | ||
4934 | @apii{0,0,m} | ||
4935 | |||
4936 | Adds a copy of the string @id{s} to the buffer @id{B}, | ||
4937 | replacing any occurrence of the string @id{p} | ||
4938 | with the string @id{r}. | ||
4939 | @seeC{luaL_Buffer}. | ||
4940 | |||
4941 | } | ||
4942 | |||
4931 | @APIEntry{void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);| | 4943 | @APIEntry{void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l);| |
4932 | @apii{?,?,m} | 4944 | @apii{?,?,m} |
4933 | 4945 | ||
@@ -5070,6 +5082,15 @@ plus the final string on its top. | |||
5070 | 5082 | ||
5071 | } | 5083 | } |
5072 | 5084 | ||
5085 | @APIEntry{char *luaL_buffaddr (luaL_Buffer *B);| | ||
5086 | @apii{0,0,-} | ||
5087 | |||
5088 | Returns the address of the current contents of buffer @id{B}. | ||
5089 | Note that any addition to the buffer may invalidate this address. | ||
5090 | @seeC{luaL_Buffer}. | ||
5091 | |||
5092 | } | ||
5093 | |||
5073 | @APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| | 5094 | @APIEntry{void luaL_buffinit (lua_State *L, luaL_Buffer *B);| |
5074 | @apii{0,0,-} | 5095 | @apii{0,0,-} |
5075 | 5096 | ||
@@ -5080,6 +5101,14 @@ the buffer must be declared as a variable | |||
5080 | 5101 | ||
5081 | } | 5102 | } |
5082 | 5103 | ||
5104 | @APIEntry{size_t luaL_bufflen (luaL_Buffer *B);| | ||
5105 | @apii{0,0,-} | ||
5106 | |||
5107 | Returns the length of the current contents of buffer @id{B}. | ||
5108 | @seeC{luaL_Buffer}. | ||
5109 | |||
5110 | } | ||
5111 | |||
5083 | @APIEntry{char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);| | 5112 | @APIEntry{char *luaL_buffinitsize (lua_State *L, luaL_Buffer *B, size_t sz);| |
5084 | @apii{?,?,m} | 5113 | @apii{?,?,m} |
5085 | 5114 | ||
@@ -5935,6 +5964,7 @@ This option can be followed by three numbers: | |||
5935 | the garbage-collector pause, | 5964 | the garbage-collector pause, |
5936 | the step multiplier, | 5965 | the step multiplier, |
5937 | and the step size. | 5966 | and the step size. |
5967 | A zero means to not change that value. | ||
5938 | } | 5968 | } |
5939 | 5969 | ||
5940 | @item{@St{generational}| | 5970 | @item{@St{generational}| |
@@ -5942,6 +5972,7 @@ Change the collector mode to generational. | |||
5942 | This option can be followed by two numbers: | 5972 | This option can be followed by two numbers: |
5943 | the garbage-collector minor multiplier | 5973 | the garbage-collector minor multiplier |
5944 | and the major multiplier. | 5974 | and the major multiplier. |
5975 | A zero means to not change that value. | ||
5945 | } | 5976 | } |
5946 | 5977 | ||
5947 | @item{@St{isrunning}| | 5978 | @item{@St{isrunning}| |
@@ -6552,7 +6583,7 @@ the value of the environment variable @defid{LUA_PATH_5_4} or | |||
6552 | the environment variable @defid{LUA_PATH} or | 6583 | the environment variable @defid{LUA_PATH} or |
6553 | with a default path defined in @id{luaconf.h}, | 6584 | with a default path defined in @id{luaconf.h}, |
6554 | if those environment variables are not defined. | 6585 | if those environment variables are not defined. |
6555 | Any @St{;;} in the value of the environment variable | 6586 | A @St{;;} in the value of the environment variable |
6556 | is replaced by the default path. | 6587 | is replaced by the default path. |
6557 | 6588 | ||
6558 | } | 6589 | } |