Modul:Str2
Die Dokumentation für dieses Modul kann unter Modul:Str2/Doku erstellt werden
Die Dokumentation für dieses Modul kann unter Modul:Str2/Doku erstellt werden
-- Modul zum Test neuer Stringfunktionen, bevor sie ins Hauptmodul Str kommen.
-- trim: Entfernen aller Zeilenumbrüche, Austausch echter Tabulatoren durch Leerzeichen
-- anschließend Entfernung führender oder abschließender Leerzeichen.
local function trim(s)
s = mw.ustring.gsub(s,"\n","");
s = mw.ustring.gsub(s,"\t"," ");
while mw.ustring.sub(s,1,1) == " " do
s= mw.ustring.sub(s,2,-1);
end
while mw.ustring.sub(s,-1,-1) == " " do
s= mw.ustring.sub(s,1,-2);
end
return s;
end
local function trimextended(s)
local tbltags = {"sup","sub","small","div","span","tt","code"};
local pat="";
-- entferne Zeilenumbrüche
s = mw.ustring.gsub(s, "\n", "");
-- ersetze echten Tabulator durch ein Leerzeichen
s = mw.ustring.gsub(s, "\t", " ");
-- entferne die wichtigsten Tags
for i,v in ipairs(tbltags) do
pat ="<" .. v ..">";
s = mw.ustring.gsub(s,pat, "");
pat ="</" .. v ..">";
s = mw.ustring.gsub(s,pat, "");
end
-- ersetze die wichtigsten Entities durch Zeichen
s = mw.ustring.gsub(s," ", " ");
s = mw.ustring.gsub(s,">", ">");
s = mw.ustring.gsub(s,"<", "<");
s = mw.ustring.gsub(s,""", '"');
s = mw.ustring.gsub(s,"'", "'");
s = mw.ustring.gsub(s," ", " "); -- hier notwendig
s = mw.ustring.gsub(s,"&", "&"); -- muss am Ende stehen
s = trim(s);
return s;
end
function escape_lua_regex(str)
return mw.ustring.gsub(str, ".", {
["%"] = "%%";
["^"] = "%^";
["$"] = "%$";
["."] = "%.";
["("] = "%(";
[")"] = "%)";
["["] = "%[";
["]"] = "%]";
["?"] = "%?";
["*"] = "%*";
["+"] = "%+";
["-"] = "%-";
["\0"] = "%z";
})
end
local Str = {};
function Str.len(frame)
local s = trim((frame.args[1] or ""));
return mw.ustring.len(s);
end
function Str.left(frame)
local s = trim((frame.args[1] or ""));
local idx = tonumber(frame.args[2]) or 0;
if idx < 1 then
return "";
end
return mw.ustring.sub(s,1,idx);
end
function Str.reverse(frame)
local args=frame:getParent().args;
local s = trim((args[1] or ""));
return mw.ustring.reverse(s);
end
function Str.lower(frame)
local s = trim((frame.args[1] or ""));
return mw.ustring.lower(s);
end
function Str.upper(frame)
local s = trim((frame.args[1] or ""));
return mw.ustring.upper(s);
end
function Str.right(frame)
local s = trim((frame.args[1] or ""));
local length = tonumber(frame.args[2]) or 1;
if length < 1 then
return ""
else
length = -length;
end
return mw.ustring.sub(s,length,-1);
end
function Str.index(frame)
local s = trim((frame.args[1] or ""));
local idx = tonumber(frame.args[2]) or 0;
if idx < 1 then
return ""
end
return mw.ustring.sub(s,idx,idx)
end
function Str.sub(frame)
local s = trim((frame.args[1] or ""));
local von = tonumber(frame.args[2]) or 1;
local length = tonumber(frame.args[3]) or 0;
if (von < 1) then
von = 1;
end
local bis = von + length - 1;
if (bis < von) then
return "";
end
return mw.ustring.sub(s,von,bis)
end
function Str.crop(frame)
local s = trim((frame.args[1] or ""));
local cut = tonumber(frame.args[2]) or 0;
local length = mw.ustring.len(s);
if cut < 1 then
return s;
end
return mw.ustring.sub(s,1,length - cut);
end
function Str.cropleft(frame)
local s = trim((frame.args[1] or ""));
local cut = tonumber(frame.args[2]) or 0;
local length = mw.ustring.len(s);
if cut < 1 then
return s;
end
return mw.ustring.sub(s,cut+1,-1);
end
function Str.find(frame)
local text = trim((frame.args[1] or ""));
local pat = frame.args[2] or "";
if pat == "" then
return 1;
end
local idx = mw.ustring.find(text,pat,1,true);
if idx then
return idx;
else
return -1;
end
end
function Str.hex2dez(frame)
a = tonumber(frame.args[1],16) or 0;
return a;
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 Str.notless(frame)
local s = frame.args[1] or "";
s = trimextended(s);
local length = mw.ustring.len(s);
local pos = tonumber(frame.args[2]) or 0;
local iftrue = trim((frame.args[3] or ""));
local iffalse = trim((frame.args[4] or ""));
if length >= pos then
return iftrue;
else
return iffalse;
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
local replace = frame.args[3] or ""; -- Ersetzungstext
if text == "" or search == "" then
return "";
end
local count = tonumber(frame.args[4])-- Anzahl der Ersetzungen (optional)
local regexsearch = frame.args[5] or ""; -- beliebiger Wert um dafür zu sorgen, dass der Suchtext "search" als Lua-regulärer Ausdruck behandelt werden soll
if 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
function Str.minus(frame)
local s = frame.args[1] or "";
s = mw.ustring.gsub(s,'−','-'); -- Erstes Zeichen ist U+2212
s = mw.ustring.gsub(s,'‐','-'); -- Erstes Zeichen ist U+2010 (Viertelgeviertstrich)!
s = mw.ustring.gsub(s,'‒','-'); -- Erstes Zeichen ist U+2012 (Figure dash)!
s = mw.ustring.gsub(s,'–','-'); -- Erstes Zeichen ist U+2013 (Halbgeviertstrich)!
s = mw.ustring.gsub(s,'—','-'); -- Erstes Zeichen ist U+2014 (Geviertstrich)!
return s;
end
-- für Vorlage Str ≥ len
function Str.greaterorequal(frame)
local s = trim((frame.args[1] or "")); -- Text, der geprüft werden soll
local length = mw.ustring.len(s);
local limit = tonumber(frame.args[2] or "0") or 0;
local ge = frame.args[3] or "1";
local st = frame.args[4] or "";
if length < limit then
return st;
else
return ge;
end
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 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
while zl > 0 do
zl = zl - 1;
z_li = z_li .. "0";
end
z_li = '<span style="visibility:hidden;">' .. z_li .. '</span>';
end
zr = i_re - mw.ustring.len(t_re);
if zr < 1 then
zr = 0;
z_re ="";
else
while zr > 0 do
zr = zr - 1;
z_re = z_re .. "0";
end
z_re = '<span style="visibility:hidden;">' .. z_re .. '</span>';
end
ausgabe = z_li .. t_li .. sign .. t_re .. z_re;
return ausgabe;
end
-- spaltet einen String auf und gibt ein Stück oder die Anzahl der Stücke zurück
function Str.split(frame)
local ausgabe;
local pos = math.floor(tonumber(frame.args[1])) or 0; -- Wert, der das Ergebnis auswählt (<=0: Anzahl, sonst Stück)
local text = frame.args[2] or ""; -- Text, der gespalten werden soll
local pattern = frame.args[3] or ""; -- Muster, das die Stücke trennt
local plain = frame.args[4]; -- bewirkt buchstäbliche Suche
if plain then
ausgabe = mw.text.split(text, pattern, plain)
else
ausgabe = mw.text.split(text, pattern)
end
if pos <= 0 then
return #ausgabe;
elseif pos <= #ausgabe then
return ausgabe[pos];
else
return "";
end
end
-- spaltet einen String auf und gibt die Position eines Stücks oder eine Vorgabe zurück
function Str.splitpos(frame)
local ausgabe;
local part = frame.args[1] or "" -- Stück, das gesucht werden soll
local text = frame.args[2] or "" -- Text, der gespalten werden soll
local pattern = frame.args[3] or "" -- Muster, das die Stücke trennt
local plain = frame.args[4] -- bewirkt buchstäbliche Suche
local default = frame.args['default'] or "" -- Vorgabe, die zurückgegeben werden kann
local index = math.floor(tonumber(frame.args['index'])) or 1; -- Wert, der das entsprechende Vorkommen bestimmt
if plain then
ausgabe = mw.text.split(text, pattern, plain)
else
ausgabe = mw.text.split(text, pattern)
end
local i = 0;
if index < 1 then
index = 1;
end
while index > 0 and i < #ausgabe do
i = i + 1;
if ausgabe[i] == part then
index = index - 1;
end
end
if index > 0 then
return default;
else
return i;
end
end
-- Prüft einen String darauf, ob die ersten 10 Zeichen ein gültiges ISO-Datum ergeben.
-- Der Test prüft auf ASCII-String, weshalb die einfachen Stringfunktionen ausreichen
function Str.CheckIsoDate(frame)
local text = frame.args[1] or "";
text = string.sub(text,1,10)
if string.len(text) < 10 then
return ""
end
local yyyy = tonumber(string.sub(text,1,4)) or "?";
local mm = tonumber(string.sub(text,6,7)) or "?";
local dd = tonumber(string.sub(text,9,10)) or "?";
if yyyy == "?" or mm == "?" or dd == "?" then
return ""
end
if mm < 1 or mm > 12 or dd < 1 then
return ""
end
if ((mm == 1 or mm==3 or mm==5 or mm==7 or mm==8 or mm==10 or mm==12) and dd > 31)
or ((mm == 4 or mm==6 or mm==9 or mm==11) and dd > 30 )
or (mm == 2 and dd > 29)
or (mm == 2 and yyyy % 4 ~= 0 and dd > 28) then
return ""
end
if mm == 2 and yyyy > 1583
and (yyyy % 400 == 100 or yyyy % 400 == 200 or yyyy % 400 == 300) and dd > 28 then
return ""
end
return "1"
end
return Str
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.