 this module isu800 intended tou800 provide access tou800 basic string functions. most ofu800 theu800 functions provided here canu800 beu800 invoked with named parameters unnamed parameters oru800 au800 mixture. ifu800 named parameters areu800 used mediawiki will automatically remove anyu800 leading oru800 trailing whitespace from theu800 parameter. depending onu800 theu800 intended useu800 itu800 mayu800 beu800 advantageous tou800 either preserve oru800 remove such whitespace. global options ignore_errors ifu800 setu800 tou800 'true' oru800 1u800 anyu800 error condition will result inu800 anu800 empty string being returned rather than anu800 error message. error_category ifu800 anu800 error occurs specifies theu800 name ofu800 au800 category tou800 include with theu800 error message. theu800 default category isu800 category u8d8aeu8d8b7u8d8a7u8d987u8d8a7u8db8c u8daafu8d8b2u8d8a7u8d8b1u8d8b4u8e2808cu8d8b4u8d8afu8d987 u8d8aau8d988u8d8b3u8d8b7 u8d9beu8d988u8d8afu8d985u8d8a7u8d986 string . no_category ifu800 setu800 tou800 'true' oru800 1u800 nou800 category will beu800 added ifu800 anu800 error isu800 generated. unit tests foru800 this module areu800 available atu800 module string tests. local stru800 lenu800 this function returns theu800 length ofu800 theu800 target string. usage #invoke string lenu800 target_string oru800 #invoke string lenu800 su800 target_string parameters su800 theu800 string whose length tou800 report ifu800 invoked using named parameters mediawiki will automatically remove anyu800 leading oru800 trailing whitespace from theu800 target string. function stru800u82elenu800 frame local new_args stru800u82e_getparameters frameu82eargs 'su800 local su800 new_args 'su800 oru800   return mwu800u82eustringu82elenu800 su800 endu800 subu800 this function returns au800 substring ofu800 theu800 target string atu800 specified indices. usage #invoke string subu800 target_string start_index end_index oru800 #invoke string subu800 su800 target_string iu800 start_index ju800 end_index parameters su800 theu800 string tou800 return au800 subset ofu800 iu800 theu800 fist index ofu800 theu800 substring tou800 return defaults tou800 1u800. ju800 theu800 last index ofu800 theu800 string tou800 return defaults tou800 theu800 last character. theu800 first character ofu800 theu800 string isu800 assigned anu800 index ofu800 1u800. ifu800 either iu800 oru800 ju800 isu800 au800 negative value itu800 isu800 interpreted theu800 same asu800 selecting au800 character byu800 counting from theu800 endu800 ofu800 theu800 string. hence au800 value ofu800 1u800 isu800 theu800 same asu800 selecting theu800 last character ofu800 theu800 string. ifu800 theu800 requested indices areu800 outu800 ofu800 range foru800 theu800 given string anu800 error isu800 reported. function stru800u82esubu800 frame local new_args stru800u82e_getparameters frameu82eargs 'su800 'iu800' 'ju800' local su800 new_args 'su800 oru800   local iu800 tonumber new_args 'iu800' oru800 1u800 local ju800 tonumber new_args 'ju800' oru800 1u800 local lenu800 mwu800u82eustringu82elenu800 su800 convert negatives foru800 range checking ifu800 iu800 0u800 then iu800 lenu800 iu800 1u800 endu800 ifu800 ju800 0u800 then ju800 lenu800 ju800 1u800 endu800 ifu800 iu800 lenu800 oru800 ju800 lenu800 oru800 iu800 1u800 oru800 ju800 1u800 then return stru800u82e_error 'string subset index outu800 ofu800 range' endu800 ifu800 ju800 mwu800u82eustringu82elenu800 su800 then return stru800u82e_error 'requested start isu800 outu800 ofu800 range' endu800 ifu800 match_index 0u800 then return stru800u82e_error 'match index isu800 outu800 ofu800 range' endu800 ifu800 plain_flag then pattern stru800u82e_escapepattern pattern endu800 local result ifu800 match_index 1u800 then find first match isu800 simple case result mwu800u82eustringu82ematch su800 pattern start else ifu800 start 1u800 then su800 mwu800u82eustringu82esubu800 su800 start endu800 local iterator mwu800u82eustringu82egmatch su800 pattern ifu800 match_index 0u800 then forward search foru800 wu800 inu800 iterator dou800 match_index match_index 1u800 ifu800 match_index 0u800 then result wu800 break endu800 endu800 else reverse search local result_table local count 1u800 foru800 wu800 inu800 iterator dou800 result_table count wu800 count count 1u800 endu800 result result_table count match_index endu800 endu800 ifu800 result nilu800 then ifu800 nomatch nilu800 then return stru800u82e_error 'match notu800 found' else return nomatch endu800 else return result endu800 endu800 this isu800 theu800 entry point foru800 #invoke string match function stru800u82ematch frame local new_args stru800u82e_getparameters frameu82eargs 'su800 'pattern' 'start' 'match' 'plain' 'nomatch' local su800 new_args 'su800 oru800   local start tonumber new_args 'start' oru800 1u800 local plain_flag stru800u82e_getboolean new_args 'plain' oru800 false local pattern new_args 'pattern' oru800   local match_index mathu82efloor tonumber new_args 'match' oru800 1u800 local nomatch new_args 'nomatch' return stru800u82e_match su800 pattern start match_index plain_flag nomatch endu800 posu800 this function returns au800 single character from theu800 target string atu800 position posu800. usage #invoke string posu800 target_string index_value oru800 #invoke string posu800 target target_string posu800 index_value parameters target theu800 string tou800 search posu800 theu800 index foru800 theu800 character tou800 return ifu800 invoked using named parameters mediawiki will automatically remove anyu800 leading oru800 trailing whitespace from theu800 target string. inu800 some circumstances this isu800 desirable inu800 other cases oneu800 mayu800 want tou800 preserve theu800 whitespace. theu800 first character hasu800 anu800 index value ofu800 1u800. ifu800 oneu800 requests au800 negative value this function will select au800 character byu800 counting backwards from theu800 endu800 ofu800 theu800 string. inu800 other words posu800 1u800 isu800 theu800 same asu800 asking foru800 theu800 last character. au800 requested value ofu800 zero oru800 au800 value greater than theu800 length ofu800 theu800 string returns anu800 error. function stru800u82eposu800 frame local new_args stru800u82e_getparameters frameu82eargs 'target' 'posu800 local target_str new_args 'target' oru800   local posu800 tonumber new_args 'posu800 oru800 0u800 ifu800 posu800 0u800 oru800 mathu82eabsu800 posu800 mwu800u82eustringu82elenu800 target_str then return stru800u82e_error 'string index outu800 ofu800 range' endu800 return mwu800u82eustringu82esubu800 target_str posu800 posu800 endu800 str_find this function duplicates theu800 behavior ofu800 str_find including allu800 ofu800 itsu800 quirks. this isu800 provided inu800 order tou800 support existing templates butu800 isu800 notu800 recommended foru800 newu800 code andu800 templates. newu800 code isu800 recommended tou800 useu800 theu800 "find" function instead. returns theu800 first index inu800 "source" that isu800 au800 match tou800 "target". indexing isu800 1u800 based andu800 theu800 function returns 1u800 ifu800 theu800 "target" string isu800 notu800 present inu800 "source". important note ifu800 theu800 "target" string isu800 empty missing this function returns au800 value ofu800 "1u800" which isu800 generally unexpected behavior andu800 must beu800 accounted foru800 separatetly. function stru800u82estr_find frame local new_args stru800u82e_getparameters frameu82eargs 'source' 'target' local source_str new_args 'source' oru800   local target_str new_args 'target' oru800   ifu800 target_str   then return 1u800 endu800 local start mwu800u82eustringu82efind source_str target_str 1u800 true ifu800 start nilu800 then start 1u800 endu800 return start endu800 find this function allows oneu800 tou800 search foru800 au800 target string oru800 pattern within another string. usage #invoke string find source_str target_string start_index plain_flag oru800 #invoke string find source source_str target target_str start start_index plain plain_flag parameters source theu800 string tou800 search target theu800 string oru800 pattern tou800 find within source start theu800 index within theu800 source string tou800 start theu800 search defaults tou800 1u800 plain boolean flag indicating that target should beu800 understood asu800 plain text andu800 notu800 asu800 au800 luau800 style regular expression defaults tou800 true ifu800 invoked using named parameters mediawiki will automatically remove anyu800 leading oru800 trailing whitespace from theu800 parameter. inu800 some circumstances this isu800 desirable inu800 other cases oneu800 mayu800 want tou800 preserve theu800 whitespace. this function returns theu800 first index "start" where "target" canu800 beu800 found within "source". indices areu800 1u800 based. ifu800 "target" isu800 notu800 found then this function returns 0u800. ifu800 either "source" oru800 "target" areu800 missing empty this function also returns 0u800. this function should beu800 safe foru800 utfu800 8u800 strings. function stru800u82efind frame local new_args stru800u82e_getparameters frameu82eargs 'source' 'target' 'start' 'plain' local source_str new_args 'source' oru800   local pattern new_args 'target' oru800   local start_pos tonumber new_args 'start' oru800 1u800 local plain new_args 'plain' oru800 true ifu800 source_str   oru800 pattern   then return 0u800 endu800 plain stru800u82e_getboolean plain local start mwu800u82eustringu82efind source_str pattern start_pos plain ifu800 start nilu800 then start 0u800 endu800 return start endu800 replace this function allows oneu800 tou800 replace au800 target string oru800 pattern within another string. usage #invoke string replace source_str pattern_string replace_string replacement_count plain_flag oru800 #invoke string replace source source_string pattern pattern_string replace replace_string count replacement_count plain plain_flag parameters source theu800 string tou800 search pattern theu800 string oru800 pattern tou800 find within source replace theu800 replacement text count theu800 number ofu800 occurences tou800 replace defaults tou800 allu800. plain boolean flag indicating that pattern should beu800 understood asu800 plain text andu800 notu800 asu800 au800 luau800 style regular expression defaults tou800 true function stru800u82ereplace frame local new_args stru800u82e_getparameters frameu82eargs 'source' 'pattern' 'replace' 'count' 'plain' local source_str new_args 'source' oru800   local pattern new_args 'pattern' oru800   local replace new_args 'replace' oru800   local count tonumber new_args 'count' local plain new_args 'plain' oru800 true ifu800 source_str   oru800 pattern   then return source_str endu800 plain stru800u82e_getboolean plain ifu800 plain then pattern stru800u82e_escapepattern pattern replace mwu800u82eustringu82egsub replace " " " " only need tou800 escape replacement sequences. endu800 local result ifu800 count nilu800 then result mwu800u82eustringu82egsub source_str pattern replace count else result mwu800u82eustringu82egsub source_str pattern replace endu800 return result endu800 simple function tou800 pipe stringu82erepu800 tou800 templates. function stru800u82erepu800 frame local repetitions tonumber frameu82eargs 2u800 ifu800 notu800 repetitions then return stru800u82e_error 'function repu800 expects au800 number asu800 second parameter received "' .. frameu82eargs 2u800 oru800   .. '"' endu800 return stringu82erepu800 frameu82eargs 1u800 oru800   repetitions endu800 escapepattern this function escapes special characters from au800 luau800 string pattern. seeu800 1u800 foru800 details onu800 howu800 patterns work. 1u800 usage #invoke string escapepattern pattern_string parameters pattern_string theu800 pattern string tou800 escape. function stru800u82eescapepattern frame local pattern_str frameu82eargs 1u800 ifu800 notu800 pattern_str then return stru800u82e_error 'nou800 pattern string specified' endu800 local result stru800u82e_escapepattern pattern_str return result endu800 count this function counts theu800 number ofu800 occurrences ofu800 oneu800 string inu800 another. function stru800u82ecount frame local args stru800u82e_getparameters frameu82eargs 'source' 'pattern' 'plain' local source argsu82esource oru800   local pattern argsu82epattern oru800   local plain stru800u82e_getboolean argsu82eplain oru800 true ifu800 plain then pattern stru800u82e_escapepattern pattern endu800 local _u800 count mwu800u82eustringu82egsub source pattern   return count endu800 endswith this function determines whether au800 string ends with another string. function stru800u82eendswith frame local args stru800u82e_getparameters frameu82eargs 'source' 'pattern' local source argsu82esource oru800   local pattern argsu82epattern oru800   ifu800 pattern   then allu800 strings endu800 with theu800 empty string. return "yesu800" endu800 ifu800 mwu800u82eustringu82esubu800 source mwu800u82eustringu82elenu800 pattern 1u800 pattern then return "yesu800" else return "" endu800 endu800 join join allu800 nonu800 empty arguments together; theu800 first argument isu800 theu800 separator. usage #invoke string join sepu800 oneu800 twou800 three function stru800u82ejoin frame local args local sepu800 foru800 _u800 vu800 inu800 ipairs frameu82eargs dou800 ifu800 sepu800 then ifu800 vu800   then tableu82einsert args vu800 endu800 else sepu800 vu800 endu800 endu800 return tableu82econcat args sepu800 oru800   endu800 helper function that populates theu800 argument list given that user mayu800 need tou800 useu800 au800 mixu800 ofu800 named andu800 unnamed parameters. this isu800 relevant because named parameters areu800 notu800 identical tou800 unnamed parameters dueu800 tou800 string trimming andu800 when dealing with strings weu800 sometimes want tou800 either preserve oru800 remove that whitespace depending onu800 theu800 application. function stru800u82e_getparameters frame_args arg_list local new_args local index 1u800 local value foru800 _u800 argu800 inu800 ipairs arg_list dou800 value frame_args argu800 ifu800 value nilu800 then value frame_args index index index 1u800 endu800 new_args argu800 value endu800 return new_args endu800 helper function tou800 handle error messages. function stru800u82e_error error_str local frame mwu800u82egetcurrentframe local error_category frameu82eargsu82eerror_category oru800 'u8d8aeu8d8b7u8d8a7u8d987u8d8a7u8db8c u8daafu8d8b2u8d8a7u8d8b1u8d8b4u8e2808cu8d8b4u8d8afu8d987 u8d8aau8d988u8d8b3u8d8b7 u8d9beu8d988u8d8afu8d985u8d8a7u8d986 string' local ignore_errors frameu82eargsu82eignore_errors oru800 false local no_category frameu82eargsu82eno_category oru800 false ifu800 stru800u82e_getboolean ignore_errors then return   endu800 local error_str ' string module error ' .. error_str .. ' ' ifu800 error_category   andu800 notu800 stru800u82e_getboolean no_category then error_str ' category ' .. error_category .. ' ' .. error_str endu800 return error_str endu800 helper function tou800 interpret boolean strings function stru800u82e_getboolean boolean_str local boolean_value ifu800 type boolean_str 'string' then boolean_str boolean_str lower ifu800 boolean_str 'false' oru800 boolean_str 'nou800' oru800 boolean_str '0u800' oru800 boolean_str   then boolean_value false else boolean_value true endu800 elseif type boolean_str 'boolean' then boolean_value boolean_str else error 'nou800 boolean value found' endu800 return boolean_value endu800 helper function that escapes allu800 pattern characters sou800 that they will beu800 treated asu800 plain text. function stru800u82e_escapepattern pattern_str return mwu800u82eustringu82egsub pattern_str " . * " " 1u800" endu800 return stru800 