diff options
-rw-r--r-- | doc/manual.txt | 14 | ||||
-rw-r--r-- | src/algo.h | 12 |
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_. @@ -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; |