Module:String: Difference between revisions

include more generic replacement function
m>WOSlinker
(replace_plain fix)
m>Dragons flight
(include more generic replacement function)
Line 127: Line 127:
     end     
     end     
      
      
     if type( plain ) == 'string' then
     plain = str._getBoolean( plain );
        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 )
     local start = mw.ustring.find( source_str, pattern, start_pos, plain )
Line 145: Line 138:


--[====[
--[====[
replace_plain
replace


This function allows one to replace a target string or pattern within another
This function allows one to replace a target string or pattern within another
Line 151: Line 144:


Usage:
Usage:
{{#invoke:String|replace_plain|source_str|pattern_string|replace_string|firstonlyflag}}
{{#invoke:String|replace_plain|source_str|pattern_string|replace_string|replacement_count|pattern_flag}}
OR
OR
{{#invoke:String|replace_plain|source=source_str|pattern=pattern_str|replace=replace_string|firstonly=firstonlyflag}}
{{#invoke:String|replace_plain|source=source_str|pattern=pattern_str|replace=replace_string|
  count=replacement_count|plain=pattern_flag}}


Parameters
Parameters
     source: The string to search
     source: The string to search
     patten: The string or pattern to find within source
     pattern: The string or pattern to find within source
     replace: The replacement text
     replace: The replacement text
     firstonly: Boolean flag indicating that only the first occurence found should be replaced
     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_plain( frame )
function str.replace( frame )
     local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'firstonly' } );  
     local new_args = str._getParameters( frame.args, {'source', 'pattern', 'replace', 'count', 'plain' } );  
     local source_str = new_args['source'] or '';
     local source_str = new_args['source'] or '';
     local pattern = new_args['pattern'] or '';
     local pattern = new_args['pattern'] or '';
     local replace = new_args['replace'] or '';
     local replace = new_args['replace'] or '';
     local firstonly = new_args['firstonly'] or '';
     local count = tonumber( new_args['count'] );
     firstonly = firstonly:lower();
     local plain = new_args['plain'] or true;
          
          
     if source_str == '' or pattern == '' then
     if source_str == '' or pattern == '' then
         return source_str;
         return source_str;
     end     
     end     
    plain = str._getBoolean( plain );


     local pattern_plain = mw.ustring.gsub(pattern, '%%', '%%%%');
     if plain then
    local replace_plain = mw.ustring.gsub(replace, '%%', '%%%%');
        pattern = str._escapePattern( pattern );
        replace = str._escapePattern( replace );
    end
   
     local result;
     local result;


     if firstonly == 'true' or firstonly == 'yes' or firstonly == '1' then
     if count ~= nil then
         result = mw.ustring.gsub( source_str, pattern_plain, replace_plain, 1 );
         result = mw.ustring.gsub( source_str, pattern, replace, count );
     else
     else
         result = mw.ustring.gsub( source_str, pattern_plain, replace_plain, n );
         result = mw.ustring.gsub( source_str, pattern, replace );
     end
     end      


     return result;
     return result;
Line 208: Line 208:
     return new_args;
     return new_args;
end         
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' 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
return str
Anonymous user