Module:String: Difference between revisions

282 bytes added ,  5 years ago
export str._match function for use in other modules - syncing from sandbox
m (use tabs instead of spaces, and remove trailing whitespace)
(export str._match function for use in other modules - syncing from sandbox)
Line 148: Line 148:


]]
]]
function str.match( frame )
-- This sub-routine is exported for use in other modules
local new_args = str._getParameters( frame.args, {'s', 'pattern', 'start', 'match', 'plain', 'nomatch'} );
function str._match( s, pattern, start, match_index, 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'];
 
if s == '' then
if s == '' then
return str._error( 'Target string is empty' );
return str._error( 'Target string is empty' );
Line 163: Line 156:
return str._error( 'Pattern string is empty' );
return str._error( 'Pattern string is empty' );
end
end
start = tonumber(start) or 1
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
if math.abs(start) < 1 or math.abs(start) > mw.ustring.len( s ) then
return str._error( 'Requested start is out of range' );
return str._error( 'Requested start is out of range' );
Line 214: Line 208:
return result;
return result;
end
end
end
-- 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, nomatch )
end
end


Anonymous user