Difference between revisions of "Module:String"

From WPRDC Wiki
Jump to navigation Jump to search
>Anomie
(+ substr variant that takes a length instead of an ending position)
>Anomie
(Oh, the existing code is 0-based)
Line 10: Line 10:


function str.sublength( frame )
function str.sublength( frame )
     local i = tonumber( frame.args.i ) or 1
     local i = tonumber( frame.args.i ) or 0
     local len = tonumber( frame.args.len )
     local len = tonumber( frame.args.len )
     return mw.ustring.sub( frame.args.s, i, len and ( i + len - 1 ) )
     return mw.ustring.sub( frame.args.s, i + 1, len and ( i + len ) )
end
end



Revision as of 01:50, 21 February 2013

Documentation for this module may be created at Module:String/doc

local str = {}

function str.len( frame )
    return mw.ustring.len( frame.args.s )
end

function str.sub( frame )
    return mw.ustring.sub( frame.args.s, tonumber( frame.args.i ), tonumber( frame.args.j ) )
end

function str.sublength( frame )
    local i = tonumber( frame.args.i ) or 0
    local len = tonumber( frame.args.len )
    return mw.ustring.sub( frame.args.s, i + 1, len and ( i + len ) )
end

function str.match( frame )
    return mw.ustring.match( frame.args.s, frame.args.pattern, tonumber( frame.args.i ) )
end

return str