Content deleted Content added
No edit summary Tag: Reverted |
|||
(8 intermediate revisions by 7 users not shown) | |||
Line 1:
{{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}}
Line 173:
:: 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)
== Bug in <code>replace</code>: empty strings are not recognized ==
Hi. I noticed that the <code>[[Module:String#replace|replace]]</code> function is unable to recognize empty strings (see third example):
# <syntaxhighlight lang="wikitext" inline>{{#invoke:string|replace|Foo|^.*$|Hello|1|false}}</syntaxhighlight>
#: ↳ {{#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)
|