Anonymous user
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
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
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|
OR
{{#invoke:String|replace_plain|source=source_str|pattern=pattern_str|replace=replace_string|
count=replacement_count|plain=pattern_flag}}
Parameters
source: The string to search
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.
local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', '
local source_str = new_args['source'] or '';
local pattern = new_args['pattern'] or '';
local replace = new_args['replace'] or '';
local
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 = str._escapePattern( replace );
▲ end
local result;
if
result = mw.ustring.gsub( source_str,
else
result = mw.ustring.gsub( source_str,
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
▲ else
▲ 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
|