Jump to content

Template talk:Str rep

Page contents not supported in other languages.
From Wikipedia, the free encyclopedia

Untitled

[edit]
  • {{Str rep|{{PAGENAME}}|Str|-String-}} = -String- rep ; should be : « -String- rep » ;
  • {{Str rep|{{PAGENAME}}|rep|-replace-}} = Str -replace- ; should be : « Str -replace- ».

« Str rep » = « First character rep ».

Budelberger (   ) 18:18, 17 January 2011 (UTC).[reply]

 Done (Du moins j'espère…) Budelberger (   ) 22:32, 17 January 2011 (UTC).[reply]

all occurrences?

[edit]

Hi, is it possible to replace all occurrences of a particular string with another string? I need to replace all white spaces in a parameter for a template that I'm working on. Thanks Benjamin Good (talk) 22:53, 5 October 2011 (UTC)[reply]

Created: template to replace all occurrences

[edit]

I have created {{str rep all}} {{replace}}, that replaces all occurences (not just the first one). If someone thinks they could be merged, feel free. If that would require to set |count=999 or even worse, a blank, that would be less fine (because the editor would need to use a code). -DePiep (talk) 22:04, 26 February 2013 (UTC)[reply]

Replace of math-Tags

[edit]

Hi, i'm trying to find a way to replace math tags in a string. But this lua based function is not able to replace them, because strings containing math tags are some kind of coded in lua. Same problem with nowiki.

  • Some text ABC
  • Some text '"`UNIQ--$-00000003-QINU`"'
  • Some text '"`UNIQ--$-00000004-QINU`"'
  • Some text ABC

Has anyone an idea how to fix this? --Cepheiden (talk) 14:24, 9 May 2013 (UTC)[reply]

"Template:Str repc" listed at Redirects for discussion

[edit]

A discussion is taking place to address the redirect Template:Str repc. The discussion will occur at Wikipedia:Redirects for discussion/Log/2021 April 26#Template:Str repc until a consensus is reached, and readers of this page are welcome to contribute to the discussion. User:GKFXtalk 22:50, 26 April 2021 (UTC)[reply]

Pattern match irregularities

[edit]

I'm finding a couple of irregularities with pattern matching in {{str rep}}, one having to do with trailing blanks, and the other with period/full stop character. Starting with trailing blanks: Unlike most templates for which trailing blanks in positional params are meaningless, that is not the case here. I'm still trying to figure out whether the locus of the problem is here or in the module, but consider the following cases, and note the different results for all three:

  • a. {{str rep|Dogs chase cats. Cats chase mice.|chase|love}} ⟶ Dogs love cats. Cats chase mice. # expected
  • b. {{str rep|Dogs chase cats. Cats chase mice.|chase |love}} ⟶ Dogs lovecats. Cats chase mice. # matches trailing blank, and eats it
  • c. {{str rep|Dogs chase cats. Cats chase mice.|chase |love}} ⟶ Dogs chase cats. Cats chase mice. # 2 trailing blanks: no match

Note that in the pattern arg, the trailing blank(s), if any, are interpreted as part of the pattern, and not stripped as trailing blanks in template params normally are, thus affecting the result.

Note: If it isn't clear in your browser, b) has one blank after param2 chase, and c) has two blanks after it. At a minimum, these differences in interpretation of blanks in the pattern arg need to be called out on the /doc page, perhaps in a new, "Limitations" section, or if there's some footnote in the Scribunto Lua regex doc page on Meta that I missed and it's actually expected behavior, then perhaps just an explanatory footnote here explaining the importance of trailing blanks, and a link to Meta or wherever it is that explains this behavior.

Sidebar concerning the use of {{tlg}} to embed the same examples as above; it *does* strip blanks, and hides the problem

This is a bit of a sidebar, and may have more to do with how {{tlg}} works than the problem with {{str rep}} I'm trying to illustrate, but fwiw: Note that when I first tried to demonstrate the trailing blanks in this section, I at first used the {{tlg}} template with |_show_result=y, because it's convenient (shorter and easier). But that corrupted my examples, which is to say, they stopped failing and worked as expected, because tlg *does strip* the trailing blanks, and so gives different results and fails to show the actual problem I've demonstrated in the examples above:

  • a'. {{str rep|Dogs chase cats. Cats chase mice.|chase|love}} → Dogs love cats. Cats chase mice.
  • b'. {{str rep|Dogs chase cats. Cats chase mice.|chase|love}} → Dogs love cats. Cats chase mice.
  • c'. {{str rep|Dogs chase cats. Cats chase mice.|chase|love}} → Dogs love cats. Cats chase mice.

It looks like {{tlg}} has done the work of ignoring/stripping blanks (as expected), so by the time {{str rep}} is evaluated on the right, the blanks are gone, and all three return the same result. This is more like what I expected in the top three examples, so in a sense is a pairwise, 3 by 3 minimal pair with the application of {{tlg}} being the only varying element.

I'd appreciate any comments on whether this (top three examples) is expected behavior or not, and if not, where the locus of the problem lies. Adding @Redrose64, PrimeHunter, Frietjes, and DePiep:.

The other case involves strings ending in period/full stop that fail to match a pattern that omits the trailing full stop, even if there's an exact match on all preceding characters. I'm not ready with minimal pair examples yet to document this adequately, but Example 5 at Template:Uw-copyright-new/sandbox/doc#Examples provides a clue for half the story, showing a successful match which would otherwise fail without the full stop at the end of the pattern; but that example is layered with all sorts of extra stuff between it and {{str rep}}, so is not really a good example for what is or may be happening with full stop here. Mathglot (talk) 08:01, 8 October 2022 (UTC)[reply]

Expanding the demo set: use |2= ..., which turns positional argument into named argument. Added: "" in LH code for illustration, not in the active RH template demo.
  • a2. {{str rep|1=Dogs chase cats. Cats chase mice.|2=chase|3=love}} ⟶ Dogs love cats. Cats chase mice. (# same as a.)
  • b2. {{str rep|1=Dogs chase cats. Cats chase mice.|2=chase|3=love}} ⟶ Dogs love cats. Cats chase mice. (# different from b.)
  • c2. {{str rep|1=Dogs chase cats. Cats chase mice.|2=chase␣␣|3=love}} ⟶ Dogs love cats. Cats chase mice. (# different from c.)
I note: the source code for {{str rep}} has this: {{#invoke:String|replace|source={{{1}}}|{{{2}}}|{{{3}}}|1}}. Using |source= for |[1]= this way, but not for |[2], [3]= may be another cause for unexpected results (wrt trailing space(s) in |source= vs. |[1]=).
-DePiep (talk) 08:43, 8 October 2022 (UTC)[reply]
This template ({{str rep}}) has wikitext which trims leading and trailing spaces from parameter 1 (the "source"). However, parameters 2 and 3 (pattern and replacement respectively) are not trimmed. Example b searches for chase  with a single trailing space and replaces the first occurrence with love. Example c searches for chase   with two trailing spaces. That is not found so no change occurs. Johnuniq (talk) 08:52, 8 October 2022 (UTC)[reply]
Thanks for these comments. I'm also noticing now that Module:String#rep shows different results based on trailing blanks. So there's a precedent at least for Modules, that trailing blanks are significant; although I thought there was a statement that for templates they were stripped; need to find that. Maybe templates which are wrappers for String modules are a case apart, and just need /doc changes to clarify this point. Mathglot (talk) 08:56, 8 October 2022 (UTC)[reply]
Okay, found something at H:PARAMETER, which says:

Whitespace characters (spaces, tabs, returns) are stripped from the beginnings and ends of named parameter names and values, but not from the middle: thus {{ ... | myparam = this is a test }} has the same effect as {{ ... |myparam=this is a test}}. This does not apply to unnamed parameters, where all whitespace characters are preserved.

but users of templates which invoke string templates where this applies can't be expected to know all that, so I think an appropriate update to the documentation of str rep to mention this, along with a wikilink to that section, may be sufficient. Do others agree? It certainly threw me for a loop. The "irregularities" in the section title, are really just the differences between how named and unnamed parameters handle trailing blanks, and my unfamiliarity with H:PARAMETER. But that probably deserves clarification in the /doc page of any template where it's an issue, perhaps with a link to Help:Manipulating strings#Automatically trimmed whitespace. Mathglot (talk) 19:51, 8 October 2022 (UTC)[reply]
Does the |source={{{1|}}} code also has effect on internal multi-whitespace? (to test: "|Dogs chase␣␣cats. Cats chase mice."). -DePiep (talk) 09:14, 8 October 2022 (UTC)[reply]

Thanks for all the feedback. As a result, I have rewritten the /doc for Template:Str rep to take into account what I've learned here, and also for general clarity. Please have a look at Template:Str rep/doc, and let me know what you think, or just fix it up as needed. A good bit of the added material isn't specific to {{str rep}} but applies more generally to any of the template wrappers of String module routines, and it might be worth isolating the common part into a documentation template on its own, so it could be easily transcluded onto the /doc page of any of the wrappers. Mathglot (talk) 01:39, 9 October 2022 (UTC)[reply]

A series of edits by DePiep leaving the /doc much improved; thanks! Any thoughts as to whether we should move some of this (especially, subsection § Details and the two following) to a documentation template on its own, which may then be useful (and easy) to include in quite a few template wrappers of Module:String functions? Mathglot (talk) 06:01, 10 October 2022 (UTC)[reply]
That would be very welcome, provided that that doc is superior & comprehensive in description & helping (say, re "whitespace and parameters"). A HELP: page? I don't have time to prepare or proofread such a page (lots of tests & demo's to be done, then: explain it ... phew). For me, this means I'll have to do spot research when I need one. Which is why I circumcodevent these issues usually. DePiep (talk) 06:10, 10 October 2022 (UTC)[reply]
What doesn't help is that even module:String is inconsistent with parameter naming (eg using |string, s, [1]=, and its handling thereof; gleichgeschalted or not?). Does H:PARAMETER note this? DePiep (talk) 06:11, 10 October 2022 (UTC)[reply]
(edit conflict)Yeah, I've been bitten by that |s= vs |source= thing a few times...
Oh, one thing I just noticed: can you have a look at the doc text where it refers to examples? Formerly, there was a minimal white-space pair in examples 1 & 2, that are now in examples 1 & 4 but still referred to as "1 & 2" in § White space. I'd like to keep 1 & 4 adjacent, but moving only #4 up to 2 would disrupt the unnamed-named pair in 4 & 5, so maybe move them both up? Btw, not familiar with circumcodevent; a portmanteau coinage of code and circumvent, perhaps? I think I get what it means. Mathglot (talk) 06:23, 10 October 2022 (UTC) [reply]
Can I ask you to fix these things to your own idea? And yes, that is what 'circumcode' it is: prevent arriving at the issue in the 1st place. I try to never use these double spaces, or even sole prefix/suffix spaces, in code. Just the tought of having(!) to /doc that for the my-template users... It could be css-formatting right away anyway. DePiep (talk) 06:39, 10 October 2022 (UTC)[reply]
No problem; just wanted to give you first refusal, because I didn't want to step on your design if you had a particular order in mind. I'll have a look, most likely tomorrow. And I agree; there are a lot of finicky "features" of template coding, and this isn't close to the worst of it. mvg, Mathglot (talk) 06:53, 10 October 2022 (UTC)[reply]