Module talk:String2
Add topic
One2a with fractions
[edit]When I try and use the One2a wrapper with convert on a faction such as {{one2a|{{convert|1/2|acre|spell=in}}}} it produces "a-half acre (0.20 ha)" with a hyphen, which isn't grammatically correct. Is there a way to change this to be a space? Thanks, --Voello talk 13:55, 15 January 2022 (UTC)
- There is no good support for all variations like that. The simplest would be to give up and use:
a half acre ({{convert|1/2|acre|disp=out}})→ a half acre (0.20 ha)
- Johnuniq (talk) 23:27, 15 January 2022 (UTC)
Fix ucfirst when using × or 32nd
[edit]This edit request has been answered. Set the |answered= parameter to no to reactivate your request. |
Currently ucfirst fails if you try to use it on for example 32nd or using html entites such as ×. Please update module with code from sandbox that fixes this. All ucfirst-testcases should be green afterwards. Tholme (talk) 15:31, 12 May 2022 (UTC)
Done Looks good to me – thanks! User:GKFXtalk 12:04, 14 May 2022 (UTC)
Redirects in findpagetext
[edit]If a page gets renamed, findpagetext no longer finds the text, and feels kinda lost. Can it be made to follow redirects (either using module:redirect or by itself)? — Guarapiranga ☎ 03:22, 7 June 2022 (UTC)
ucfirst()
[edit]Re: this discussion (permalink) at Wikipedia:Help desk.
I have tweaked ucfirst() in Module:String2/sandbox to account for that case. I have tweaked Module:String2/testcases to show that I didn't break anything and to show that the ~/sandbox version correctly renders the string of wikilinks where the piped link is not the first link.
Comments desired. Without anyone comments, I shall update the live module from the sandbox.
—Trappist the monk (talk) 00:07, 25 February 2024 (UTC)
- Good. See my edit at Module:String2/sandbox for a trivial issue. My head is not quite up to parsing the regexes at the moment but surely if first_text exists, that means it is not piped and you don't need to check for that? I think that's what is happening? Several of the mw.ustring could be plain string (faster) but that might be a bit tricky for subsequent editors. I suspect finding the start and end of the lowercase letter and replacing it with sub would be more efficient than gsub but I'm not up to that... Johnuniq (talk) 01:02, 25 February 2024 (UTC)
- Thanks for the fix. Everything I write from scratch and everything that I maintain has
require ('strict'). I forget that other modules don't always use that. I've added it to this sandbox. - The code doesn't actually check for unpiped links but does check to see if the link we found is a piped link – we want to upcase the piped link's display text, not the piped link's link text. If not a piped link, we fall into the code that upcases the unpiped link's link text.
mw.ustring.<whatever>()because unicode characters are allowed as article titles. I can imagine non-English redirect links for example.- You may be right with regard to
mw.ustring.sub()vsmw.ustring.gsub()but I have never really likedmw.ustring.sub()because to me, it is more cryptic than patterns. I don't suppose it really matters that much because I suspect thatucfirst()isn't used all that often. - —Trappist the monk (talk) 02:00, 25 February 2024 (UTC)
- Oh. I had a look at what aroused my suspicion and see that I was imagining the "extract" regex at line 26 included a pipe in the "not these characters" part so it only found an unpiped link. A hallucination from non-artificial intelligence. Johnuniq (talk) 04:51, 25 February 2024 (UTC)
- Thanks for the fix. Everything I write from scratch and everything that I maintain has
- In the time since my last post, I have hacked some more on ~/sandbox. I did that because it seemed odd to me that
ucfirst()had specific code to handle<li>but no other tags. Why just that tag? So I've hacked ~/sandbox so thatucfirst()upcases the first letter character that is not in a stripmarker, an html-like tag, an html character or decimal/hexadecimal numeric entity, and strips the various list markup and miscellaneous punctuation. For example, non-English text wrapped in a{{lang}}template has multiple leading html tags:{{lang|es|casa}}→<span title="Spanish-language text"><i lang="es">casa</i></span>→ casa
- So we want the 'c' in casa to be uppercased:
{{#invoke:String2/sandbox|ucfirst|{{lang|es|casa}}}}→ Casa
- The live version of the function can't handle that:
{{#invoke:String2|ucfirst|{{lang|es|casa}}}}→ Casa
- Miscellaneous other examples:
- House ←
{{#invoke:String2/sandbox|ucfirst|*house}} - 'House ←
{{#invoke:String2/sandbox|ucfirst|*{{'}}house}}–{{'}}→<span class="nowrap" style="padding-left:0.1em;">'</span> - House ←
{{#invoke:String2/sandbox|ucfirst|*''house''}} - House ←
{{#invoke:String2/sandbox|ucfirst|*'''house'''}} - 'House' ←
{{#invoke:String2/sandbox|ucfirst|*''''house''''}}– malformed markup - House ←
{{#invoke:String2/sandbox|ucfirst|*'''''house'''''}}
- Anything I've missed? Is there anything glaringly wrong with the implementation?
- —Trappist the monk (talk) 17:27, 3 March 2024 (UTC)
- There having been no comment (and after a bit of a fumble), I have updated the live module from the sandbox.
- —Trappist the monk (talk) 18:48, 10 March 2024 (UTC)
- Thanks! Johnuniq (talk) 01:09, 11 March 2024 (UTC)
GetParameters dependency
[edit]I noticed that Module:String specifically does not have any dependencies, presumably so that it can be easily copied to other wikis. However, this module does, and the dependency it specifically requires is Module:GetParameters, which Module:String also uses: specifically, that module copies the function directly in its source to avoid any dependencies. (see: _str.getParameters)
I think it would be nice to do this also for this module, unless I'm misunderstanding the reasoning behind the original decision to omit it from Module:String. ClarFonThey (talk) 02:52, 5 July 2025 (UTC)
- The original decision had to do with risk levels, IIRC; Module:String was one of the most-used modules on the project with 11.7 million uses, and at the time it wasn't wanted to give a then-obscure module so many uses. Since then Module:String2 has also become high-risk, though, so that's moot. I personally think we should take a few of the higher-use functions here and move them to Module:String so this module doesn't end up used so widely. * Pppery * it has begun... 19:05, 7 July 2025 (UTC)
Performance
[edit]I created a wiki page that consisted of 10,000 invocations to trim a string, reloaded (purged) the page a couple of times and checked the CPU time usage in the generated HTML source code. Three different ways to trim 10,000 strings:
{{trim| foo }}: roughly between 0.5 seconds and 1.0 seconds{{#invoke|String2|trim| foo }}: between 2.5 seconds and 5.0 seconds{{#if:1| foo }}: between 0.1 seconds and 0.25 seconds
I guess the bad performance of {{#invoke|String2|trim| foo }} is due to module loading. If you care about performance, you should probably use {{trim| foo }} instead. Or if you really care, use the {{#if:1| foo }} trick. — Chrisahn (talk) 10:00, 4 July 2026 (UTC)
Requests
[edit]This edit request has been answered. Set the |answered= parameter to no to reactivate your request. |
- The function
_getParameters()should not be exported, it should be local. I fixed it in the sandbox. See Special:Diff/1362498383. Please apply to the main template. Thanks! — Chrisahn (talk) 10:12, 4 July 2026 (UTC) - The functions _findpagetext, _escapePattern, _getBoolean and hyphen_to_dash should be local instead of exported. Special:Diff/1362503371. — Chrisahn (talk) 11:42, 4 July 2026 (UTC)
- The function isInteger() is unused and undocumented and should be removed. (It was used in Template:Start date until a year ago.) I removed it from the sandbox. See Special:Diff/1362504312. — Chrisahn (talk) 11:44, 4 July 2026 (UTC)
- The function _urldecode() is unused and should be removed. I removed it from the sandbox. See Special:Diff/1362503490. — Chrisahn (talk) 11:46, 4 July 2026 (UTC)
- How do you know that these functions are not used by another module? — Martin (MSGJ · talk) 21:38, 9 July 2026 (UTC)
- User:Chrisahn, please see the question by MSGJ above. Disabling the request for now. —andrybak (talk) 13:51, 26 July 2026 (UTC)