Modul:String: Unterschied zwischen den Versionen

Aus FürthWiki

(fix c.split2())
(+ vonBisText())
 
(21 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
local p = {}
local p = {}
local c = require("Module:Common")
local com = require("Modul:Common")


function p.replace(s, old, new, count)
function p.replace(s, old, new, count)
Zeile 35: Zeile 35:
function p.split2(s, delimiter)
function p.split2(s, delimiter)
-- same as p.split(), but with emptiness-check
-- same as p.split(), but with emptiness-check
-- if empty, then an empty table would be returned
-- to do: merge with p.split()
-- to do: merge with p.split()
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")")
-- mw.log("split2(" .. (s or "nil") .. ", " .. (delimiter or "nil") .. ")")
     if not c.isEmpty(s) then
     local result = {}
delimiter = delimiter or " "
    if s ~= nil and s ~= "" then
    local start = 1
result = p.split(s, delimiter)
    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))
     end
     end
-- mw.logObject(result)
-- mw.logObject(result)
return result
return result
end
end


function p.list(table, separator)
function p.strip(s)
-- converts a keyed table into a list with delimiters
-- Removes all spaces at the start and at the end of a string
-- the indexes/keys gonna lost and the list may be unsorted
    return s:match("^%s*(.-)%s*$")
-- it's the counterpart of split()
end
local list = ""
 
if not c.isEmpty(table) then
function p.nilStrip(s)
if type(table) == "table" then
-- same as p.strip(s), but removes nil, if s is empty or nil
separator = separator or " "
-- useful to handle missing/empty args
for _, v in pairs(table) do
if s == nil then
if #list ~= 0 then
return
list = list .. separator
end
end
s = p.strip(s)
  list = list .. v
if s == "" then
end
return
else
list = table
end
end
end
    return 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 or "") .. 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


-- mw.log(list) -- Debugging only
function p.bracketSplitAndStrip(s)
return list
-- 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


function p.ilist(table, separator)
function p.appendWithSeparator(s, sep, a)
-- converts a indexed table into a list with delimiters
if a ~= nil and a ~= "" then
-- the indexes gonna lost
if s ~= "" then
-- it's the counterpart of split()
s = s .. sep .. a
local list = ""
if not c.isEmpty(table) then
if type(table) == "table" then
separator = separator or " "
for _, v in ipairs(table) do
if #list ~= 0 then
list = list .. separator
end
  list = list .. v
end
else
else
list = table
s = a
end
end
end
end
return s
end


-- mw.log(list) -- Debugging only
function p.appendWithComma(s, a)
return list
return p.appendWithSeparator(s, ", ", a)
end
end


function p.strip(s)
function p.anfangsbuchstabeGross(s)
-- Removes all spaces at the start and at the end of a string
return string.upper(string.sub(s, 1, 1)) .. string.sub(s, 2)
    return s:match("^%s*(.-)%s*$")
end
end


function p.lstrip(s)
function p.anfangsbuchstabeKlein(s)
-- Removes all spaces at the start of a string
return string.lower(string.sub(s, 1, 1)) .. string.sub(s, 2)
return s:match("^%s*(.-)$")
end
end


function p.rstrip(s)
function p.vonBisText(von, bis)
-- Removes all spaces at the end of a string
local t
return s:gsub("%s+$", "")
von = von or ""
bis = bis or ""
if von ~= "" then
if bis ~= "" then
t = von .. " - " .. bis
else
t = "seit " .. von
end
elseif bis ~= "" then
t = "bis " .. bis
end
return t
end
end


return p
return p

Aktuelle Version vom 19. Februar 2026, 17:06 Uhr

Unterseiten

Siehe auch


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.nilStrip(s)
	-- same as p.strip(s), but removes nil, if s is empty or nil
	-- useful to handle missing/empty args
	if s == nil then
		return
	end
	s = p.strip(s)
	if s == "" then
		return
	end
    return 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 or "") .. 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)
	-- 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

function p.appendWithSeparator(s, sep, a)
	if a ~= nil and a ~= "" then
		if s ~= "" then
			s = s .. sep .. a
		else
			s = a
		end
	end
	return s
end

function p.appendWithComma(s, a)
	return p.appendWithSeparator(s, ", ", a)
end

function p.anfangsbuchstabeGross(s)
	return string.upper(string.sub(s, 1, 1)) .. string.sub(s, 2)
end

function p.anfangsbuchstabeKlein(s)
	return string.lower(string.sub(s, 1, 1)) .. string.sub(s, 2)
end

function p.vonBisText(von, bis)
	local t
	von = von or ""
	bis = bis or ""
	if von ~= "" then
		if bis ~= "" then
			t = von .. " - " .. bis
		else
			t = "seit " .. von
		end
	elseif bis ~= "" then
		t = "bis " .. bis
	end
	return t
end

return p