Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow to signal no replacement in gsub with a nil or false replacement.

  • Loading branch information...
commit db3e4e5b0e5dbfcbbf47b4140a6186749616152b 1 parent e3ca7bf
Reuben Thomas rrthomas authored

Showing 2 changed files with 19 additions and 7 deletions. Show diff stats Hide diff stats

  1. +10 4 doc/manual.txt
  2. +9 3 src/algo.h
14 doc/manual.txt
@@ -230,7 +230,8 @@ below).
230 230 +---------+-----------------------------------+-------------------------+-------------+
231 231 | patt |regular expression pattern |string or userdata | n/a |
232 232 +---------+-----------------------------------+-------------------------+-------------+
233   - | repl |substitution source |string, function or table| n/a |
  233 + | repl |substitution source |string, function, table, | n/a |
  234 + | | |``false`` or ``nil`` | |
234 235 +---------+-----------------------------------+-------------------------+-------------+
235 236 | [n] |maximum number of matches to search| number or function | ``nil`` |
236 237 | |for, or control function, or nil | | |
@@ -248,9 +249,9 @@ below).
248 249 3. Number of substitutions made.
249 250
250 251 **Details:**
251   - The parameter *repl* can be either a string, a function or a table.
252   - On each match made, it is converted into a value *repl_out* that may be used
253   - for the replacement.
  252 + The parameter *repl* can be either a string, a function, a table,
  253 + ``false`` or ``nil``. On each match made, it is converted into a
  254 + value *repl_out* that may be used for the replacement.
254 255
255 256 *repl_out* is generated differently depending on the type of *repl*:
256 257
@@ -289,6 +290,11 @@ below).
289 290 same rules as for the return value of *repl* call, described in the above
290 291 paragraph.
291 292
  293 + 4. If *repl* is ``false`` or ``nil``, no replacement is done. Note
  294 + that, unusually for Lua, if ``repl`` is absent, it is not taken
  295 + to be ``nil``. This is to prevent programming errors caused by
  296 + inadvertently missing out *repl*.
  297 +
292 298 Note: Under some circumstances, the value of *repl_out* may be ignored; see
293 299 below_.
294 300
12 src/algo.h
@@ -172,8 +172,10 @@ static void checkarg_gsub (lua_State *L, TArgComp *argC, TArgExec *argE) {
172 172 lua_tostring (L, 3); /* converts number (if any) to string */
173 173 argE->reptype = lua_type (L, 3);
174 174 if (argE->reptype != LUA_TSTRING && argE->reptype != LUA_TTABLE &&
175   - argE->reptype != LUA_TFUNCTION) {
176   - luaL_typerror (L, 3, "string, table or function");
  175 + argE->reptype != LUA_TFUNCTION && argE->reptype != LUA_TNIL &&
  176 + (argE->reptype != LUA_TBOOLEAN ||
  177 + (argE->reptype == LUA_TBOOLEAN && lua_toboolean (L, 3)))) {
  178 + luaL_typerror (L, 3, "string, table, function, false or nil");
177 179 }
178 180 argE->funcpos = 3;
179 181 argE->funcpos2 = 4;
@@ -334,7 +336,11 @@ static int algf_gsub (lua_State *L) {
334 336 }
335 337 }
336 338 /*----------------------------------------------------------------*/
337   - if (argE.reptype != LUA_TSTRING) {
  339 + else if (argE.reptype == LUA_TNIL || argE.reptype == LUA_TBOOLEAN) {
  340 + buffer_addlstring (pBuf, argE.text + from, to - from);
  341 + }
  342 + /*----------------------------------------------------------------*/
  343 + if (argE.reptype == LUA_TTABLE || argE.reptype == LUA_TFUNCTION) {
338 344 if (lua_tostring (L, -1)) {
339 345 buffer_addvalue (pBuf, -1);
340 346 curr_subst = 1;

0 comments on commit db3e4e5

Please sign in to comment.
Something went wrong with that request. Please try again.