Naar inhoud springen

Module:Stringsjablonen

Uit WikiWoordenboek

Documentatie voor deze module kan aangemaakt worden op de volgende pagina: Module:Stringsjablonen/doc

local p = {}

local function txtarg(frame, key)
	return mw.text.trim(frame.args[key])
end

function p.strfindspace(frame)
	return mw.ustring.find(txtarg(frame, 1), ' ') or ''
end

function p.strindex(frame)
	local s = txtarg(frame, 1)
	local i = tonumber(frame.args[2])
	if i <= 0 then
		return frame:expandTemplate{ title='FormattingError', args={'((str index)): Index must be positive.'}}
	end
	if i > #s then
		return frame:expandTemplate{ title='FormattingError', args={'((str index)): String is not long enough.'}}
	end
	return mw.ustring.char(mw.ustring.codepoint(s, i))
end

function p.strlen(frame)
	local r = mw.ustring.len(txtarg(frame, 1))
	if r > 500 then
		r = 500
	end
	return r
end

-- ≥, greater or equal
local function ge(a, b)
	return a >= b
end

local function strcmplen(frame, cmp)
	local a = p.strlen(frame)
	local b = tonumber(frame.args[2])
	local r
	if cmp(a, b) then
		r = frame.args[3]
	else
		r = frame.args[4]
	end
	return r
end

-- str ≥ len
function p.strgelen(frame)
	return strcmplen(frame, ge)
end

function p.strleft(frame)
	local s = txtarg(frame, 1)
	local n = tonumber(frame.args[2])
	if mw.ustring.len(s) == 0 then
		return ''	-- prevent infinite loop
	end
	if n > 500 then
		n = 500
	end
	while mw.ustring.len(s) < n do
		s = s .. s
	end
	return mw.ustring.sub(s, 1, n)
end

function p.strright(frame)
	local s = txtarg(frame, 1)
	if mw.ustring.len(s) > 100 then
		return frame:expandTemplate{ title='FormattingError', args={'max index is 100 for str right'}}
	end
	local i = tonumber(frame.args[2])
	return mw.ustring.sub(s, i + 1)
end

function p.strrightc(frame)
	local i = tonumber(frame.args[2])
	if i == 0 then
		return ''
	end
	local s = txtarg(frame, 1)
	if mw.ustring.len(s) > 100 then
		return frame:expandTemplate{ title='FormattingError', args={'max index is 100 for str rightc'}}
	end
	return mw.ustring.sub(s, -i)
end

return p
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy