Module:String: Difference between revisions

From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
include more generic replacement function
(replace_plain fix)
(include more generic replacement function)
Line 127:
end
ifplain type= str._getBoolean( plain ) == 'string' then;
plain = plain:lower();
if plain == 'false' or plain == 'no' or plain == '0' then
plain = false;
else
plain = true;
end
end
 
local start = mw.ustring.find( source_str, pattern, start_pos, plain )
Line 145 ⟶ 138:
 
--[====[
replace
replace_plain
 
This function allows one to replace a target string or pattern within another
Line 151 ⟶ 144:
 
Usage:
{{#invoke:String|replace_plain|source_str|pattern_string|replace_string|firstonlyflagreplacement_count|pattern_flag}}
OR
{{#invoke:String|replace_plain|source=source_str|pattern=pattern_str|replace=replace_string|firstonly=firstonlyflag}}
count=replacement_count|plain=pattern_flag}}
 
Parameters
source: The string to search
pattenpattern: The string or pattern to find within source
replace: The replacement text
count: The number of occurences to replace, defaults to all.
firstonly: Boolean flag indicating that only the first occurence found should be replaced
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_plainreplace( frame )
local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'firstonlycount', 'plain' } );
local source_str = new_args['source'] or '';
local pattern = new_args['pattern'] or '';
local replace = new_args['replace'] or '';
local firstonlycount = tonumber( new_args['firstonlycount'] or '');
local plain = new_args['plain'] or true;
firstonly = firstonly:lower();
if source_str == '' or pattern == '' then
return source_str;
end
plain = str._getBoolean( plain );
 
if plain then
local pattern_plain = mw.ustring.gsub(pattern, '%%', '%%%%');
pattern = str._escapePattern( pattern );
local replace_plain = mw.ustring.gsub(replace, '%%', '%%%%');
replace = str._escapePattern( replace );
end
local result;
 
if firstonlycount == 'true' or firstonly == 'yes' or firstonly =~= '1'nil then
result = mw.ustring.gsub( source_str, pattern_plainpattern, replace_plainreplace, 1count );
else
result = mw.ustring.gsub( source_str, pattern_plainpattern, replace_plain, nreplace );
end
 
return result;
Line 208:
return new_args;
end
 
--[====[
Helper Function to interpret boolean strings
]====]
function str._getBoolean( boolean_str )
local boolean_value;
if type( boolean_str ) == 'string' then
plainboolean_str = plainboolean_str:lower();
if plainboolean_str == 'false' or plainboolean_str == 'no' or plainboolean_str == '0' then
plainboolean_value = false;
else
plainboolean_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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.

Navigation menu