Modul:String/Work
Die Dokumentation für dieses Modul kann unter Modul:String/Work/Doku erstellt werden
local p = {}
local com = require("Modul:Common")
function p.replace(s, old, new, count)
local result
-- Wenn count angegeben ist, wird string.gsub mit dem limitierten Ersetzungszähler verwendet
if count then
result = string.gsub(s, old, new, count)
else
-- Wenn kein count angegeben ist, wird string.gsub alle Vorkommen ersetzen
result = string.gsub(s, old, new)
end
return result
end
function p.split(s, delimiter)
-- splits string s into pieces with delimiter and returns table
-- it's the counterpart of list()
delimiter = delimiter or " "
local result = {}
local start = 1
local delim_start, delim_end = string.find(s, delimiter, start, true) -- true = plain find (keine Patterns)
while delim_start do
table.insert(result, string.sub(s, start, delim_start - 1))
start = delim_end + 1
delim_start, delim_end = string.find(s, delimiter, start, true)
end
table.insert(result, string.sub(s, start))
-- mw.logObject(result) -- Debugging only
return result
end
function p.split2(s, delimiter)
-- same as p.split(), but with emptiness-check
-- if empty, then an empty table would be returned
-- to do: merge with p.split()
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")")
local result = {}
if s ~= nil and s ~= "" then
result = p.split(s, delimiter)
end
-- mw.logObject(result)
return result
end
function p.strip(s)
-- Removes all spaces at the start and at the end of a string
return s:match("^%s*(.-)%s*$")
end
function p.lstrip(s)
-- Removes all spaces at the start of a string
return s:match("^%s*(.-)$")
end
function p.rstrip(s)
-- Removes all spaces at the end of a string
return s:gsub("%s+$", "")
end
function p.splitAndStrip(s, delimiter)
-- combination of p.split and p.strip
-- transforms (string)list to table with the list elements
-- spaces before and after the list elements will be removed (stripped)
-- if list element is empty, no entry will be added to table
-- if list has no content, table will be empty
delimiter = delimiter or " "
s = s .. delimiter
local result = {}
local start = 1
repeat
local delim_start, delim_end = string.find(s, delimiter, start, true) -- true = plain find (keine Patterns)
if delim_start ~= nil then
local element = p.strip(string.sub(s, start, delim_start - 1))
if element ~= "" then
table.insert(result, element)
end
start = delim_end + 1
end
until delim_start == nil
-- mw.logObject(result)
return result
end
function p.bracketSplitAndStrip(s)
local s_pre_bracket = ""
local s_main = ""
local s_post_bracket = ""
s = p.strip(s)
local s_len = string.len(s)
local first_opening_bracket = string.find(s, "%(")
local first_closing_bracket = string.find(s, "%)")
local last_opening_bracket = string.find(s, "%([^%(]*$")
local last_closing_bracket = string.find(s, "%)[^%)]*$")
mw.log( "first_opening_bracket=" .. (first_opening_bracket or "nil") ..
" first_closing_bracket=" .. (first_closing_bracket or "nil") ..
" last_opening_bracket=" .. (last_opening_bracket or "nil") ..
" last_closing_bracket=" .. (last_closing_bracket or "nil"))
if first_opening_bracket == nil or first_closing_bracket == nil then -- Klammer-auf/zu-Pärchen nicht vorhanden
s_main = s -- => keine Klammer-Abtrennung
elseif first_opening_bracket == last_opening_bracket then -- nur ein Klammer-auf/zu-Pärchen vorhanden
if first_opening_bracket ~= 1 and first_closing_bracket ~= s_len then -- Klammern mitten im String
s_main = s -- => keine Klammer-Abtrennung
elseif first_opening_bracket == 1 and first_closing_bracket == s_len then -- Klammerung umfasst ganzen String
s_pre_bracket = s -- => Klammer-String ohne Haupt-String
elseif first_opening_bracket == 1 then -- Klammerung vorne
s_pre_bracket = string.sub(s, first_opening_bracket, first_closing_bracket) -- => Klammer-String mit Haupt-String
s_main = p.strip(string.sub(s, first_closing_bracket+1, s_len))
else -- ergo Klammerung hinten
s_post_bracket = string.sub(s, first_opening_bracket, first_closing_bracket) -- => Klammer-String mit Haupt-String
s_main = p.strip(string.sub(s, 1, first_opening_bracket-1))
end
else
end
-- else
mw.log( "s_pre_bracket=" .. s_pre_bracket ..
" s_main=" .. s_main ..
" s_post_bracket=" .. s_post_bracket)
return s_pre_bracket, s_main, s_post_bracket
end
return p