Jump to content

Template:Str left/doc: Difference between revisions

m
move used in system template to top
(text editing)
 
m (move used in system template to top)
Line 1:
{{used in system}}
{{Documentation subpage}}
<!-- Categories go where indicated at the bottom of this page, please; interwikis go to Wikidata (see also: [[Wikipedia:Wikidata]]). -->
 
== Usage ==
: {{TlxTemplate link expanded|Str left|<string>|<count>}}
 
Gives the resultant <count> of characters ''creating a substring of characters'' from the start of the [[w:Trim (programming)|trimmed]] string (i.e. the substring returned will have length <count>, exclusive of leading whitespace characters, which are trimmed first before <count> is invoked).
Line 24 ⟶ 28:
All the limitations of use for this template and shown below are caused by the current implementation of the <nowiki>{{padleft:}}</nowiki> parser function used in this template, which does not filter its third parameter containing the characters used for padding a string to the specified length (it currently counts incorrectly the characters to extract from the padding string, and incorrectly measures its effective length; in addition this parameter is limited to 500 bytes).
 
The following examples are demonstrating that this template isdoes not safe withinterpret character entity references foundas the character they represent. It treats them as multiple characters inand itscan stringleave parameterthem :truncated.
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&amp;nbsp;c | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a&nbsp;c | 3 }}
:*: ThisThe should displayoutput "a&nbsp;c" may have been intended, but the named character entity was partially truncated, leaving no length available for the final "c".
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&amp;#66;c | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a&#66;c | 3 }}
:*: This should display "abc", but the named character entity was partially truncated, leaving no length available for the final "c".
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a c | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a c | 3 }}
:*: The result is correct, as expected.
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&amp;#32;c | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a&#32;c | 3 }}
:*: This last example should be equivalent to the previous one, but it is not.
 
This template will erase nowiki tags without counting their length:
The following example shows that the presence of some wiki markup that should be transparent and ignored, does not allow this template to work as expected (the nowiki section is replaced by a unique index string in the effective runtime value of the parameter; this unique (43-bytes long) string starts by a control character (which will be finally filtered out from the generated HTML page if it remains in the result of this template), followed by "UNIQ" and some unique sequence and terminated by "QINU" and a final control character; it is partially visible below, because the unique string is not complete after truncation, and then cannot be eliminated from the output in the first two lines):
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&lt;nowiki/&gt;bcbcd | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a<nowiki/>bcbcd | 3 }}
*: Returns three characters as expected
:*: It effectively returns 3 bytes, but the \x07 (BELL) control in the second position is stripped out when generating the HTML displayed here.
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&lt;nowiki>E&lt;/nowiki&gt;bcbcd | 433 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a<nowiki>E</nowiki>bcbcd | 433 }}
*: Returns three characters, but not aEb as might have been expected.
:*: It effectively returns 43 bytes, but the \x07 (BELL) control in the second position is stripped out when generating the HTML displayed here, and the final \x07 was truncated
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&lt;nowiki/&gt;bc | 4410 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a<nowiki/>bc | 4410 }}
*: Returns 10 characters as expected.
:*: It effectively returns 44 bytes, but the complete 43-bytes sequence is filtered out from the output wiki code when generating the final HTML, so there just remains the first leading "a" character .
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&lt;nowiki/&gt;bc | 45 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a<nowiki/>bc | 45 }}
 
On the opposite, the HTML comments, and the "noinclude", "includeonly" and "onlyinclude" wiki markup tags are safealso ignored, because they are preprocessed in template parameters, before including it and substituting parameters:
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&lt;noinclude/&gt;bc | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a<noinclude/>bc | 3 }}
:* <code><nowiki>{{</nowiki> {{BASEPAGENAME}} | a&lt;!-- comment --&gt;bc | 3 <nowiki>}}</nowiki></code> &rarr; {{ {{BASEPAGENAME}} | a<!-- comment -->bc | 3 }}
Line 54 ⟶ 57:
<templatedata>
{
"endescription": "A template to give the <count> substring of characters from the start of the trimmed string ",
"description": {
"params": {
"en": "A template to give the <count> substring of characters from the start of the trimmed string ",
"1": {
"cs": "Šablona pro zadání <count> podřetězce znaků od začátku oříznutého řetězce"
"label": "String",
},
"endescription": "The string to be trimmed and counted",
"params": {
"type": "string",
"1": {
"required": true
"label": {
},
"en": "String",
"2": {
"cs": "Řetězec"
"label": "Count",
},
"endescription": "Gives the <count> substring of characters from the start of the trimmed string",
"description": {
"type": "number",
"en": "The string to be trimmed and counted",
"required": true
"cs": "Řetězec, který má být oříznut a spočítán"
}
},
}
"type": "string",
"required": true
},
"2": {
"label": {
"en": "Count",
"cs": "Počet"
},
"description": {
"en": "Gives the <count> substring of characters from the start of the trimmed string",
"cs": "Dává <count> podřetězec znaků od začátku oříznutého řetězce"
},
"type": "number",
"required": true
}
}
}
</templatedata>
 
== See also ==
* [[Bugzilla:22555]] (historical; need for correcting padleft/padright functions and to provide better string-handling parser functions)
 
{{String-handling templates |sub}}
 
<includeonly>{{Sandbox other||
Anonymous user