Module:String2: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
(Add startswith, analogous to endswith) |
(Per TfD outcome) |
||
Line 94: | Line 94: | ||
local str = mw.text.trim(frame.args[1] or "") |
local str = mw.text.trim(frame.args[1] or "") |
||
return mw.text.nowiki(str) |
return mw.text.nowiki(str) |
||
end |
|||
-- posnq (position, no quotes) returns the numerical start position of the first occurrence |
|||
-- of one piece of text ("match") inside another ("str"). |
|||
-- It returns nil if no match is found, or if either parameter is blank. |
|||
-- It takes the text to be searched in as the first unnamed parameter, which is trimmed. |
|||
-- It takes the text to match as the second unnamed parameter, which is trimmed and |
|||
-- any double quotes " are stripped out. |
|||
p.posnq = function(frame) |
|||
local args = frame.args |
|||
local pargs = frame:getParent().args |
|||
for k, v in pairs(pargs) do |
|||
args[k] = v |
|||
end |
|||
local str = mw.text.trim(args[1] or args.source or "") |
|||
local match = mw.text.trim(args[2] or args.target or ""):gsub('"', '') |
|||
if str == "" or match == "" then return nil end |
|||
local plain = mw.text.trim(args[3] or args.plain or "") |
|||
if plain == "false" then plain = false else plain = true end |
|||
local nomatch = mw.text.trim(args[4] or args.nomatch or "") |
|||
-- just take the start position |
|||
local pos = mw.ustring.find(str, match, 1, plain) or nomatch |
|||
return pos |
|||
end |
end |
||