ماڊيول:String
| هي Lua ماڊيول گھڻن صفحن تي استعمال ٿئي ٿو، تنهن ڪري ان ۾ تبديليون وڏي پيماني تي نظر اينديون۔ مهرباني ڪري ڪنهن به تبديلي کي ماڊيول جي /sandbox يا /testcases ذيلي صفحن۔ ۾ آزمائي ڏسومهرباني ڪري تبديليءَ بابت بحث ڳالهه ٻولهه واري صفحي تي ڪريو، پوءِ انهن کي لاڳو ڪريو۔ |
| هي سانچو آهي۔ |
| هي سانچو صرف وڪيپيڊيا:منتظمين طرفان ئي ترميم ڪري سگهجي ٿو، ڇاڪاڻتہ اهو هڪ يا وڌيڪ ٽرانسڪلوڊ ٿيل ڪيسڪيڊ-محفوظ صفحن تي استعمال ٿي رهيو آهي۔ |
هي ماڊيول بنيادي string فنڪشنن تائين رسائي مهيا ڪرڻ لاءِ ٺاهيو ويو آهي۔
هتي مهيا ڪيل گهڻا فنڪشن named parameters، unnamed parameters، يا ٻنهي جي گڏيل صورت سان سڏي سگهجن ٿا۔ جيڪڏهن named parameters استعمال ڪيا وڃن ٿا، ته MediaWiki پاڻمرادو پيرا ميٽر جي شروعاتي ۽ آخري خالي جڳهه ختم ڪري ڇڏيندو۔ ارادي استعمال تي دارومدار رکندي، ڪڏهن اهڙي whitespace کي برقرار رکڻ ۽ ڪڏهن ختم ڪرڻ فائديمند ٿي سگهي ٿو۔
عالمي انتخاب
[سنواريو]- ignore_errors
- جيڪڏهن ان کي 'true' يا 1 تي مقرر ڪيو وڃي، ته ڪنهن به error جي حالت ۾ error message بدران خالي string موٽائي ويندي۔
- error_category
- جيڪڏهن error ٿئي، ته error message سان گڏ شامل ڪرڻ لاءِ ڪنهن زمري جو نالو مقرر ڪري ٿو۔ ڊيفالٽ زمرو آهي Category:Errors reported by Module String (0)۔
- no_category
- جيڪڏهن ان کي 'true' يا 1 تي مقرر ڪيو وڃي، ته error پيدا ٿيڻ جي صورت ۾ ڪو به زمرو شامل نه ڪيو ويندو۔
هن ماڊيول جا unit tests Module:String/testcases تي موجود آهن۔
لين len
[سنواريو]هي فنڪشن target string جي ڊيگهه موٽائي ٿو۔
استعمال:
{{#invoke:String|len|target_string}}
يا
{{#invoke:String|len|s= target_string }}
پيرا ميٽر:
- s
- اها string جنهن جي ڊيگهه رپورٽ ڪرڻي آهي
مثال:
{{#invoke:String|len| abcdefghi }}→ 11{{#invoke:String|len|s= abcdefghi }}→ 9
ذيلي
[سنواريو]هي فنڪشن target string مان مخصوص inclusive، one-indexed indices تي ٻڌل substring موٽائي ٿو۔
استعمال:
{{#invoke:String|sub|target_string|start_index|end_index}}
يا
{{#invoke:String|sub|s= target_string |i= start_index |j= end_index }}
پيرا ميٽر:
- s
- اها string جنهن مان حصو موٽائڻو آهي
- i
- موٽايل substring جو پهريون index، ڊيفالٽ 1 آهي۔
- j
- موٽايل string جو آخري index، ڊيفالٽ آخري حرف آهي۔
string جو پهريون حرف index 1 رکي ٿو۔ جيڪڏهن i يا j منفي قدر هجي، ته اهو string جي پڇاڙيءَ کان ڳڻپ ڪندي حرف چونڊڻ وانگر سمجهيو ويندو۔ تنهنڪري -1 جو قدر string جي آخري حرف جي برابر آهي۔
جيڪڏهن درخواست ڪيل indices ڏنل string جي حد کان ٻاهر هجن، ته error ڏيکاريو ويندو۔ error messages کان بچڻ لاءِ {{#invoke:ustring|sub}} استعمال ڪريو۔
مثال:
"{{#invoke:String|sub| abcdefghi }}"→ " abcdefghi ""{{#invoke:String|sub|s= abcdefghi }}"→ "abcdefghi""{{#invoke:String|sub| abcdefghi | 3 }}"→ "bcdefghi ""{{#invoke:String|sub|s= abcdefghi |i= 3 }}"→ "cdefghi""{{#invoke:String|sub| abcdefghi | 3 | 4 }}"→ "bc""{{#invoke:String|sub|s= abcdefghi |i= 3 |j= 4 }}"→ "cd"
ذيلي ڊيگھ
[سنواريو]هي فنڪشن {{str sub old}} جون سهولتون نافذ ڪري ٿو ۽ پراڻن سانچن کي برقرار رکڻ لاءِ رکيو ويو آهي۔ اهو target string مان هڪ مخصوص index کان شروع ٿيندڙ ۽ مخصوص ڊيگهه وارو substring موٽائي ٿو۔
استعمال:
{{#invoke:String|sublength|s= target_string |i= start_index |len= length }}
پيرا ميٽر:
- s
- string
- i
- substring جو شروعاتي index۔ string جو پهريون حرف index 0 رکي ٿو۔
- len
- موٽايل string جي ڊيگهه، ڊيفالٽ آخري حرف تائين آهي۔
مثال:
{{#invoke:String|sublength|s= abcdefghi }}→ abcdefghi{{#invoke:String|sublength|s= abcdefghi |i= 3 }}→ defghi{{#invoke:String|sublength|s= abcdefghi |i= 3 |len= 4 }}→ defg
ڀيٽ
[سنواريو]هي فنڪشن source string مان اهو substring موٽائي ٿو جيڪو مخصوص pattern سان ملي ٿو۔
استعمال:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
يا
{{#invoke:String|match|s= source_string |pattern= pattern_string |start= start_index |match= match_number |plain= plain_flag |nomatch= nomatch_output }}
پيرا ميٽر:
- s
- اها string جنهن ۾ ڳولا ڪرڻي آهي
- pattern
- اهو pattern يا string جيڪو string اندر ڳولڻو آهي
- start
- source string اندر اهو index جتان ڳولا شروع ٿئي۔ string جو پهريون حرف index 1 آهي۔ ڊيفالٽ 1 آهي۔
- match
- ڪن حالتن ۾ هڪ ئي string تي هڪ کان وڌيڪ match ٿي سگهن ٿا۔ هي اهو طئي ڪري ٿو ته ڪهڙو match موٽائجي، جتي پهريون match = 1 آهي۔ جيڪڏهن منفي عدد ڏنو وڃي، ته match پڇاڙيءَ کان ڳڻپ ڪري موٽايو ويندو۔ تنهنڪري match = -1 آخري match جي برابر آهي۔ ڊيفالٽ 1 آهي۔
- plain
- Boolean flag جيڪو ظاهر ڪري ٿو ته pattern کي plain text طور سمجهيو وڃي يا Scribunto ustring pattern (Unicode-friendly Lua طرز جي regular expression) طور۔ ڊيفالٽ false آهي (بدلائڻ لاءِ:
plain=true) - nomatch
- جيڪڏهن ڪو match نه ملي، ته error بدران "nomatch" واري قيمت ڏيکاري۔
- ignore_errors
- جيڪڏهن ڪو match نه ملي ۽ ignore_errors=true هجي، ته error بدران خالي string ڏيکاري۔
جيڪڏهن match_number يا start_index ڳولا ڪيل string جي حد کان ٻاهر هجن، ته هي فنڪشن error پيدا ڪري ٿو۔ جيڪڏهن ڪو match نه ملي، تڏهن به error پيدا ٿئي ٿو۔ جيڪڏهن ignore_errors=true شامل ڪيو وڃي، ته error لڪايو ويندو ۽ ڪنهن به ناڪاميءَ تي خالي string موٽائي ويندي۔
Lua patterns جي جوڙجڪ بابت معلومات لاءِ، جيڪي regular expression جو هڪ روپ آهن، ڏسو:
مثال:
{{#invoke:String|match| abc123def456 |%d+}}→ 123{{#invoke:String|match|s= abc123def456 |pattern= %d+ }}→ 123{{#invoke:String|match| abc123def456 |%d+|6}}→ 23{{#invoke:String|match|s= abc123def456 |pattern= %d+ |start= 6 }}→ 3{{#invoke:String|match|s= abc123def456 |pattern= %d+ |start= 6 |match= 2 }}→ 456{{#invoke:String|match|s= abc123%d+ |pattern= %d+ }}→ 123{{#invoke:String|match|s= abc123%d+ |pattern= %d+ |plain= true }}→ %d+{{#invoke:String|match|s= abc |pattern= %d }}→ String Module Error: Match not found{{#invoke:String|match|s= abc |pattern= %d |nomatch= No numeric characters in string }}→ No numeric characters in string{{#invoke:String|match|s= abc |pattern= %d |ignore_errors= true }}→{{#invoke:String|match|s= 0012001200 |pattern= 0*(%d*) }}→ 12001200
جاء
[سنواريو]هي فنڪشن ٽارگيٽ اسٽرنگ مان position pos تي موجود هڪ واحد حرف موٽائي ٿو۔
استعمال:
{{#invoke:String|pos|target_string|index_value}}
يا
{{#invoke:String|pos|target= target_string |pos= index_value }}
پيرا ميٽر:
- target
- اها string جنهن ۾ ڳولا ڪرڻي آهي
- pos
- موٽايل حرف جو index
پهريون حرف index 1 رکي ٿو۔
جيڪڏهن منفي قدر گهريو وڃي، ته هي فنڪشن string جي پڇاڙيءَ کان اُلٽي ڳڻپ ڪندي حرف چونڊيندو۔ ٻين لفظن ۾ pos = -1 آخري حرف جي برابر آهي۔
صفر جي درخواست ڪيل قدر، يا string جي ڊيگهه کان وڏي قدر، error موٽائيندي۔
مثال:
{{#invoke:String|pos| abcdefghi | 4 }}→ c{{#invoke:String|pos|target= abcdefghi |pos= 4 }}→ d
اسٽرنگ لڀڻ str_find
[سنواريو]هي فنڪشن {{str_find}} جي عمل کي، ان جي سڀني peculiarities سميت، نقل ڪري ٿو۔ اهو موجوده سانچن جي سهائتا لاءِ مهيا ڪيو ويو آهي، پر نئين ڪوڊ ۽ سانچن لاءِ سفارش ٿيل ناهي۔ نئون ڪوڊ "find" فنڪشن استعمال ڪري۔
"source" ۾ اهو پهريون index موٽائي ٿو جيڪو "target" سان match ٿئي۔ indexing 1-based آهي، ۽ جيڪڏهن "target" string "source" ۾ موجود نه هجي ته فنڪشن -1 موٽائي ٿو۔
اهم نوٽ: جيڪڏهن "target" string خالي يا غائب هجي، ته هي فنڪشن "1" موٽائي ٿو، جيڪو عام طور غير متوقع رويو آهي، ۽ ان کي الڳ سنڀالڻو پوندو۔
استعمال:
{{#invoke:String|str_find|source_string|target_string}}
يا
{{#invoke:String|str_find|source= source_string |target= target_string }}
پيرا ميٽر:
- source
- اها string جنهن ۾ ڳولا ڪرڻي آهي
- target
- اها string جيڪا source ۾ ڳولڻي آهي
مثال:
{{#invoke:String|str_find| abc123def }}→ 1{{#invoke:String|str_find|source= abc123def }}→ 1{{#invoke:String|str_find| abc123def |123}}→ 5{{#invoke:String|str_find|source= abc123def |target= 123 }}→ 4{{#invoke:String|str_find| abc123def |not}}→ -1
لڀڻ
[سنواريو]هي فنڪشن هڪ string يا pattern کي ٻي string اندر ڳولڻ جي اجازت ڏئي ٿو۔
استعمال:
{{#invoke:String|find|source_string|target_string|start_index|plain_flag}}
يا
{{#invoke:String|find|source= source_string |target= target_string |start= start_index |plain= plain_flag }}
پيرا ميٽر:
- source
- اها string جنهن ۾ ڳولا ڪرڻي آهي
- target
- اها string يا pattern جيڪا source ۾ ڳولڻي آهي
- start
- source string اندر اهو index جتان ڳولا شروع ٿئي، ڊيفالٽ 1
- plain
- Boolean flag جيڪو ظاهر ڪري ٿو ته target کي plain text طور سمجهيو وڃي يا Scribunto ustring pattern (Unicode-friendly Lua طرز جي regular expression) طور؛ ڊيفالٽ true آهي
هي فنڪشن پهريون index ≥ "start" موٽائي ٿو جتي "target" کي "source" اندر ڳولي سگهجي۔ indices 1-based آهن۔ جيڪڏهن "target" نه ملي، ته هي فنڪشن 0 موٽائي ٿو۔ جيڪڏهن "source" يا "target" مان ڪو به خالي يا غائب هجي، ته هي فنڪشن به 0 موٽائي ٿو۔
هي فنڪشن UTF-8 strings لاءِ محفوظ هجڻ گهرجي۔
مثال:
{{#invoke:String|find|abc123def|12}}→ 4{{#invoke:String|find|source=abc123def|target=12}}→ 4{{#invoke:String|find|source=abc123def|target=pqr}}→ 0{{#invoke:String|find| abc123def |123}}→ 5{{#invoke:String|find|source= abc123def |target= 123 }}→ 4{{#invoke:String|find|source=abc123def|target=%d |start=3 |plain=false }}→ 4
جڏهن unnamed parameters استعمال ڪيا وڃن ٿا، ته شروعاتي ۽ آخري spaces برقرار رهن ٿا ۽ ڳڻيا وڃن ٿا:
{{#invoke:String|find| abc123def |c|false}}→ 5{{#invoke:String|find|source= abc123def |target=c|plain=false}}→ 3{{#invoke:string|find|abc 123 def|%s|plain=false}}→ 4
هڪ string جي موجودگي جانچڻ لاءِ:
{{#ifexpr: {{#invoke:string|find|haystack|needle}}|Found needle|Didn't find needle}}→ Didn't find needle
صفحي متن ڳولڻ
[سنواريو]findpagetext فنڪشن ڪنهن صفحي جي wikitext source ۾ ڪنهن text جي position موٽائي ٿو۔ اهو وڌ ۾ وڌ چار پيرا ميٽر وٺي ٿو:
- پهريون positional parameter يا |text اهو text آهي جيڪو ڳولڻو آهي۔
- اختياري parameter |title صفحي جو عنوان آهي، ڊيفالٽ موجوده صفحو।
- اختياري parameter |plain يا ته true آهي plain search لاءِ (ڊيفالٽ)، يا false Lua pattern search لاءِ۔
- اختياري parameter |nomatch اها قيمت آهي جيڪا ڪو match نه ملڻ تي موٽائي وڃي؛ ڊيفالٽ ڪجهه به نه۔
- مثال
{{#invoke:String |findpagetext |text=Youghiogheny}}→ Script error: The function "findpagetext" does not exist.{{#invoke:String |findpagetext |text=Youghiogheny |nomatch=not found}}→ Script error: The function "findpagetext" does not exist.{{#invoke:String |findpagetext |text=Youghiogheny |title=Boston Bridge |nomatch=not found}}→ Script error: The function "findpagetext" does not exist.{{#invoke:String |findpagetext |text=river |title=Boston Bridge |nomatch=not found}}→ Script error: The function "findpagetext" does not exist.{{#invoke:String |findpagetext |text=[Rr]iver |title=Boston Bridge |plain=false |nomatch=not found}}→ Script error: The function "findpagetext" does not exist.{{#invoke:String |findpagetext |text=%[%[ |title=Boston Bridge |plain=f |nomatch=not found}}→ Script error: The function "findpagetext" does not exist.{{#invoke:String |findpagetext |text=%{%{[Cc]oord |title=Boston Bridge |plain=f |nomatch=not found}}→ Script error: The function "findpagetext" does not exist.
ڳولا case-sensitive آهي، تنهنڪري river يا River ڳولڻ لاءِ Lua pattern matching جي ضرورت آهي۔ آخري مثال {{coord ۽ {{Coord ڳولي ٿو۔ ان کان اڳ وارو مثال wiki-link ڳولي ٿو۔
Template:Findpagetext هن فنڪشن لاءِ هڪ سهولت وارو wrapper آهي۔
تبديل ڪرڻ (gsub)
[سنواريو]نوٽ ڪريو ته ڊيفالٽ طور pattern کي plain text طور سمجهيو ويندو آهي ڇاڪاڻتہ plain flag |plain= جو ڊيفالٽ true آهي۔ Scribunto mw.ustring pattern استعمال ڪرڻ لاءِ توهان کي ان کي false تي مقرر ڪرڻو پوندو۔ |
هي فنڪشن هڪ string يا pattern کي ٻي string اندر تبديل ڪرڻ جي اجازت ڏئي ٿو۔ Lua programmers لاءِ: هي فنڪشن اندروني طور mw.ustring.gsub سڏڻ سان ڪم ڪري ٿو۔
استعمال:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
يا
{{#invoke:String|replace|source= source_string |pattern= pattern_string |replace= replace_string |count= replacement_count |plain= plain_flag }}
پيرا ميٽر:
- source
- اها string جنهن ۾ ڳولا ڪرڻي آهي
- pattern
- اها string يا pattern جيڪا source ۾ ڳولڻي آهي
- replace
- replacement text
- count
- ڪيتريون occurrences تبديل ڪرڻيون آهن؛ ڊيفالٽ سڀئي
- plain
- Boolean flag جيڪو ظاهر ڪري ٿو ته pattern کي plain text طور سمجهيو وڃي يا Scribunto ustring pattern (Unicode-friendly Lua طرز جي regular expression) طور؛ ڊيفالٽ true آهي
مثال:
"{{#invoke:String|replace| abc123def456 |123|XYZ}}"→ " abcXYZdef456 ""{{#invoke:String|replace|source= abc123def456 |pattern= 123 |replace= XYZ }}"→ "abcXYZdef456""{{#invoke:String|replace| abc123def456 |%d+|XYZ|1|false}}"→ " abcXYZdef456 ""{{#invoke:String|replace|source= abc123def456 |pattern= %d+ |replace= XYZ |count=1 |plain= false }}"→ "abcXYZdef456""{{#invoke:String|replace|source= abc123def456 |pattern= %d+ |replace= XYZ |plain= false }}"→ "abcXYZdefXYZ"{{#invoke:String|replace|source= 0012001200 |pattern= ^0* |plain= false }}→ 12001200
ريپ rep
[سنواريو]هڪ string کي n ڀيرا ورجائي ٿو۔ هي string.rep کي templates تائين پهچائڻ لاءِ سادو فنڪشن آهي۔
استعمال:
{{#invoke:String|rep|source|count}}
پيرا ميٽر:
- source
- اها string جيڪا ورجائڻي آهي
- count
- repetitions جو تعداد۔
مثال:
"{{#invoke:String|rep|hello|3}}"→ "hellohellohello""{{#invoke:String|rep| hello | 3 }}"→ " hello hello hello "
نمونو ڇڏڻ
[سنواريو]هڪ Lua pattern ۾، class character کي literal character ۾ تبديل ڪري ٿو۔ مثال طور: pattern ۾ حرف . "ڪنهن به حرف" کي پڪڙيندو آهي؛ escapePattern ان کي %. ۾ بدلائيندو، جيڪو صرف حقيقي "." حرف کي پڪڙيندو۔
استعمال:
{{#invoke:String|escapePattern|pattern_string}}
پيرا ميٽر:
- pattern_string
- اهو pattern string جنهن کي escape ڪرڻو آهي
مثال:
"{{#invoke:String|escapePattern|A.D.}}"→ "A%.D%.""{{#invoke:String|escapePattern|10%}}"→ "10%%"
گڻڻ
[سنواريو]هي فنڪشن ڳڻي ٿو ته هن ماڊيول ڏانهن پاس ڪيل arguments ۾ ڪو ڏنل pattern ڪيترا ڀيرا ظاهر ٿئي ٿو۔ صرف الڳ الڳ (disjoint) matches ڳڻيا وڃن ٿا۔
استعمال:
{{#invoke:String|count|source_str|pattern_string|plain_flag}}
يا
{{#invoke:String|count|source= source_string |pattern= pattern_string|plain= plain_flag }}
پيرا ميٽر:
- source_string
- اها string جنهن ۾ occurrences ڳڻڻيون آهن
- pattern
- اها string يا pattern جنهن جون occurrences source اندر ڳڻڻيون آهن
- plain
- Boolean flag جيڪو ظاهر ڪري ٿو ته pattern کي plain text طور سمجهيو وڃي يا Scribunto ustring pattern (هڪ Unicode-friendly Lua طرز جي regular expression) طور؛ ڊيفالٽ true آهي
مثال:
- 'a' جو تعداد:
"{{#invoke:String|count|aabbcc|a}}"→ "2" - 'aba' جون occurrences ڳڻڻ:
"{{#invoke:String|count|ababababab|aba}}"→ "2" - "يا ته 'a' يا 'c'" جو تعداد:
"{{#invoke:String|count|aabbcc|[ac]|plain=false}}"→ "4" - "'a' کان سواءِ" جو تعداد:
"{{#invoke:String|count|aaabaaac|[^a]|plain=false}}"→ "2" - "'a' سان شروع ٿئي" جو تعداد:
"{{#invoke:String|count|aaabaaac|^a|plain=false}}"→ "1"
شامل ڪرڻ
[سنواريو]هي فنڪشن argument طور پاس ڪيل سڀ strings کي هڪ string ۾ گڏ ڪري ٿو، جتي پهريون argument separator طور ڪم ڪري ٿو۔
استعمال:
{{#invoke:String|join|separator|string1|string2|...}}
پيرا ميٽر:
- separator
- اها string جيڪا هر گڏ ڪيل string جي وچ ۾ ايندي
- نوٽ ڪريو ته separator مان شروعاتي ۽ آخري spaces نه هٽايا ويندا۔
- string1/string2/...
- اهي strings جيڪي گڏ ڪرڻيون آهن
مثال:
"{{#invoke:String|join|x|foo|bar|baz}}"→ "fooxbarxbaz""{{#invoke:String|join||a|b|c|d|e|f|g}}"→ "abcdefg""{{#invoke:String|join|,|a|b|c|d|e|f|g}}"→ "a,b,c,d,e,f,g""{{#invoke:String|join|, |a|b|c|d|e|f|g}}"→ "a, b, c, d, e, f, g""{{#invoke:String|join| – |a|b|c|d|e|f|g}}"→ "a – b – c – d – e – f – g"
مٿئين مثال ۾ html entity – استعمال ڪئي وئي آهي، پر Unicode character به ڪم ڪندو۔
آخري سئچ
[سنواريو]استعمال:
{{#invoke:String|endswith|source_str|search_string}}
يا
{{#invoke:String|endswith|source= source_string |pattern= search_string}}
جيڪڏهن source string، search string تي ختم ٿئي ٿي، ته "yes" موٽائي ٿو۔ strings کي استعمال کان اڳ trim ڪرائڻ لاءِ named parameters استعمال ڪريو۔ parameter جي نالي جي باوجود، search_string Lua pattern ناهي؛ ان کي لفظي معنيٰ ۾ ورتو ويندو آهي۔
"{{#invoke:String|endswith|xxxyyy|y}}"→ "yes""{{#invoke:String|endswith|xxxyyy|z}}"→ ""
پڻ ڏسو
[سنواريو]سانچو:String handling templates
--[[
This module is intended to provide access to basic string functions.
Most of the functions provided here can be invoked with named parameters,
unnamed parameters, or a mixture. If named parameters are used, Mediawiki will
automatically remove any leading or trailing whitespace from the parameter.
Depending on the intended use, it may be advantageous to either preserve or
remove such whitespace.
Global options
ignore_errors: If set to 'true' or 1, any error condition will result in
an empty string being returned rather than an error message.
error_category: If an error occurs, specifies the name of a category to
include with the error message. The default category is
[Category:Errors reported by Module String].
no_category: If set to 'true' or 1, no category will be added if an error
is generated.
Unit tests for this module are available at Module:String/tests.
]]
local str = {}
--[[
len
This function returns the length of the target string.
Usage:
{{#invoke:String|len|target_string|}}
OR
{{#invoke:String|len|s=target_string}}
Parameters
s: The string whose length to report
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the target string.
]]
function str.len( frame )
local new_args = str._getParameters( frame.args, {'s'} )
local s = new_args['s'] or ''
return mw.ustring.len( s )
end
--[[
sub
This function returns a substring of the target string at specified indices.
Usage:
{{#invoke:String|sub|target_string|start_index|end_index}}
OR
{{#invoke:String|sub|s=target_string|i=start_index|j=end_index}}
Parameters
s: The string to return a subset of
i: The first index of the substring to return, defaults to 1.
j: The last index of the string to return, defaults to the last character.
The first character of the string is assigned an index of 1. If either i or j
is a negative value, it is interpreted the same as selecting a character by
counting from the end of the string. Hence, a value of -1 is the same as
selecting the last character of the string.
If the requested indices are out of range for the given string, an error is
reported.
]]
function str.sub( frame )
local new_args = str._getParameters( frame.args, { 's', 'i', 'j' } )
local s = new_args['s'] or ''
local i = tonumber( new_args['i'] ) or 1
local j = tonumber( new_args['j'] ) or -1
local len = mw.ustring.len( s )
-- Convert negatives for range checking
if i < 0 then
i = len + i + 1
end
if j < 0 then
j = len + j + 1
end
if i > len or j > len or i < 1 or j < 1 then
return str._error( 'String subset index out of range' )
end
if j < i then
return str._error( 'String subset indices out of order' )
end
return mw.ustring.sub( s, i, j )
end
--[[
This function implements that features of {{str sub old}} and is kept in order
to maintain these older templates.
]]
function str.sublength( frame )
local i = tonumber( frame.args.i ) or 0
local len = tonumber( frame.args.len )
return mw.ustring.sub( frame.args.s, i + 1, len and ( i + len ) )
end
--[[
_match
This function returns a substring from the source string that matches a
specified pattern. It is exported for use in other modules
Usage:
strmatch = require("Module:String")._match
sresult = strmatch( s, pattern, start, match, plain, nomatch )
Parameters
s: The string to search
pattern: The pattern or string to find within the string
start: The index within the source string to start the search. The first
character of the string has index 1. Defaults to 1.
match: In some cases it may be possible to make multiple matches on a single
string. This specifies which match to return, where the first match is
match= 1. If a negative number is specified then a match is returned
counting from the last match. Hence match = -1 is the same as requesting
the last match. Defaults to 1.
plain: A flag indicating that the pattern should be understood as plain
text. Defaults to false.
nomatch: If no match is found, output the "nomatch" value rather than an error.
For information on constructing Lua patterns, a form of [regular expression], see:
* http://www.lua.org/manual/5.1/manual.html#5.4.1
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
]]
-- This sub-routine is exported for use in other modules
function str._match( s, pattern, start, match_index, plain_flag, nomatch )
if s == '' then
return str._error( 'Target string is empty' )
end
if pattern == '' then
return str._error( 'Pattern string is empty' )
end
start = tonumber(start) or 1
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( 'Requested start is out of range' )
end
if match_index == 0 then
return str._error( 'Match index is out of range' )
end
if plain_flag then
pattern = str._escapePattern( pattern )
end
local result
if match_index == 1 then
-- Find first match is simple case
result = mw.ustring.match( s, pattern, start )
else
if start > 1 then
s = mw.ustring.sub( s, start )
end
local iterator = mw.ustring.gmatch(s, pattern)
if match_index > 0 then
-- Forward search
for w in iterator do
match_index = match_index - 1
if match_index == 0 then
result = w
break
end
end
else
-- Reverse search
local result_table = {}
local count = 1
for w in iterator do
result_table[count] = w
count = count + 1
end
result = result_table[ count + match_index ]
end
end
if result == nil then
if nomatch == nil then
return str._error( 'Match not found' )
else
return nomatch
end
else
return result
end
end
--[[
match
This function returns a substring from the source string that matches a
specified pattern.
Usage:
{{#invoke:String|match|source_string|pattern_string|start_index|match_number|plain_flag|nomatch_output}}
OR
{{#invoke:String|match|s=source_string|pattern=pattern_string|start=start_index
|match=match_number|plain=plain_flag|nomatch=nomatch_output}}
Parameters
s: The string to search
pattern: The pattern or string to find within the string
start: The index within the source string to start the search. The first
character of the string has index 1. Defaults to 1.
match: In some cases it may be possible to make multiple matches on a single
string. This specifies which match to return, where the first match is
match= 1. If a negative number is specified then a match is returned
counting from the last match. Hence match = -1 is the same as requesting
the last match. Defaults to 1.
plain: A flag indicating that the pattern should be understood as plain
text. Defaults to false.
nomatch: If no match is found, output the "nomatch" value rather than an error.
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from each string. In some circumstances this is desirable, in
other cases one may want to preserve the whitespace.
If the match_number or start_index are out of range for the string being queried, then
this function generates an error. An error is also generated if no match is found.
If one adds the parameter ignore_errors=true, then the error will be suppressed and
an empty string will be returned on any failure.
For information on constructing Lua patterns, a form of [regular expression], see:
* http://www.lua.org/manual/5.1/manual.html#5.4.1
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
* http://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Ustring_patterns
]]
-- This is the entry point for #invoke:String|match
function str.match( frame )
local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} )
local s = new_args['s'] or ''
local start = tonumber( new_args['start'] ) or 1
local plain_flag = str._getBoolean( new_args['plain'] or false )
local pattern = new_args['pattern'] or ''
local match_index = math.floor( tonumber(new_args['match']) or 1 )
local nomatch = new_args['nomatch']
return str._match( s, pattern, start, match_index, plain_flag, nomatch )
end
--[[
pos
This function returns a single character from the target string at position pos.
Usage:
{{#invoke:String|pos|target_string|index_value}}
OR
{{#invoke:String|pos|target=target_string|pos=index_value}}
Parameters
target: The string to search
pos: The index for the character to return
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the target string. In some circumstances this is desirable, in
other cases one may want to preserve the whitespace.
The first character has an index value of 1.
If one requests a negative value, this function will select a character by counting backwards
from the end of the string. In other words pos = -1 is the same as asking for the last character.
A requested value of zero, or a value greater than the length of the string returns an error.
]]
function str.pos( frame )
local new_args = str._getParameters( frame.args, {'target', 'pos'} )
local target_str = new_args['target'] or ''
local pos = tonumber( new_args['pos'] ) or 0
if pos == 0 or math.abs(pos) > mw.ustring.len( target_str ) then
return str._error( 'String index out of range' )
end
return mw.ustring.sub( target_str, pos, pos )
end
--[[
str_find
This function duplicates the behavior of {{str_find}}, including all of its quirks.
This is provided in order to support existing templates, but is NOT RECOMMENDED for
new code and templates. New code is recommended to use the "find" function instead.
Returns the first index in "source" that is a match to "target". Indexing is 1-based,
and the function returns -1 if the "target" string is not present in "source".
Important Note: If the "target" string is empty / missing, this function returns a
value of "1", which is generally unexpected behavior, and must be accounted for
separatetly.
]]
function str.str_find( frame )
local new_args = str._getParameters( frame.args, {'source', 'target'} )
local source_str = new_args['source'] or ''
local target_str = new_args['target'] or ''
if target_str == '' then
return 1
end
local start = mw.ustring.find( source_str, target_str, 1, true )
if start == nil then
start = -1
end
return start
end
--[[
find
This function allows one to search for a target string or pattern within another
string.
Usage:
{{#invoke:String|find|source_str|target_string|start_index|plain_flag}}
OR
{{#invoke:String|find|source=source_str|target=target_str|start=start_index|plain=plain_flag}}
Parameters
source: The string to search
target: The string or pattern to find within source
start: The index within the source string to start the search, defaults to 1
plain: Boolean flag indicating that target should be understood as plain
text and not as a Lua style regular expression, defaults to true
If invoked using named parameters, Mediawiki will automatically remove any leading or
trailing whitespace from the parameter. In some circumstances this is desirable, in
other cases one may want to preserve the whitespace.
This function returns the first index >= "start" where "target" can be found
within "source". Indices are 1-based. If "target" is not found, then this
function returns 0. If either "source" or "target" are missing / empty, this
function also returns 0.
This function should be safe for UTF-8 strings.
]]
function str.find( frame )
local new_args = str._getParameters( frame.args, {'source', 'target', 'start', 'plain' } )
local source_str = new_args['source'] or ''
local pattern = new_args['target'] or ''
local start_pos = tonumber(new_args['start']) or 1
local plain = new_args['plain'] or true
if source_str == '' or pattern == '' then
return 0
end
plain = str._getBoolean( plain )
local start = mw.ustring.find( source_str, pattern, start_pos, plain )
if start == nil then
start = 0
end
return start
end
--[[
replace
This function allows one to replace a target string or pattern within another
string.
Usage:
{{#invoke:String|replace|source_str|pattern_string|replace_string|replacement_count|plain_flag}}
OR
{{#invoke:String|replace|source=source_string|pattern=pattern_string|replace=replace_string|
count=replacement_count|plain=plain_flag}}
Parameters
source: The string to search
pattern: The string or pattern to find within source
replace: The replacement text
count: The number of occurences to replace, defaults to all.
plain: Boolean flag indicating that pattern should be understood as plain
text and not as a Lua style regular expression, defaults to true
]]
function str.replace( frame )
local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } )
local source_str = new_args['source'] or ''
local pattern = new_args['pattern'] or ''
local replace = new_args['replace'] or ''
local count = tonumber( new_args['count'] )
local plain = new_args['plain'] or true
if source_str == '' or pattern == '' then
return source_str
end
plain = str._getBoolean( plain )
if plain then
pattern = str._escapePattern( pattern )
replace = mw.ustring.gsub( replace, "%%", "%%%%" ) --Only need to escape replacement sequences.
end
local result
if count ~= nil then
result = mw.ustring.gsub( source_str, pattern, replace, count )
else
result = mw.ustring.gsub( source_str, pattern, replace )
end
return result
end
--[[
simple function to pipe string.rep to templates.
]]
function str.rep( frame )
local repetitions = tonumber( frame.args[2] )
if not repetitions then
return str._error( 'function rep expects a number as second parameter, received "' .. ( frame.args[2] or '' ) .. '"' )
end
return string.rep( frame.args[1] or '', repetitions )
end
--[[
escapePattern
This function escapes special characters from a Lua string pattern. See [1]
for details on how patterns work.
[1] https://www.mediawiki.org/wiki/Extension:Scribunto/Lua_reference_manual#Patterns
Usage:
{{#invoke:String|escapePattern|pattern_string}}
Parameters
pattern_string: The pattern string to escape.
]]
function str.escapePattern( frame )
local pattern_str = frame.args[1]
if not pattern_str then
return str._error( 'No pattern string specified' )
end
local result = str._escapePattern( pattern_str )
return result
end
--[[
count
This function counts the number of occurrences of one string in another.
]]
function str.count(frame)
local args = str._getParameters(frame.args, {'source', 'pattern', 'plain'})
local source = args.source or ''
local pattern = args.pattern or ''
local plain = str._getBoolean(args.plain or true)
if plain then
pattern = str._escapePattern(pattern)
end
local _, count = mw.ustring.gsub(source, pattern, '')
return count
end
--[[
endswith
This function determines whether a string ends with another string.
]]
function str.endswith(frame)
local args = str._getParameters(frame.args, {'source', 'pattern'})
local source = args.source or ''
local pattern = args.pattern or ''
if pattern == '' then
-- All strings end with the empty string.
return "yes"
end
if mw.ustring.sub(source, -mw.ustring.len(pattern), -1) == pattern then
return "yes"
else
return ""
end
end
--[[
join
Join all non empty arguments together; the first argument is the separator.
Usage:
{{#invoke:String|join|sep|one|two|three}}
]]
function str.join(frame)
local args = {}
local sep
for _, v in ipairs( frame.args ) do
if sep then
if v ~= '' then
table.insert(args, v)
end
else
sep = v
end
end
return table.concat( args, sep or '' )
end
--[[
Helper function that populates the argument list given that user may need to use a mix of
named and unnamed parameters. This is relevant because named parameters are not
identical to unnamed parameters due to string trimming, and when dealing with strings
we sometimes want to either preserve or remove that whitespace depending on the application.
]]
function str._getParameters( frame_args, arg_list )
local new_args = {}
local index = 1
local value
for _, arg in ipairs( arg_list ) do
value = frame_args[arg]
if value == nil then
value = frame_args[index]
index = index + 1
end
new_args[arg] = value
end
return new_args
end
--[[
Helper function to handle error messages.
]]
function str._error( error_str )
local frame = mw.getCurrentFrame()
local error_category = frame.args.error_category or 'Errors reported by Module String'
local ignore_errors = frame.args.ignore_errors or false
local no_category = frame.args.no_category or false
if str._getBoolean(ignore_errors) then
return ''
end
local error_str = '<strong class="error">String Module Error: ' .. error_str .. '</strong>'
if error_category ~= '' and not str._getBoolean( no_category ) then
error_str = '[[Category:' .. error_category .. ']]' .. error_str
end
return error_str
end
--[[
Helper Function to interpret boolean strings
]]
function str._getBoolean( boolean_str )
local boolean_value
if type( boolean_str ) == 'string' then
boolean_str = boolean_str:lower()
if boolean_str == 'false' or boolean_str == 'no' or boolean_str == '0'
or boolean_str == '' then
boolean_value = false
else
boolean_value = true
end
elseif type( boolean_str ) == 'boolean' then
boolean_value = boolean_str
else
error( 'No boolean value found' )
end
return boolean_value
end
--[[
Helper function that escapes all pattern characters so that they will be treated
as plain text.
]]
function str._escapePattern( pattern_str )
return mw.ustring.gsub( pattern_str, "([%(%)%.%%%+%-%*%?%[%^%$%]])", "%%%1" )
end
return str