Difference between revisions of "Module:Str endswith"

From Dharmawiki
Jump to navigation Jump to search
en>BU Rob13
m (Changed protection level for "Template:Str endswith": Highly visible template ([Edit=Require template editor access] (indefinite) [Move=Require administrator access] (indefinite)))
 
m (1 revision imported)
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{#invoke:Str endswith|main}}<noinclude>
+
-- This module implements {{str endswith}}.
{{documentation}}
+
 
</noinclude>
+
local TRUE_STRING = 'yes'
 +
local FALSE_STRING = ''
 +
 
 +
local p = {}
 +
 
 +
local function trim(s)
 +
return s:match('^%s*(.-)%s*$')
 +
end
 +
 
 +
function p.main(frame)
 +
local args = frame:getParent().args
 +
local s = args[1]
 +
local pattern = args[2]
 +
if not s or not pattern then
 +
-- TRUE_STRING is not the natural choice here, but is needed for
 +
-- backwards compatibility.
 +
return TRUE_STRING
 +
end
 +
s = trim(s)
 +
pattern = trim(pattern)
 +
if pattern == '' then
 +
-- All strings end with the empty string.
 +
return TRUE_STRING
 +
end
 +
if mw.ustring.sub(s, 0 - mw.ustring.len(pattern), -1) == pattern then
 +
return TRUE_STRING
 +
else
 +
return FALSE_STRING
 +
end
 +
end
 +
 
 +
return p

Latest revision as of 10:59, 11 February 2019

-- This module implements yes.

local TRUE_STRING = 'yes' local FALSE_STRING =

local p = {}

local function trim(s) return s:match('^%s*(.-)%s*$') end

function p.main(frame) local args = frame:getParent().args local s = args[1] local pattern = args[2] if not s or not pattern then -- TRUE_STRING is not the natural choice here, but is needed for -- backwards compatibility. return TRUE_STRING end s = trim(s) pattern = trim(pattern) if pattern == then -- All strings end with the empty string. return TRUE_STRING end if mw.ustring.sub(s, 0 - mw.ustring.len(pattern), -1) == pattern then return TRUE_STRING else return FALSE_STRING end end

return p