Module talk:String: Difference between revisions

Content deleted Content added
 
(13 intermediate revisions by 10 users not shown)
Line 10:
| minthreadsleft=5
}}
 
== Extract number ==
 
I am trying to extract a number from a string and ignore any units. I have been using {{tl|first word}} which works in some cases.
 
*<nowiki>{{first word|1={{convert|45|m}}}}</nowiki> -> {{first word|1={{convert|45|m}}}}
*<nowiki>{{first word|1={{convert|18.6|nmi|km mi|abbr=on}}}}</nowiki> -> {{first word|1={{convert|18.6|nmi|km mi|abbr=on}}}}
 
In the second case it doesn't work. I think because a nbsp is being used instead of a space. Is there any template which will work for both? &mdash;&nbsp;Martin <small>([[User:MSGJ|MSGJ]]&nbsp;·&nbsp;[[User talk:MSGJ|talk]])</small> 08:20, 11 April 2022 (UTC)
:You can use [[Special:ExpandTemplates]] to see that convert outputs <code>18.6&amp;nbsp;nmi (34.4&amp;nbsp;km; 21.4&amp;nbsp;mi)</code> for the second case above. What is the purpose of this? If it's for convert and if you can control the parameters, using abbr=values would make selection easier. For the second case above, that would give <code>18.6 (34.4; 21.4)</code>. [[User:Johnuniq|Johnuniq]] ([[User talk:Johnuniq|talk]]) 11:01, 11 April 2022 (UTC)
::It's for importing data into Wikidata from infobox fields. I can't control the parameters because convert is used separately on each article. &mdash;&nbsp;Martin <small>([[User:MSGJ|MSGJ]]&nbsp;·&nbsp;[[User talk:MSGJ|talk]])</small> 19:36, 11 April 2022 (UTC)
 
== Template:Str rep pattern match irregularities ==
 
Editors interested in this module may be interested in the following discussion: [[Template talk:Str rep#Pattern match irregularities]]. Your feedback would be welcome. [[User:Mathglot|Mathglot]] ([[User talk:Mathglot|talk]]) 08:14, 8 October 2022 (UTC)
 
== How to do string.replace of level3 headers in transcluded wikitext ==
Line 186 ⟶ 171:
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)
: {{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)
 
:: 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)