Modul:String: Unterschied zwischen den Versionen

+ appendWithComma(s, a)
(p.maxWordLen(s, dlist) ustring)
Markierung: Zurückgesetzt
(+ appendWithComma(s, a))
 
(5 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 49: Zeile 49:
-- Removes all spaces at the start and at the end of a string
-- Removes all spaces at the start and at the end of a string
     return s:match("^%s*(.-)%s*$")
     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
end


Zeile 68: Zeile 81:
-- if list has no content, table will be empty
-- if list has no content, table will be empty
delimiter = delimiter or " "
delimiter = delimiter or " "
s = s .. delimiter
s = (s or "") .. delimiter
     local result = {}
     local result = {}
     local start = 1
     local start = 1
Zeile 107: Zeile 120:
end
end


function p.maxWordLen(s, dlist)
function p.appendWithComma(s, a)
-- returns the maximum word length of a string
if a ~= nil and a ~= "" then
dlist = dlist or " " -- delimiter chars, default " " (space only)
if s ~= "" then
local max_word_len = 0
s = s .. ", " .. a
s = p.strip(s)
else
s_len = mw.ustring.len(s)
s = a
if s_len > 0 then
end
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
end
return max_word_len
return s
end
end


return p
return p