Module:String: Difference between revisions

From NvWiki
Jump to navigation Jump to search
Fix
 
tlx
Line 1: Line 1:
--[[
{{Old TfD|date= 2020 May 8 |result=no consensus |merge=Module:HTMLDecode |disc=Module:HTMLDecode}}
{{copied|from=Module:String|from_oldid=552254999|to=:incubator:Module:Wp/nod/String|to_diff=4299113}}
{{User:HBC Archive Indexerbot/OptIn|target=/Archive index|mask=/Archive <#>|leading_zeros=0|indexhere=yes}}
{{archives|banner=yes|age=90|bot=lowercase sigmabot III}}{{User:MiszaBot/config
| algo=old(90d)
| archive=Module talk:String/Archive %(counter)d
| counter=2
| maxarchivesize=100K
| archiveheader={{Automatic archive navigator}}
| minthreadsleft=5
}}


This module is intended to provide access to basic string functions.
== Protected edit request on 25 October 2023 ==


Most of the functions provided here can be invoked with named parameters,
{{edit fully-protected|Module:String|answered=yes}}
unnamed parameters, or a mixture. If named parameters are used, Mediawiki will
Please, add '''r''' to the word '''fist''' (resulting in ''fi'''r'''st''), line number 61. [[User:Gkiyoshinishimoto|Nishimoto, Gilberto Kiyoshi]] ([[User talk:Gkiyoshinishimoto|talk]]) 18:11, 25 October 2023 (UTC)
automatically remove any leading or trailing whitespace from the parameter.
Depending on the intended use, it may be advantageous to either preserve or
remove such whitespace.


Global options
== Protected edit request on 3 September 2024 ==
    ignore_errors: If set to 'true' or 1, any error condition will result in
        an empty string being returned rather than an error message.


    error_category: If an error occurs, specifies the name of a category to
{{edit template-protected|answered=yes}}
        include with the error message.  The default category is
        [Category:Errors reported by Module String].


    no_category: If set to 'true' or 1, no category will be added if an error
All of the Lua pseudo-regex special characters are in the ASCII range. See [[:en:UTF-8#Encoding]]. Therefore, we don't need at all to use the (costly) <code>mw.ustring.*</code> functions in some parts I have reviewed.
        is generated.


Unit tests for this module are available at Module:String/tests.
My request is to replace:
]]
<syntaxhighlight lang="lua">
 
function str._escapePattern( pattern_str )
local str = {}
return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
 
--[[
len
 
This function returns the length of the target string.
 
Usage:
{{#invoke:String|len|target_string|}}
OR
{{#invoke:String|len|s=target_string}}
 
Parameters
    s: The string whose length to report
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the target string.
]]
function str.len( frame )
local new_args = str._getParameters( frame.args, {'s'} )
local s = new_args['s'] or ''
return mw.ustring.len( s )
end
end
</syntaxhighlight>


--[[
with:
sub
<syntaxhighlight lang="lua">
 
function str._escapePattern( pattern_str )
This function returns a substring of the target string at specified indices.
return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) )
 
Usage:
{{#invoke:String|sub|target_string|start_index|end_index}}
OR
{{#invoke:String|sub|s=target_string|i=start_index|j=end_index}}
 
Parameters
    s: The string to return a subset of
    i: The first index of the substring to return, defaults to 1.
    j: The last index of the string to return, defaults to the last character.
 
The first character of the string is assigned an index of 1.  If either i or j
is a negative value, it is interpreted the same as selecting a character by
counting from the end of the string.  Hence, a value of -1 is the same as
selecting the last character of the string.
 
If the requested indices are out of range for the given string, an error is
reported.
]]
function str.sub( frame )
local new_args = str._getParameters( frame.args, { 's', 'i', 'j' } )
local s = new_args['s'] or ''
local i = tonumber( new_args['i'] ) or 1
local j = tonumber( new_args['j'] ) or -1
 
local len = mw.ustring.len( s )
 
-- Convert negatives for range checking
if i < 0 then
i = len + i + 1
end
if j < 0 then
j = len + j + 1
end
 
if i > len or j > len or i < 1 or j < 1 then
return str._error( 'String subset index out of range' )
end
if j < i then
return str._error( 'String subset indices out of order' )
end
 
return mw.ustring.sub( s, i, j )
end
 
--[[
This function implements that features of {{str sub old}} and is kept in order
to maintain these older templates.
]]
function str.sublength( frame )
local i = tonumber( frame.args.i ) or 0
local len = tonumber( frame.args.len )
return mw.ustring.sub( frame.args.s, i + 1, len and ( i + len ) )
end
 
--[[
_match
 
This function returns a substring from the source string that matches a
specified pattern. It is exported for use in other modules
 
Usage:
strmatch = require("Module:String")._match
sresult = strmatch( s, pattern, start, match, plain, nomatch )
 
Parameters
    s: The string to search
    pattern: The pattern or string to find within the string
    start: The index within the source string to start the search.  The first
        character of the string has index 1.  Defaults to 1.
    match: In some cases it may be possible to make multiple matches on a single
        string.  This specifies which match to return, where the first match is
        match= 1.  If a negative number is specified then a match is returned
        counting from the last match.  Hence match = -1 is the same as requesting
        the last match.  Defaults to 1.
    plain: A flag indicating that the pattern should be understood as plain
        text.  Defaults to false.
    nomatch: If no match is found, output the "nomatch" value rather than an error.
 
For information on constructing Lua patterns, a form of [regular expression], see:
 
* http://www.lua.org/manual/5.1/manual.html#5.4.1
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
 
]]
-- This sub-routine is exported for use in other modules
function str._match( s, pattern, start, match_index, plain_flag, nomatch )
if s == '' then
return str._error( 'Target string is empty' )
end
if pattern == '' then
return str._error( 'Pattern string is empty' )
end
start = tonumber(start) or 1
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( 'Requested start is out of range' )
end
if match_index == 0 then
return str._error( 'Match index is out of range' )
end
if plain_flag then
pattern = str._escapePattern( pattern )
end
 
local result
if match_index == 1 then
-- Find first match is simple case
result = mw.ustring.match( s, pattern, start )
else
if start > 1 then
s = mw.ustring.sub( s, start )
end
 
local iterator = mw.ustring.gmatch(s, pattern)
if match_index > 0 then
-- Forward search
for w in iterator do
match_index = match_index - 1
if match_index == 0 then
result = w
break
end
end
else
-- Reverse search
local result_table = {}
local count = 1
for w in iterator do
result_table[count] = w
count = count + 1
end
 
result = result_table[ count + match_index ]
end
end
 
if result == nil then
if nomatch == nil then
return str._error( 'Match not found' )
else
return nomatch
end
else
return result
end
end
 
--[[
match
 
This function returns a substring from the source string that matches a
specified pattern.
 
Usage:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
OR
{{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index
    |match=match_number|plain=plain_flag|nomatch=nomatch_output}}
 
Parameters
    s: The string to search
    pattern: The pattern or string to find within the string
    start: The index within the source string to start the search.  The first
        character of the string has index 1.  Defaults to 1.
    match: In some cases it may be possible to make multiple matches on a single
        string.  This specifies which match to return, where the first match is
        match= 1.  If a negative number is specified then a match is returned
        counting from the last match.  Hence match = -1 is the same as requesting
        the last match.  Defaults to 1.
    plain: A flag indicating that the pattern should be understood as plain
        text.  Defaults to false.
    nomatch: If no match is found, output the "nomatch" value rather than an error.
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from each string.  In some circumstances this is desirable, in
other cases one may want to preserve the whitespace.
 
If the match_number or start_index are out of range for the string being queried, then
this function generates an error.  An error is also generated if no match is found.
If one adds the parameter ignore_errors=true, then the error will be suppressed and
an empty string will be returned on any failure.
 
For information on constructing Lua patterns, a form of [regular expression], see:
 
* http://www.lua.org/manual/5.1/manual.html#5.4.1
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
 
]]
-- This is the entry point for #invoke:String|match
function str.match( frame )
local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} )
local s = new_args['s'] or ''
local start = tonumber( new_args['start'] ) or 1
local plain_flag = str._getBoolean( new_args['plain'] or false )
local pattern = new_args['pattern'] or ''
local match_index = math.floor( tonumber(new_args['match']) or 1 )
local nomatch = new_args['nomatch']
 
return str._match( s, pattern, start, match_index, plain_flag, nomatch )
end
 
--[[
pos
 
This function returns a single character from the target string at position pos.
 
Usage:
{{#invoke:String|pos|target_string|index_value}}
OR
{{#invoke:String|pos|target=target_string|pos=index_value}}
 
Parameters
    target: The string to search
    pos: The index for the character to return
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the target string.  In some circumstances this is desirable, in
other cases one may want to preserve the whitespace.
 
The first character has an index value of 1.
 
If one requests a negative value, this function will select a character by counting backwards
from the end of the string.  In other words pos = -1 is the same as asking for the last character.
 
A requested value of zero, or a value greater than the length of the string returns an error.
]]
function str.pos( frame )
local new_args = str._getParameters( frame.args, {'target', 'pos'} )
local target_str = new_args['target'] or ''
local pos = tonumber( new_args['pos'] ) or 0
 
if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then
return str._error( 'String index out of range' )
end
 
return mw.ustring.sub( target_str, pos, pos )
end
 
--[[
str_find
 
This function duplicates the behavior of {{str_find}}, including all of its quirks.
This is provided in order to support existing templates, but is NOT RECOMMENDED for
new code and templates.  New code is recommended to use the "find" function instead.
 
Returns the first index in "source" that is a match to "target".  Indexing is 1-based,
and the function returns -1 if the "target" string is not present in "source".
 
Important Note: If the "target" string is empty / missing, this function returns a
value of "1", which is generally unexpected behavior, and must be accounted for
separatetly.
]]
function str.str_find( frame )
local new_args = str._getParameters( frame.args, {'source', 'target'} )
local source_str = new_args['source'] or ''
local target_str = new_args['target'] or ''
 
if target_str == '' then
return 1
end
 
local start = mw.ustring.find( source_str, target_str, 1, true )
if start == nil then
start = -1
end
 
return start
end
 
--[[
find
 
This function allows one to search for a target string or pattern within another
string.
 
Usage:
{{#invoke:String|find|source_str|target_string|start_index|plain_flag}}
OR
{{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}}
 
Parameters
    source: The string to search
    target: The string or pattern to find within source
    start: The index within the source string to start the search, defaults to 1
    plain: Boolean flag indicating that target should be understood as plain
        text and not as a Lua style regular expression, defaults to true
 
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the parameter.  In some circumstances this is desirable, in
other cases one may want to preserve the whitespace.
 
This function returns the first index >= "start" where "target" can be found
within "source".  Indices are 1-based.  If "target" is not found, then this
function returns 0.  If either "source" or "target" are missing / empty, this
function also returns 0.
 
This function should be safe for UTF-8 strings.
]]
function str.find( frame )
local new_args = str._getParameters( frame.args, {'source', 'target', 'start', 'plain' } )
local source_str = new_args['source'] or ''
local pattern = new_args['target'] or ''
local start_pos = tonumber(new_args['start']) or 1
local plain = new_args['plain'] or true
 
if source_str == '' or pattern == '' then
return 0
end
 
plain = str._getBoolean( plain )
 
local start = mw.ustring.find( source_str, pattern, start_pos, plain )
if start == nil then
start = 0
end
 
return start
end
end
</syntaxhighlight>


--[[
(I am also removing the capture group, which is unneeded as we can use the "%0" whole capture)
replace
 
This function allows one to replace a target string or pattern within another
string.
 
Usage:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
OR
{{#invoke:String|replace|source=source_string|pattern=pattern_string|replace=replace_string|
  count=replacement_count|plain=plain_flag}}
 
Parameters
    source: The string to search
    pattern: The string or pattern to find within source
    replace: The replacement text
    count: The number of occurences to replace, defaults to all.
    plain: Boolean flag indicating that pattern should be understood as plain
        text and not as a Lua style regular expression, defaults to true
]]
function str.replace( frame )
local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } )
local source_str = new_args['source'] or ''
local pattern = new_args['pattern'] or ''
local replace = new_args['replace'] or ''
local count = tonumber( new_args['count'] )
local plain = new_args['plain'] or true
 
if source_str == '' or pattern == '' then
return source_str
end
plain = str._getBoolean( plain )
 
if plain then
pattern = str._escapePattern( pattern )
replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.
end
 
local result
 
if count ~= nil then
result = mw.ustring.gsub( source_str, pattern, replace, count )
else
result = mw.ustring.gsub( source_str, pattern, replace )
end
 
return result
end


--[[
('''edit:''' I am also taking the opportunity, for extra robustness, to add parentheses in order to discard the 2nd value (number of replacements) returned by these gsub() functions, then subsequently by _escapePattern(). The more I encounter this "multiple values returned" Lua feature, the more I think it was a terrible design idea)
    simple function to pipe string.rep to templates.
]]
function str.rep( frame )
local repetitions = tonumber( frame.args[2] )
if not repetitions then
return str._error( 'function rep expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' )
end
return string.rep( frame.args[1] or '', repetitions )
end


--[[
Second change: line 409, we can similarly replace:
escapePattern
<syntaxhighlight lang="lua">
replace = mw.ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.
</syntaxhighlight>


This function escapes special characters from a Lua string pattern. See [1]
with:
for details on how patterns work.
<syntaxhighlight lang="lua">
replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.
</syntaxhighlight>


[1] https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
These changes would significantly decrease the overhead of having the "plain mode" enabled in this module's functions.


Usage:
[[User:Od1n|Od1n]] ([[User talk:Od1n|talk]]) 03:26, 3 September 2024 (UTC)
{{#invoke:String|escapePattern|pattern_string}}
:[{{fullurl:Module:String|diff=prev&oldid=1243840019}} 1243840019], thanks. [[User:Od1n|Od1n]] ([[User talk:Od1n|talk]]) 22:38, 3 September 2024 (UTC)


Parameters
== Protected edit request on 18 October 2024 ==
    pattern_string: The pattern string to escape.
]]
function str.escapePattern( frame )
local pattern_str = frame.args[1]
if not pattern_str then
return str._error( 'No pattern string specified' )
end
local result = str._escapePattern( pattern_str )
return result
end


--[[
{{edit fully-protected|Module:String|answered=yes}}
count
The value returned by a module function must always be a string, however some functions here return numbers (these are <code>[[Module:String#len|len]]</code>, <code>[[Module:String#str_find|str_find]]</code>, <code>[[Module:String#find|find]]</code> and <code>[[Module:String#count|count]]</code>). Could you please apply [[Special:Diff/1251805774/1251806211|this diff]]? You can just copy and paste the code at [[Special:PermanentLink/1251806211|this permanent link]].
This function counts the number of occurrences of one string in another.
]]
function str.count(frame)
local args = str._getParameters(frame.args, {'source', 'pattern', 'plain'})
local source = args.source or ''
local pattern = args.pattern or ''
local plain = str._getBoolean(args.plain or true)
if plain then
pattern = str._escapePattern(pattern)
end
local _, count = mw.ustring.gsub(source, pattern, '')
return count
end


--[[
Although unnoticeable when used in normal wikitext, this can create problems when [[Module:String]] is invoked using other modules.
endswith
This function determines whether a string ends with another string.
]]
function str.endswith(frame)
local args = str._getParameters(frame.args, {'source', 'pattern'})
local source = args.source or ''
local pattern = args.pattern or ''
if pattern == '' then
-- All strings end with the empty string.
return "yes"
end
if mw.ustring.sub(source, -mw.ustring.len(pattern), -1) == pattern then
return "yes"
else
return ""
end
end


--[[
For instance, focusing on the <code>[[Module:String#len|len]]</code> function, for each argument passed, a template named <code>mytemplate</code> containing the following code
join


Join all non empty arguments together; the first argument is the separator.
<syntaxhighlight lang="wikitext">{{#invoke:params|mapping_by_invoking|string|len|mapping_by_replacing|^.*$|%0 mod 3|1|for_each|[$#:$@]}}</syntaxhighlight>
Usage:
{{#invoke:String|join|sep|one|two|three}}
]]
function str.join(frame)
local args = {}
local sep
for _, v in ipairs( frame.args ) do
if sep then
if v ~= '' then
table.insert(args, v)
end
else
sep = v
end
end
return table.concat( args, sep or '' )
end


-- findpagetext returns the position of a piece of text in a page
should print <syntaxhighlight lang="wikitext" inline>[PARAMETER-NAME:LENGTH-OF-PARAMETER mod 3]</syntaxhighlight>
-- First positional parameter or |text is the search text
-- Optional parameter |title is the page title, defaults to current page
-- Optional parameter |plain is either true for plain search (default) or false for Lua pattern search
-- Optional parameter |nomatch is the return value when no match is found; default is nil
function str._findpagetext(args)
-- process parameters
local nomatch = args.nomatch or ""
if nomatch == "" then nomatch = nil end
--
local text = mw.text.trim(args[1] or args.text or "")
if text == "" then return nil end
--
local title = args.title or ""
local titleobj
if title == "" then
titleobj = mw.title.getCurrentTitle()
else
titleobj = mw.title.new(title)
end
--
local plain = args.plain or ""
if plain:sub(1, 1) == "f" then plain = false else plain = true end
-- get the page content and look for 'text' - return position or nomatch
local content = titleobj and titleobj:getContent()
return content and mw.ustring.find(content, text, 1, plain) or nomatch
end
function str.findpagetext(frame)
local args = frame.args
local pargs = frame:getParent().args
for k, v in pairs(pargs) do
args[k] = v
end
if not (args[1] or args.text) then return nil end
-- just the first value
return (str._findpagetext(args))
end
--[[
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters.  This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
function str._getParameters( frame_args, arg_list )
local new_args = {}
local index = 1
local value


for _, arg in ipairs( arg_list ) do
The code above invokes <code>{{mfl|string|len|...}}</code> for each parameter passed. Then it attempts to replace the lengths saved with <code>%0 mod 3</code>, i.e. by adding <code>&nbsp;mod 3</code> at the end of each parameter. And so, for instance, <syntaxhighlight lang="wikitext" inline>{{mytemplate|hello|world|foo|bar}}</syntaxhighlight> should print
value = frame_args[arg]
if value == nil then
value = frame_args[index]
index = index + 1
end
new_args[arg] = value
end


return new_args
: [1:5 mod 3][2:5 mod 3][3:3 mod 3][4:3 mod 3]
end


--[[
However, since <code>{{mfl|string|len|...}}</code> returns a number, any attempt to do string manipulation with the number returned will generate an error. --[[User:Grufo|Grufo]] ([[User talk:Grufo|talk]]) 05:17, 18 October 2024 (UTC)
Helper function to handle error messages.
: {{not done}}:<!-- Template:ESp --> {{tq|q=y|The value returned by a module function must always be a string}} is not true. [[mw:Extension:Scribunto/Lua reference manual#Returning text]] states {{tq|The module function should usually return a single string; whatever values are returned will be passed through tostring() and then concatenated with no separator.}} Further, when calling a module function from other Lua code even that doesn't apply; in that case it's like any other Lua function. I also note this change may well break other code that calls these functions (if it for some reason calls functions from this module instead of calling Scribunto's string manipulation functions directly) that expect a number from <code>len</code> or the like. [[User:Anomie|Anomie]][[User talk:Anomie|⚔]] 11:12, 18 October 2024 (UTC)
]]
function str._error( error_str )
local frame = mw.getCurrentFrame()
local error_category = frame.args.error_category or 'Errors reported by Module String'
local ignore_errors = frame.args.ignore_errors or false
local no_category = frame.args.no_category or false


if str._getBoolean(ignore_errors) then
:: Alright, it seems then that I will have to fix that in {{mfl|params|mapping_by_invoking}} and stringify whatever modules may return. --[[User:Grufo|Grufo]] ([[User talk:Grufo|talk]]) 13:35, 18 October 2024 (UTC)
return ''
end


local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>'
== Bug in <code>replace</code>: empty strings are not recognized ==
if error_category ~= '' and not str._getBoolean( no_category ) then
Hi. I noticed that the <code>[[Module:String#replace|replace]]</code> function is unable to recognize empty strings (see third example):
error_str = '[[Category:' .. error_category .. ']]' .. error_str
# <syntaxhighlight lang="wikitext" inline>{{#invoke:string|replace|Foo|^.*$|Hello|1|false}}</syntaxhighlight>
end
#: ↳ {{#invoke:string|replace|Foo|^.*$|Hello|1|false}}
# <syntaxhighlight lang="wikitext" inline>{{#invoke:string|replace|Bar|^.*$|Hello|1|false}}</syntaxhighlight>
#: ↳ {{#invoke:string|replace|Bar|^.*$|Hello|1|false}}
# <syntaxhighlight lang="wikitext" inline>{{#invoke:string|replace||^.*$|Hello|1|false}}</syntaxhighlight>
#: ↳ {{#invoke:string|replace||^.*$|Hello|1|false}}
--[[User:Grufo|Grufo]] ([[User talk:Grufo|talk]]) 10:47, 12 July 2025 (UTC)
:Because of [[Module:String#L-402--L-404|lines 402–404]]. The reasoning for that code is not, so far as I can tell, documented. There is similar code, also not documented, in <code>find()</code> but that code makes some sort of sense – find anything in an empty string should return <code>0</code>. Makes me wonder if <code>replace()</code> was created after <code>find()</code> and used <code>find()</code> as an armature upon which to construct <code>replace()</code>. Seems to me that [[Module:String#L-402|line 402]] could be rewritten as: <syntaxhighlight lang="lua" inline="1">if '' == pattern then</syntaxhighlight>.  But, are there any templates out there that rely on this anomaly?
:—[[User:Trappist the monk|Trappist the monk]] ([[User talk:Trappist the monk|talk]]) 13:24, 12 July 2025 (UTC)
::Function <code>replace()</code> [[Special:Diff/540121093|was added on 24 February 2013]], two days after [[Special:Diff/539690696|function <code>find()</code> was added]]. The early return in <code><nowiki>if source_str == '' or pattern == '' [...]</nowiki></code> was added in between those edits: [[Special:Diff/540073010]]. —⁠[[User:Andrybak|andrybak]] ([[User talk:Andrybak|talk]]) 14:10, 12 July 2025 (UTC)
:::With some work (<code><nowiki>{{#invoke:string|replace|2=^.*$|3=Hello|4=1|5=false}}</nowiki></code>), it is possible for there to be no parameter 1. I don't know what <code>_getParameters</code> would do with that but the code in <code>str.replace</code> should handle a situation where parameter 1 is nil. For convenience, the code treats nil and empty as the same and that might be part of the reasoning for returning an empty string. I agree that <code>^.*$</code> should match an empty string although, as mentioned above, it is possible that someone has taken advantage of this undocumented behavior. {{ping|WOSlinker}} Any thoughts? [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 04:37, 13 July 2025 (UTC)
::::Yes, I think I must have just copied find and updated the code to do replace. There only seems to be [https://en.wikipedia.org/w/index.php?title=Special:Search&limit=50&offset=0&ns0=1&ns1=1&ns2=1&ns3=1&ns4=1&ns5=1&ns6=1&ns7=1&ns8=1&ns9=1&ns10=1&ns11=1&ns12=1&ns13=1&ns14=1&ns15=1&ns100=1&ns101=1&ns118=1&ns119=1&ns828=1&ns829=1&search=insource%3A%2F%5C%5E%5C.%5C%2A%5C%24%2F 24 occurences] of <code>^.*$</code> so won't take long to check if the undocumented behaviour is used. -- [[User:WOSlinker|WOSlinker]] ([[User talk:WOSlinker|talk]]) 07:39, 13 July 2025 (UTC)
:::::@[[User:WOSlinker|WOSlinker]]: Unfortunately there are an arbitrary number of patterns that can match an empty string, e.g., {{code|^X*$}}, {{code|X*}}, {{code|X?}} and of course an empty string will match another empty string, etc. There are certainly better ways to replace empty strings with nonempty ones but the logic is valid. The suggestion [[User:Trappist the monk|Trappist the monk]] made is not the right solution either because it ignores the {{code|replace}} text. Instead change the {{code|lang=lua|or}} to an {{code|lang=lua|and}} and change the return from {{code|source_str}} to {{code|replace}}. In fact, another optimization would be: inside {{code|lang=lua|if plain then}} add {{code|lang=lua|1=if pattern == source_str then return replace end}}. —[[User:Uzume|Uzume]] ([[User talk:Uzume|talk]]) 19:16, 16 July 2025 (UTC)
:::::: {{Re|Trappist the monk|⁠andrybak|Johnuniq|WOSlinker|Uzume}} Any updates on this? --[[User:Grufo|Grufo]] ([[User talk:Grufo|talk]]) 12:49, 27 July 2025 (UTC)


return error_str
== How to search for vertical bar ==
end


--[[
I am having trouble searching for [[vertical bar]] (<code>|</code>) i.e., U+007C; <code>&amp;vert;</code>. (My RW app: find {{kbd|{{!}}-}} in tables to identify the beginning of a table row.) Here are some tests trying to match {{kbd|c{{!}}d}} in a string containing <code><nowiki>abc|def</nowiki></code> that do not work, but I'm not sure how to specify the vertical bar either in the pattern. (Or, for that matter, in the string in the tests below; note that some of these use &amp;vert; or &amp;#124; although they render as vbar even embedded in nowikis, so what appear to be duplicate tests below are actually different):
Helper Function to interpret boolean strings
# <code><nowiki>{{#invoke:String|match|s=abc{{!}}def  |pattern=c&vert;d |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc{{!}}def  |pattern=c&vert;d |plain=false |nomatch=0}}
]]
# <code><nowiki>{{#invoke:String|match|s=abc{{!}}def  |pattern=c&#124;d |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc{{!}}def  |pattern=c&#124;d |plain=false |nomatch=0}}
function str._getBoolean( boolean_str )
# <code><nowiki>{{#invoke:String|match|s=abc{{!}}def  |pattern=c\|d    |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc{{!}}def  |pattern=c\|d    |plain=false |nomatch=0}}
local boolean_value
# <code><nowiki>{{#invoke:String|match|s=abc&vert;def |pattern=c{{!}}d  |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc&vert;def |pattern=c{{!}}d  |plain=false |nomatch=0}}
# <code><nowiki>{{#invoke:String|match|s=abc&#124;def |pattern=c\|d    |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc&#124;def |pattern=c\|d    |plain=false |nomatch=0}}
# <code><nowiki>{{#invoke:String|match|s=abc\|def    |pattern=c\|d    |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc\|def    |pattern=c\|d    |plain=false |nomatch=0}}
# <code><nowiki>{{#invoke:String|match|s=abc|def      |pattern=c\|d    |plain=false |nomatch=0}}</nowiki></code> ⟶ {{#invoke:String|match|s=abc|def      |pattern=c\|d    |plain=false |nomatch=0}}


if type( boolean_str ) == 'string' then
According to [[mw:LUAREF#Character class]], <code>|</code> just represents itself, because it is not one of <code>^$()%.[]*+-?)</code>, but you can't place it into a pattern in <code><nowiki>{{#invoke:String|...}}</nowiki></code> because the invocation will treat it as a param separator. So, how do I search for it using string match, or in any of the other string functions that take patterns?
boolean_str = boolean_str:lower()
if boolean_str == 'false' or boolean_str == 'no' or boolean_str == '0'
or boolean_str == '' then
boolean_value = false
else
boolean_value = true
end
elseif type( boolean_str ) == 'boolean' then
boolean_value = boolean_str
else
error( 'No boolean value found' )
end
return boolean_value
end


--[[
Using transcluded file [[/vbar test data]], I was able to do it, thus:
Helper function that escapes all pattern characters so that they will be treated
* <code><nowiki>{{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false}}</nowiki></code> ⟶ {{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false}}
as plain text.
* <code><nowiki>{{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false |match=2}}</nowiki></code> ⟶ {{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false |match=2}}
]]
But I wasn't able to do it inline without transcluding a file. Is there a way? [[User:Mathglot|Mathglot]] ([[User talk:Mathglot|talk]]) 22:01, 6 October 2025 (UTC)
function str._escapePattern( pattern_str )
return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) )
end


return str
: @[[User:Mathglot|Mathglot]]: These two solutions
: #1
: <syntaxhighlight lang="wikitext">{{#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = false
| nomatch = 0
}}</syntaxhighlight>
: #2
: <syntaxhighlight lang="wikitext">{{#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = true
| nomatch = 0
}}</syntaxhighlight>
: yield:
:: {{#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = false
| nomatch = 0
}}
: and
:: {{#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = true
| nomatch = 0
}}
: Since you don't use Lua patterns, I suggest you use the second one (i.e. {{tlx|tjp2|plain|true}}). P.S. Template calls, parser functions and module invocations are expanded before parameters are passed; so if you write <code>{{mwl|!}}</code>, the <code>match</code> function will simply receive <code>&#124;</code> and will have no idea you ever called a parser function. --[[User:Grufo|Grufo]] ([[User talk:Grufo|talk]]) 22:35, 6 October 2025 (UTC)
:: Thank you for this. Actually, your top version was my initial attempt and got a match, but I couldn't tell what was being matched (i.e., the curlies and all match, but not what I was seeking) so I didn't think that was valid. But given your explanation, it sounds like it is a valid match, so that's good to know. I will actually be using Lua patterns; these are simplified examples for the question. Thanks again! [[User:Mathglot|Mathglot]] ([[User talk:Mathglot|talk]]) 22:50, 6 October 2025 (UTC)
::: @[[User:Mathglot|Mathglot]]: I am glad it helped. An important exception to the expansion order happens in substitutions. If you write,
::: <syntaxhighlight lang="wikitext">{{subst:#invoke:string | match
| s = abc{{!}}def
| pattern = c{{!}}d
| plain = true
| nomatch = 0
}}</syntaxhighlight>
::: you will still get a match, but the <code>match</code> function will actually receive <syntaxhighlight lang="wikitext" inline>{{!}}</syntaxhighlight> instead of <code>&#124;</code> (this is because substitutions are expanded before transclusions). And so, to pass <code>&#124;</code> to the <code>match</code> function in substitutions you will have to write:
::: <syntaxhighlight lang="wikitext">{{subst:#invoke:string | match
| s = abc{{subst:!}}def
| pattern = c{{subst:!}}d
| plain = true
| nomatch = 0
}}</syntaxhighlight>
::: --[[User:Grufo|Grufo]] ([[User talk:Grufo|talk]]) 16:24, 8 October 2025 (UTC)
:::: Wow, thanks for that. The core of both of your responses here should be added somewhere at [[mw:Extension:Scribunto/Lua reference manual]], as I looked all over and couldn't find anything like this about vertical bar. [[User:Mathglot|Mathglot]] ([[User talk:Mathglot|talk]]) 16:40, 8 October 2025 (UTC)
:::::That's probably because the documentation considers Lua code mostly from the perspective of Lua, rather than trying to detail all the idiosyncrasies of MediaWiki's wikitext processing before the arguments get passed into Lua. [[User:Anomie|Anomie]][[User talk:Anomie|⚔]] 00:00, 9 October 2025 (UTC)

Revision as of 17:24, 2 December 2025

Template:Old TfD Template:Copied User:HBC Archive Indexerbot/OptIn Template:ArchivesUser:MiszaBot/config

Protected edit request on 25 October 2023

Template:Edit fully-protected Please, add r to the word fist (resulting in first), line number 61. Nishimoto, Gilberto Kiyoshi (talk) 18:11, 25 October 2023 (UTC)

Protected edit request on 3 September 2024

Template:Edit template-protected

All of the Lua pseudo-regex special characters are in the ASCII range. See en:UTF-8#Encoding. Therefore, we don't need at all to use the (costly) mw.ustring.* functions in some parts I have reviewed.

My request is to replace:

function str._escapePattern( pattern_str )
	return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
end

with:

function str._escapePattern( pattern_str )
	return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) )
end

(I am also removing the capture group, which is unneeded as we can use the "%0" whole capture)

(edit: I am also taking the opportunity, for extra robustness, to add parentheses in order to discard the 2nd value (number of replacements) returned by these gsub() functions, then subsequently by _escapePattern(). The more I encounter this "multiple values returned" Lua feature, the more I think it was a terrible design idea)

Second change: line 409, we can similarly replace:

replace = mw.ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.

with:

replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.

These changes would significantly decrease the overhead of having the "plain mode" enabled in this module's functions.

Od1n (talk) 03:26, 3 September 2024 (UTC)

1243840019, thanks. Od1n (talk) 22:38, 3 September 2024 (UTC)

Protected edit request on 18 October 2024

Template:Edit fully-protected The value returned by a module function must always be a string, however some functions here return numbers (these are len, str_find, find and count). Could you please apply this diff? You can just copy and paste the code at this permanent link.

Although unnoticeable when used in normal wikitext, this can create problems when Module:String is invoked using other modules.

For instance, focusing on the len function, for each argument passed, a template named mytemplate containing the following code

{{#invoke:params|mapping_by_invoking|string|len|mapping_by_replacing|^.*$|%0 mod 3|1|for_each|[$#:$@]}}

should print [PARAMETER-NAME:LENGTH-OF-PARAMETER mod 3]

The code above invokes Template:Mfl for each parameter passed. Then it attempts to replace the lengths saved with %0 mod 3, i.e. by adding  mod 3 at the end of each parameter. And so, for instance, {{mytemplate|hello|world|foo|bar}} should print

[1:5 mod 3][2:5 mod 3][3:3 mod 3][4:3 mod 3]

However, since Template:Mfl returns a number, any attempt to do string manipulation with the number returned will generate an error. --Grufo (talk) 05:17, 18 October 2024 (UTC)

Template:Not done: Template:Tq is not true. mw:Extension:Scribunto/Lua reference manual#Returning text states Template:Tq Further, when calling a module function from other Lua code even that doesn't apply; in that case it's like any other Lua function. I also note this change may well break other code that calls these functions (if it for some reason calls functions from this module instead of calling Scribunto's string manipulation functions directly) that expect a number from len or the like. Anomie⚔ 11:12, 18 October 2024 (UTC)
Alright, it seems then that I will have to fix that in Template:Mfl and stringify whatever modules may return. --Grufo (talk) 13:35, 18 October 2024 (UTC)

Bug in replace: empty strings are not recognized

Hi. I noticed that the replace function is unable to recognize empty strings (see third example):

  1. {{#invoke:string|replace|Foo|^.*$|Hello|1|false}}
    ↳ Lua error at line 1: unexpected symbol near '{'.
  2. {{#invoke:string|replace|Bar|^.*$|Hello|1|false}}
    ↳ Lua error at line 1: unexpected symbol near '{'.
  3. {{#invoke:string|replace||^.*$|Hello|1|false}}
    ↳ Lua error at line 1: unexpected symbol near '{'.

--Grufo (talk) 10:47, 12 July 2025 (UTC)

Because of lines 402–404. The reasoning for that code is not, so far as I can tell, documented. There is similar code, also not documented, in find() but that code makes some sort of sense – find anything in an empty string should return 0. Makes me wonder if replace() was created after find() and used find() as an armature upon which to construct replace(). Seems to me that line 402 could be rewritten as: if '' == pattern then. But, are there any templates out there that rely on this anomaly?
—Trappist the monk (talk) 13:24, 12 July 2025 (UTC)
Function replace() was added on 24 February 2013, two days after function find() was added. The early return in if source_str == '' or pattern == '' [...] was added in between those edits: Special:Diff/540073010. —⁠andrybak (talk) 14:10, 12 July 2025 (UTC)
With some work ({{#invoke:string|replace|2=^.*$|3=Hello|4=1|5=false}}), it is possible for there to be no parameter 1. I don't know what _getParameters would do with that but the code in str.replace should handle a situation where parameter 1 is nil. For convenience, the code treats nil and empty as the same and that might be part of the reasoning for returning an empty string. I agree that ^.*$ should match an empty string although, as mentioned above, it is possible that someone has taken advantage of this undocumented behavior. Template:Ping Any thoughts? Johnuniq (talk) 04:37, 13 July 2025 (UTC)
Yes, I think I must have just copied find and updated the code to do replace. There only seems to be 24 occurences of ^.*$ so won't take long to check if the undocumented behaviour is used. -- WOSlinker (talk) 07:39, 13 July 2025 (UTC)
@WOSlinker: Unfortunately there are an arbitrary number of patterns that can match an empty string, e.g., Template:Code, Template:Code, Template:Code and of course an empty string will match another empty string, etc. There are certainly better ways to replace empty strings with nonempty ones but the logic is valid. The suggestion Trappist the monk made is not the right solution either because it ignores the Template:Code text. Instead change the Template:Code to an Template:Code and change the return from Template:Code to Template:Code. In fact, another optimization would be: inside Template:Code add Template:Code. —Uzume (talk) 19:16, 16 July 2025 (UTC)
Template:Re Any updates on this? --Grufo (talk) 12:49, 27 July 2025 (UTC)

How to search for vertical bar

I am having trouble searching for vertical bar (|) i.e., U+007C; &vert;. (My RW app: find Template:Kbd in tables to identify the beginning of a table row.) Here are some tests trying to match Template:Kbd in a string containing abc|def that do not work, but I'm not sure how to specify the vertical bar either in the pattern. (Or, for that matter, in the string in the tests below; note that some of these use &vert; or &#124; although they render as vbar even embedded in nowikis, so what appear to be duplicate tests below are actually different):

  1. {{#invoke:String|match|s=abc{{!}}def |pattern=c|d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  2. {{#invoke:String|match|s=abc{{!}}def |pattern=c|d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  3. {{#invoke:String|match|s=abc{{!}}def |pattern=c\|d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  4. {{#invoke:String|match|s=abc|def |pattern=c{{!}}d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  5. {{#invoke:String|match|s=abc|def |pattern=c\|d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  6. {{#invoke:String|match|s=abc\|def |pattern=c\|d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  7. {{#invoke:String|match|s=abc|def |pattern=c\|d |plain=false |nomatch=0}} ⟶ Lua error at line 1: unexpected symbol near '{'.

According to mw:LUAREF#Character class, | just represents itself, because it is not one of ^$()%.[]*+-?), but you can't place it into a pattern in {{#invoke:String|...}} because the invocation will treat it as a param separator. So, how do I search for it using string match, or in any of the other string functions that take patterns?

Using transcluded file /vbar test data, I was able to do it, thus:

  • {{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false}} ⟶ Lua error at line 1: unexpected symbol near '{'.
  • {{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false |match=2}} ⟶ Lua error at line 1: unexpected symbol near '{'.

But I wasn't able to do it inline without transcluding a file. Is there a way? Mathglot (talk) 22:01, 6 October 2025 (UTC)

@Mathglot: These two solutions
#1
{{#invoke:string | match
	| s = abc{{!}}def
	| pattern = c{{!}}d 
	| plain = false 
	| nomatch = 0
}}
#2
{{#invoke:string | match
	| s = abc{{!}}def
	| pattern = c{{!}}d 
	| plain = true 
	| nomatch = 0
}}
yield:
Lua error at line 1: unexpected symbol near '{'.
and
Lua error at line 1: unexpected symbol near '{'.
Since you don't use Lua patterns, I suggest you use the second one (i.e. {{tjp2|plain|true}}). P.S. Template calls, parser functions and module invocations are expanded before parameters are passed; so if you write Template:Mwl, the match function will simply receive | and will have no idea you ever called a parser function. --Grufo (talk) 22:35, 6 October 2025 (UTC)
Thank you for this. Actually, your top version was my initial attempt and got a match, but I couldn't tell what was being matched (i.e., the curlies and all match, but not what I was seeking) so I didn't think that was valid. But given your explanation, it sounds like it is a valid match, so that's good to know. I will actually be using Lua patterns; these are simplified examples for the question. Thanks again! Mathglot (talk) 22:50, 6 October 2025 (UTC)
@Mathglot: I am glad it helped. An important exception to the expansion order happens in substitutions. If you write,
{{subst:#invoke:string | match
	| s = abc{{!}}def
	| pattern = c{{!}}d 
	| plain = true 
	| nomatch = 0
}}
you will still get a match, but the match function will actually receive {{!}} instead of | (this is because substitutions are expanded before transclusions). And so, to pass | to the match function in substitutions you will have to write:
{{subst:#invoke:string | match
	| s = abc{{subst:!}}def
	| pattern = c{{subst:!}}d 
	| plain = true 
	| nomatch = 0
}}
--Grufo (talk) 16:24, 8 October 2025 (UTC)
Wow, thanks for that. The core of both of your responses here should be added somewhere at mw:Extension:Scribunto/Lua reference manual, as I looked all over and couldn't find anything like this about vertical bar. Mathglot (talk) 16:40, 8 October 2025 (UTC)
That's probably because the documentation considers Lua code mostly from the perspective of Lua, rather than trying to detail all the idiosyncrasies of MediaWiki's wikitext processing before the arguments get passed into Lua. Anomie⚔ 00:00, 9 October 2025 (UTC)