Modul:Str: Unterschied zwischen den Versionen
te>NordNordWest K (Schützte „Modul:Str“: Häufig eingebundenes Modul ([Bearbeiten=Nur angemeldete, nicht neue Benutzer] (unbeschränkt) [Verschieben=Nur angemeldete, nicht neue Benutzer] (unbeschränkt))) |
Admin (Diskussion | Beiträge) K (20 Versionen importiert) |
||
(17 dazwischenliegende Versionen von 8 Benutzern werden nicht angezeigt) | |||
Zeile 1: | Zeile 1: | ||
local Str = {} | local Str = {} | ||
+ | |||
function Str.len(frame) | function Str.len(frame) | ||
− | return | + | return mw.ustring.len(frame.args[1]) |
end | end | ||
function Str.left(frame) | function Str.left(frame) | ||
− | return | + | local idx = tonumber(frame.args[2]) |
+ | if (not idx) or idx < 1 then | ||
+ | return "" | ||
+ | end | ||
+ | return mw.ustring.sub(frame.args[1],1,idx) | ||
end | end | ||
function Str.right(frame) | function Str.right(frame) | ||
− | + | local laenge = tonumber(frame.args[2]) | |
+ | if (not laenge) or laenge < 1 then | ||
+ | return "" | ||
+ | else | ||
+ | laenge = - laenge | ||
+ | end | ||
+ | return mw.ustring.sub(frame.args[1],laenge,-1) | ||
end | end | ||
function Str.index(frame) | function Str.index(frame) | ||
− | return | + | local idx = tonumber(frame.args[2]) |
+ | if (not idx) or idx < 1 then | ||
+ | return "" | ||
+ | end | ||
+ | return mw.ustring.sub(frame.args[1],idx,idx) | ||
end | end | ||
function Str.sub(frame) | function Str.sub(frame) | ||
− | + | local von = tonumber(frame.args[2]) | |
− | return | + | local laenge = tonumber(frame.args[3]) |
+ | if (not von) or (not laenge) then | ||
+ | return "" | ||
+ | end | ||
+ | if (von < 1) then | ||
+ | von = 1 | ||
+ | end | ||
+ | local bis = von + laenge - 1 | ||
+ | if (bis < von) then | ||
+ | return "" | ||
+ | end | ||
+ | return mw.ustring.sub(frame.args[1],von,bis) | ||
end | end | ||
− | return Str | + | function Str.crop(frame) |
+ | local s = frame.args[1] | ||
+ | local cut = tonumber(frame.args[2]) | ||
+ | local laenge = mw.ustring.len(s) | ||
+ | if (not cut) or (cut < 1) then | ||
+ | return s | ||
+ | end | ||
+ | return mw.ustring.sub(s,1,laenge - cut) | ||
+ | end | ||
+ | |||
+ | function Str.cropleft(frame) | ||
+ | local s = frame.args[1] | ||
+ | local cut = tonumber(frame.args[2]) | ||
+ | local laenge = mw.ustring.len(s) | ||
+ | if (not cut) or (cut < 1) then | ||
+ | return s | ||
+ | end | ||
+ | return mw.ustring.sub(s,cut+1,-1) | ||
+ | end | ||
+ | |||
+ | function Str.find(frame) | ||
+ | if not frame.args[2] or frame.args[2] == "" then | ||
+ | return 1 | ||
+ | end | ||
+ | local idx = mw.ustring.find(frame.args[1], frame.args[2],1, true) | ||
+ | if idx then | ||
+ | return idx | ||
+ | else | ||
+ | return -1 | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function Str.hex2dez(frame) | ||
+ | a = tonumber(frame.args[1],16) | ||
+ | if a then | ||
+ | return a | ||
+ | else | ||
+ | return 0 | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function Str.match(frame) | ||
+ | local text = frame.args[1] or "" | ||
+ | local pattern = frame.args[2] or "" | ||
+ | local index = tonumber(frame.args[3]) or 0 | ||
+ | if (text == "" or pattern == "") then return "" end | ||
+ | |||
+ | -- return all captures (denoted by brackets in the pattern) if index is zero, otherwise return only the index-th capture | ||
+ | if index <= 0 then | ||
+ | return mw.ustring.match(text, pattern) | ||
+ | else | ||
+ | return ({mw.ustring.match(text, pattern)})[index] | ||
+ | end | ||
+ | end | ||
+ | |||
+ | function escape_lua_regex(str) | ||
+ | return mw.ustring.gsub(str, ".", { | ||
+ | ["%"] = "%%"; | ||
+ | ["^"] = "%^"; | ||
+ | ["$"] = "%$"; | ||
+ | ["."] = "%."; | ||
+ | ["("] = "%("; | ||
+ | [")"] = "%)"; | ||
+ | ["["] = "%["; | ||
+ | ["]"] = "%]"; | ||
+ | ["?"] = "%?"; | ||
+ | ["*"] = "%*"; | ||
+ | ["+"] = "%+"; | ||
+ | ["-"] = "%-"; | ||
+ | ["\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 | ||
+ | |||
+ | 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 | ||
+ | |||
+ | -- richtet Zahlen numerisch aus | ||
+ | function Str.adjustnumber(frame) | ||
+ | local ausgabe; | ||
+ | local text = frame.args[1] or "" -- Text, der bearbeitet werden soll, i.d.R. eine Dezimalzahl | ||
+ | local i_li = math.floor(tonumber(frame.args[2])) or 2; -- maximale Stellen links vom Trennzeichen | ||
+ | local i_re = math.floor(tonumber(frame.args[3])) or 2; -- maximale Stellen rechts vom Trennzeichen | ||
+ | local sign = frame.args['Z'] or "," -- Trennzeichen | ||
+ | local zeroes='00000000000000000000'; -- 20 duerften ausreichen. | ||
+ | local zpos = 0; | ||
+ | local len = mw.ustring.len(text); | ||
+ | if not text or sign == "" then | ||
+ | zpos = len + 1; | ||
+ | else | ||
+ | zpos = mw.ustring.find(text, sign,1, true) or len; | ||
+ | end | ||
+ | |||
+ | local zl = 0; | ||
+ | local zr = 0; | ||
+ | local t_li = ""; | ||
+ | local t_re = ""; | ||
+ | local z_li =""; | ||
+ | local z_re =""; | ||
+ | |||
+ | if zpos > 1 then | ||
+ | t_li = mw.ustring.sub(text,1, zpos-1); | ||
+ | else | ||
+ | t_li=""; | ||
+ | end | ||
+ | |||
+ | if len-zpos > 0 then | ||
+ | t_re = mw.ustring.sub(text,zpos+1,-1); | ||
+ | else | ||
+ | t_re=""; | ||
+ | end | ||
+ | |||
+ | zl = i_li - mw.ustring.len(t_li); | ||
+ | if zl < 1 then | ||
+ | zl = 0; | ||
+ | z_li = ""; | ||
+ | else | ||
+ | z_li = '<span style="visibility:hidden;">' .. mw.ustring.sub(zeroes,1,zl) .. '</span>'; | ||
+ | end | ||
+ | |||
+ | zr = i_re - mw.ustring.len(t_re); | ||
+ | if zr < 1 then | ||
+ | zr = 0; | ||
+ | z_re =""; | ||
+ | else | ||
+ | z_re ='<span style="visibility:hidden;">' .. mw.ustring.sub(zeroes,1,zr) .. '</span>'; | ||
+ | end | ||
+ | ausgabe = z_li .. t_li .. sign .. t_re .. z_re; | ||
+ | return ausgabe; | ||
+ | end | ||
+ | return Str |
Aktuelle Version vom 6. September 2019, 12:27 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Str/Doku erstellt werden
local Str = {}
function Str.len(frame)
return mw.ustring.len(frame.args[1])
end
function Str.left(frame)
local idx = tonumber(frame.args[2])
if (not idx) or idx < 1 then
return ""
end
return mw.ustring.sub(frame.args[1],1,idx)
end
function Str.right(frame)
local laenge = tonumber(frame.args[2])
if (not laenge) or laenge < 1 then
return ""
else
laenge = - laenge
end
return mw.ustring.sub(frame.args[1],laenge,-1)
end
function Str.index(frame)
local idx = tonumber(frame.args[2])
if (not idx) or idx < 1 then
return ""
end
return mw.ustring.sub(frame.args[1],idx,idx)
end
function Str.sub(frame)
local von = tonumber(frame.args[2])
local laenge = tonumber(frame.args[3])
if (not von) or (not laenge) then
return ""
end
if (von < 1) then
von = 1
end
local bis = von + laenge - 1
if (bis < von) then
return ""
end
return mw.ustring.sub(frame.args[1],von,bis)
end
function Str.crop(frame)
local s = frame.args[1]
local cut = tonumber(frame.args[2])
local laenge = mw.ustring.len(s)
if (not cut) or (cut < 1) then
return s
end
return mw.ustring.sub(s,1,laenge - cut)
end
function Str.cropleft(frame)
local s = frame.args[1]
local cut = tonumber(frame.args[2])
local laenge = mw.ustring.len(s)
if (not cut) or (cut < 1) then
return s
end
return mw.ustring.sub(s,cut+1,-1)
end
function Str.find(frame)
if not frame.args[2] or frame.args[2] == "" then
return 1
end
local idx = mw.ustring.find(frame.args[1], frame.args[2],1, true)
if idx then
return idx
else
return -1
end
end
function Str.hex2dez(frame)
a = tonumber(frame.args[1],16)
if a then
return a
else
return 0
end
end
function Str.match(frame)
local text = frame.args[1] or ""
local pattern = frame.args[2] or ""
local index = tonumber(frame.args[3]) or 0
if (text == "" or pattern == "") then return "" end
-- return all captures (denoted by brackets in the pattern) if index is zero, otherwise return only the index-th capture
if index <= 0 then
return mw.ustring.match(text, pattern)
else
return ({mw.ustring.match(text, pattern)})[index]
end
end
function escape_lua_regex(str)
return mw.ustring.gsub(str, ".", {
["%"] = "%%";
["^"] = "%^";
["$"] = "%$";
["."] = "%.";
["("] = "%(";
[")"] = "%)";
["["] = "%[";
["]"] = "%]";
["?"] = "%?";
["*"] = "%*";
["+"] = "%+";
["-"] = "%-";
["\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
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
-- richtet Zahlen numerisch aus
function Str.adjustnumber(frame)
local ausgabe;
local text = frame.args[1] or "" -- Text, der bearbeitet werden soll, i.d.R. eine Dezimalzahl
local i_li = math.floor(tonumber(frame.args[2])) or 2; -- maximale Stellen links vom Trennzeichen
local i_re = math.floor(tonumber(frame.args[3])) or 2; -- maximale Stellen rechts vom Trennzeichen
local sign = frame.args['Z'] or "," -- Trennzeichen
local zeroes='00000000000000000000'; -- 20 duerften ausreichen.
local zpos = 0;
local len = mw.ustring.len(text);
if not text or sign == "" then
zpos = len + 1;
else
zpos = mw.ustring.find(text, sign,1, true) or len;
end
local zl = 0;
local zr = 0;
local t_li = "";
local t_re = "";
local z_li ="";
local z_re ="";
if zpos > 1 then
t_li = mw.ustring.sub(text,1, zpos-1);
else
t_li="";
end
if len-zpos > 0 then
t_re = mw.ustring.sub(text,zpos+1,-1);
else
t_re="";
end
zl = i_li - mw.ustring.len(t_li);
if zl < 1 then
zl = 0;
z_li = "";
else
z_li = '<span style="visibility:hidden;">' .. mw.ustring.sub(zeroes,1,zl) .. '</span>';
end
zr = i_re - mw.ustring.len(t_re);
if zr < 1 then
zr = 0;
z_re ="";
else
z_re ='<span style="visibility:hidden;">' .. mw.ustring.sub(zeroes,1,zr) .. '</span>';
end
ausgabe = z_li .. t_li .. sign .. t_re .. z_re;
return ausgabe;
end
return Str