summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorReuben Thomas <rrt@sc3d.org>2012-10-18 13:22:15 +0100
committerReuben Thomas <rrt@sc3d.org>2012-10-18 13:22:15 +0100
commitdb3e4e5b0e5dbfcbbf47b4140a6186749616152b (patch)
tree04c632ecb80f0b821098196b063af8498626e9f6
parente3ca7bf625c105afc9165c358e859708f1d678bb (diff)
downloadlrexlib-db3e4e5b0e5dbfcbbf47b4140a6186749616152b.tar.gz
Allow to signal no replacement in gsub with a nil or false replacement.
-rw-r--r--doc/manual.txt14
-rw-r--r--src/algo.h12
2 files changed, 19 insertions, 7 deletions
diff --git a/doc/manual.txt b/doc/manual.txt
index f0c379a..ab68394 100644
--- a/doc/manual.txt
+++ b/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_.
diff --git a/src/algo.h b/src/algo.h
index 488cad7..2255070 100644
--- a/src/algo.h
+++ b/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;