Modul:String: Unterschied zwischen den Versionen

splitAndStrip() nil handling
(+ splitAndStrip())
(splitAndStrip() nil handling)
 
(9 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
local p = {}
local p = {}
local com = require("Module:Common")
local com = require("Modul:Common")


function p.replace(s, old, new, count)
function p.replace(s, old, new, count)
Zeile 39: Zeile 39:
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")")
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")")
     local result = {}
     local result = {}
     if not com.isEmpty(s) then
     if s ~= nil and s ~= "" then
result = p.split(s, delimiter)
result = p.split(s, delimiter)
     end
     end
-- mw.logObject(result)
-- mw.logObject(result)
return result
return result
end
function p.list(table, separator)
-- converts a keyed table into a list with delimiters
-- the indexes/keys gonna lost and the list may be unsorted
-- it's the counterpart of p.split()
local list = ""
if not com.isEmpty(table) then
if type(table) == "table" then
separator = separator or " "
for _, v in pairs(table) do
if #list ~= 0 then
list = list .. separator
end
  list = list .. v
end
else
list = table
end
end
return list
end
end


Zeile 83: Zeile 62:


function p.splitAndStrip(s, delimiter)
function p.splitAndStrip(s, delimiter)
-- combination of p.split2 and p.strip
-- 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 or "") .. delimiter
     local result = {}
     local result = {}
     if not com.isEmpty(s) then
     local start = 1
local split = p.split2(s, delimiter)
    repeat
for _, v in ipairs(split) do
    local delim_start, delim_end = string.find(s, delimiter, start, true) -- true = plain find (keine Patterns)
v = p.strip(v)
    if delim_start ~= nil then
if v ~= "" then
    local element = p.strip(string.sub(s, start, delim_start - 1))
table.insert(result, v)
    if element ~= "" then
end
        table.insert(result, element)
end
        end
     end
        start = delim_end + 1
    end
     until delim_start == nil
-- mw.logObject(result)
-- mw.logObject(result)
return result
    return result
end
 
function p.bracketSplitAndStrip(s)
-- scheidet einen geklammerten String am Anfang und am Ende ab
-- Beispiel: "( a ) b (c)" ergibt "( a )", "b", "(c)"
local s_pre_bracket = ""
local s_post_bracket = ""
-- Klammerung am Anfang
local bracket_open = string.find(s, "%(")
local bracket_close = string.find(s, "%)")
if bracket_open == 1 and bracket_close ~= nil then
s_pre_bracket = string.sub(s, bracket_open, bracket_close)
s = p.strip(string.sub(s, bracket_close+1))
end
-- Klammerung am Ende
bracket_open = string.find(s, "%([^%(]*$")
bracket_close = string.find(s, "%)[^%)]*$")
if bracket_open ~= nil and bracket_close == string.len(s) then
s_post_bracket = string.sub(s, bracket_open, bracket_close)
s = p.strip(string.sub(s, 1, bracket_open-1))
end
return s_pre_bracket, s, s_post_bracket
end
end


return p
return p