Modul:String: Unterschied zwischen den Versionen

Aus FürthWiki

(p.maxWordLen(s, dlist) ustring)
Markierung: Zurückgesetzt
(- maxWordLen() obsolete)
Markierung: Manuelle Zurücksetzung
Zeile 105: Zeile 105:
end
end
return s_pre_bracket, s, s_post_bracket
return s_pre_bracket, s, s_post_bracket
end
function p.maxWordLen(s, dlist)
-- returns the maximum word length of a string
dlist = dlist or " " -- delimiter chars, default " " (space only)
local max_word_len = 0
s = p.strip(s)
s_len = mw.ustring.len(s)
if s_len > 0 then
local start, word_len = 1
repeat
local delimiter = 0
for i = start, s_len do
local c = mw.ustring.sub(s, i, i)
for j = 1, #dlist do
if c == mw.ustring.sub(dlist, j, j) then
delimiter = i
break
end
end
if delimiter ~= 0 then
break
end
end
if delimiter == 0 then
word_len = s_len - start + 1
else
word_len = delimiter - start
start = delimiter + 1
end
if word_len > max_word_len then
max_word_len = word_len
end
until delimiter == 0
end
return max_word_len
end
end


return p
return p

Version vom 25. November 2025, 15:40 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.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)
	-- 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

return p