Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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

  • Loading branch information...
commit db3e4e5b0e5dbfcbbf47b4140a6186749616152b 1 parent e3ca7bf
@rrthomas rrthomas authored
Showing with 19 additions and 7 deletions.
  1. +10 −4 doc/manual.txt
  2. +9 −3 src/algo.h
View
14 doc/manual.txt
@@ -230,7 +230,8 @@ below).
+---------+-----------------------------------+-------------------------+-------------+
| patt |regular expression pattern |string or userdata | n/a |
+---------+-----------------------------------+-------------------------+-------------+
- | repl |substitution source |string, function or table| n/a |
+ | repl |substitution source |string, function, table, | n/a |
+ | | |``false`` or ``nil`` | |
+---------+-----------------------------------+-------------------------+-------------+
| [n] |maximum number of matches to search| number or function | ``nil`` |
| |for, or control function, or nil | | |
@@ -248,9 +249,9 @@ below).
3. Number of substitutions made.
**Details:**
- The parameter *repl* can be either a string, a function or a table.
- On each match made, it is converted into a value *repl_out* that may be used
- for the replacement.
+ The parameter *repl* can be either a string, a function, a table,
+ ``false`` or ``nil``. On each match made, it is converted into a
+ value *repl_out* that may be used for the replacement.
*repl_out* is generated differently depending on the type of *repl*:
@@ -289,6 +290,11 @@ below).
same rules as for the return value of *repl* call, described in the above
paragraph.
+ 4. If *repl* is ``false`` or ``nil``, no replacement is done. Note
+ that, unusually for Lua, if ``repl`` is absent, it is not taken
+ to be ``nil``. This is to prevent programming errors caused by
+ inadvertently missing out *repl*.
+
Note: Under some circumstances, the value of *repl_out* may be ignored; see
below_.
View
12 src/algo.h
@@ -172,8 +172,10 @@ static void checkarg_gsub (lua_State *L, TArgComp *argC, TArgExec *argE) {
lua_tostring (L, 3); /* converts number (if any) to string */
argE->reptype = lua_type (L, 3);
if (argE->reptype != LUA_TSTRING && argE->reptype != LUA_TTABLE &&
- argE->reptype != LUA_TFUNCTION) {
- luaL_typerror (L, 3, "string, table or function");
+ argE->reptype != LUA_TFUNCTION && argE->reptype != LUA_TNIL &&
+ (argE->reptype != LUA_TBOOLEAN ||
+ (argE->reptype == LUA_TBOOLEAN && lua_toboolean (L, 3)))) {
+ luaL_typerror (L, 3, "string, table, function, false or nil");
}
argE->funcpos = 3;
argE->funcpos2 = 4;
@@ -334,7 +336,11 @@ static int algf_gsub (lua_State *L) {
}
}
/*----------------------------------------------------------------*/
- if (argE.reptype != LUA_TSTRING) {
+ else if (argE.reptype == LUA_TNIL || argE.reptype == LUA_TBOOLEAN) {
+ buffer_addlstring (pBuf, argE.text + from, to - from);
+ }
+ /*----------------------------------------------------------------*/
+ if (argE.reptype == LUA_TTABLE || argE.reptype == LUA_TFUNCTION) {
if (lua_tostring (L, -1)) {
buffer_addvalue (pBuf, -1);
curr_subst = 1;
Please sign in to comment.
Something went wrong with that request. Please try again.