Modul:Str: Unterschied zwischen den Versionen

K 1 Version importiert: Citation Templates
imported>Count Count
K Änderte den Schutzstatus für „Modul:Str“: Häufig eingebundenes Modul ([Bearbeiten=Nur Administratoren] (unbeschränkt) [Verschieben=Nur Administratoren] (unbeschränkt))
 
Zeile 1: Zeile 1:
  local Str = {}  
function escape_lua_regex(str)
return mw.ustring.gsub(str, ".", {
    ["%"] = "%%";
    ["^"] = "%^";
    ["$"] = "%$";
    ["."] = "%.";
    ["("] = "%(";
    [")"] = "%)";
    ["["] = "%[";
    ["]"] = "%]";
    ["?"] = "%?";
    ["*"] = "%*";
    ["+"] = "%+";
    ["-"] = "%-";
    ["\0"] = "%z";
  })
end
 
 
local Str = {}  


     function Str.len(frame)
     function Str.len(frame)
Zeile 102: Zeile 121:
     end
     end


function escape_lua_regex(str)
function Str.rep(frame)
return mw.ustring.gsub(str, ".", {
local repetitions = tonumber(frame.args[2]) or 0;
    ["%"] = "%%";
return mw.ustring.rep( frame.args[1] or '', repetitions )
    ["^"] = "%^";
    ["$"] = "%$";
    ["."] = "%.";
    ["("] = "%(";
    [")"] = "%)";
    ["["] = "%[";
    ["]"] = "%]";
    ["?"] = "%?";
    ["*"] = "%*";
    ["+"] = "%+";
    ["-"] = "%-";
    ["\0"] = "%z";
  })
end
function Str.replace(frame)
local text = frame.args[1] or ""      -- Text, der bearbeitet werden soll
local search = frame.args[2] or ""    -- Textstellen innerhalb von "text" die ersetzt werden sollen
if text == "" or search == "" then return "" end
local replace = frame.args[3] or ""  -- Ersetzungstext
local count = tonumber(frame.args[4]) -- Anzahl der Ersetzungen (optional)
local regexsearch = frame.args[5]     -- beliebiger Wert um dafür zu sorgen, dass der Suchtext "search" als Lua-regulärer Ausdruck behandelt werden soll
if not regexsearch or regexsearch == "" then
search = escape_lua_regex(search)
replace = mw.ustring.gsub(replace, "%%", "%%%%")
end
end
function Str.replace(frame)
local text = frame.args[1] or ""      -- Text, der bearbeitet werden soll
local search = frame.args[2] or ""    -- Textstellen innerhalb von "text" die ersetzt werden sollen
if text == "" or search == "" then return "" end
   
   
local result
local replace = frame.args[3] or ""  -- Ersetzungstext
if count then
local count = tonumber(frame.args[4]) -- Anzahl der Ersetzungen (optional)
result,_ = mw.ustring.gsub(text, search, replace, count)
local regexsearch = frame.args[5]    -- beliebiger Wert um dafür zu sorgen, dass der Suchtext "search" als Lua-regulärer Ausdruck behandelt werden soll
else
if not regexsearch or regexsearch == "" then
result,_ = mw.ustring.gsub(text, search, replace)
search = escape_lua_regex(search)
replace = mw.ustring.gsub(replace, "%%", "%%%%")
end
local result
if count then
result,_ = mw.ustring.gsub(text, search, replace, count)
else
result,_ = mw.ustring.gsub(text, search, replace)
end
return result
end
end
return result
end


-- richtet Zahlen numerisch aus
-- richtet Zahlen numerisch aus