Mòdul:StringFunc: diferència entre les revisions

Contingut suprimit Contingut afegit
mCap resum de modificació
arreglo funció split, només per text pla
Línia 37:
Split
 
This function Splits a string based on a patternseparator, returns nth substring based on count.
 
Usage:
{{#invoke:StringFunc|split|source_string|patternseparator|count|plain}}
 
Parameters:
source: The string to return a subset of
patternseparator: The pattern or string to split on
count: The nth substring based on the pattern to return
plain: A flag indicating if the pattern should be understood as plain text, defaults to true.
]]
function p.split( frame )
local new_args = p._getParameters( frame.args, {'source', 'pattern', 'count', 'plain'} )
local source_str = new_args['source'] or '';
local pattern = new_args['pattern'] or '';
local pattern_len = mw.ustring.len(pattern);
if source_str == '' or pattern == '' then
return source_str;
end
local l_plain = p._getBoolean( new_args['plain'] or true );
pattern = p._escapePattern( pattern );
if l_plain then
pattern = p._escapePattern( pattern );
end
local plain = false;
pattern = "[" .. pattern .. "]"
local ret_count = tonumber( new_args['count'] ) or 1;
local start = 1;
local iter = mw.ustring.find(source_str, pattern, start, plain) - 1;
if iter == nil then
return source_str;
Linha 71 ⟶ 68:
end
for i=2, ret_count do
start = iter+pattern_len + 1;
iter = mw.ustring.find(source_str, pattern, start, plain) - 1;
if iter == nil then
break