Module talk:String
Template:Old TfD Lua error in package.lua at line 80: module 'Module:Message box' not found. 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: <syntaxhighlight lang="lua"> function str._escapePattern( pattern_str ) return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" ) end </syntaxhighlight>
with: <syntaxhighlight lang="lua"> function str._escapePattern( pattern_str ) return ( string.gsub( pattern_str, "[%(%)%.%%%+%-%*%?%[%^%$%]]", "%%%0" ) ) end </syntaxhighlight>
(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: <syntaxhighlight lang="lua"> replace = mw.ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. </syntaxhighlight>
with: <syntaxhighlight lang="lua"> replace = string.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences. </syntaxhighlight>
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
<syntaxhighlight lang="wikitext"></syntaxhighlight>
should print <syntaxhighlight lang="wikitext" inline>[PARAMETER-NAME:LENGTH-OF-PARAMETER mod 3]</syntaxhighlight>
The code above invokes {{#invoke:string|len|...}} 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, <syntaxhighlight lang="wikitext" inline>Template:Mytemplate</syntaxhighlight> should print
- [1:5 mod 3][2:5 mod 3][3:3 mod 3][4:3 mod 3]
However, since {{#invoke:string|len|...}} 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
lenor the like. Anomie⚔ 11:12, 18 October 2024 (UTC)
- Alright, it seems then that I will have to fix that in {{#invoke:params|mapping_by_invoking}} 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):
- <syntaxhighlight lang="wikitext" inline>Hello</syntaxhighlight>
- ↳ Hello
- <syntaxhighlight lang="wikitext" inline>Hello</syntaxhighlight>
- ↳ Hello
- <syntaxhighlight lang="wikitext" inline></syntaxhighlight>
- ↳
--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 return0. Makes me wonder ifreplace()was created afterfind()and usedfind()as an armature upon which to constructreplace(). Seems to me that 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? - —Trappist the monk (talk) 13:24, 12 July 2025 (UTC)
- Function
replace()was added on 24 February 2013, two days after functionfind()was added. The early return inif 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_getParameterswould do with that but the code instr.replaceshould 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. @WOSlinker: 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., <syntaxhighlight lang="text" class="" style="" inline="1">^X*$</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">X*</syntaxhighlight>, <syntaxhighlight lang="text" class="" style="" inline="1">X?</syntaxhighlight> 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 <syntaxhighlight lang="text" class="" style="" inline="1">replace</syntaxhighlight> text. Instead change the <syntaxhighlight lang="lua" class="" style="" inline="1">or</syntaxhighlight> to an <syntaxhighlight lang="lua" class="" style="" inline="1">and</syntaxhighlight> and change the return from <syntaxhighlight lang="text" class="" style="" inline="1">source_str</syntaxhighlight> to <syntaxhighlight lang="text" class="" style="" inline="1">replace</syntaxhighlight>. In fact, another optimization would be: inside <syntaxhighlight lang="lua" class="" style="" inline="1">if plain then</syntaxhighlight> add <syntaxhighlight lang="lua" class="" style="" inline="1">if pattern == source_str then return replace end</syntaxhighlight>. —Uzume (talk) 19:16, 16 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
- With some work (
- Function
How to search for vertical bar
I am having trouble searching for vertical bar (|) i.e., U+007C; |. (My RW app: find |- in tables to identify the beginning of a table row.) Here are some tests trying to match c|d 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 | or | although they render as vbar even embedded in nowikis, so what appear to be duplicate tests below are actually different):
{{#invoke:String|match|s=abc{{!}}def |pattern=c|d |plain=false |nomatch=0}}⟶ 0{{#invoke:String|match|s=abc{{!}}def |pattern=c|d |plain=false |nomatch=0}}⟶ 0{{#invoke:String|match|s=abc{{!}}def |pattern=c\|d |plain=false |nomatch=0}}⟶ 0{{#invoke:String|match|s=abc|def |pattern=c{{!}}d |plain=false |nomatch=0}}⟶ 0{{#invoke:String|match|s=abc|def |pattern=c\|d |plain=false |nomatch=0}}⟶ 0{{#invoke:String|match|s=abc\|def |pattern=c\|d |plain=false |nomatch=0}}⟶ c\{{#invoke:String|match|s=abc|def |pattern=c\|d |plain=false |nomatch=0}}⟶ 0
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}}⟶ String Module Error: Match not found{{#invoke:String|match|s={{/vbar test data}}|pattern=[a-z]{{!}}[a-z] |plain=false |match=2}}⟶ String Module Error: Match not found
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
- <syntaxhighlight lang="wikitext">c|d</syntaxhighlight>
- #2
- <syntaxhighlight lang="wikitext">c|d</syntaxhighlight>
- yield:
- c|d
- and
- c|d
- 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{{!}}, thematchfunction 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,
- <syntaxhighlight lang="wikitext">{{subst:#invoke:string | match
- 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)
| s = abc|def | pattern = c|d | plain = true | nomatch = 0 }}</syntaxhighlight>
- you will still get a match, but the
matchfunction will actually receive <syntaxhighlight lang="wikitext" inline>|</syntaxhighlight> instead of|(this is because substitutions are expanded before transclusions). And so, to pass|to thematchfunction in substitutions you will have to write: - <syntaxhighlight lang="wikitext">{{subst:#invoke:string | match
- you will still get a match, but the
| s = abc{{subst:!}}def | pattern = c{{subst:!}}d | plain = true | nomatch = 0 }}</syntaxhighlight>
- --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)
- --Grufo (talk) 16:24, 8 October 2025 (UTC)