CreateText & splitString improvements

Improved the createText and splitString functions.
This commit is contained in:
Robert Jelic
2022-12-18 00:31:52 +01:00
committed by GitHub
parent 5499057e7e
commit 923b570d48

View File

@@ -1,14 +1,19 @@
local sub = string.sub local sub,find = string.sub,string.find
local splitString = function(str, sep) local function splitString(str, delimiter)
if sep == nil then local result = {}
sep = "%s" if str == "" or delimiter == "" then
return result
end
local start = 1
local delim_start, delim_end = find(str, delimiter, start)
while delim_start do
table.insert(result, sub(str, start, delim_start - 1))
start = delim_end + 1
delim_start, delim_end = find(str, delimiter, start)
end end
local t={} table.insert(result, sub(str, start))
for v in string.gmatch(str, "([^"..sep.."]+)") do return result
table.insert(t, v)
end
return t
end end
local relations = {[0] = {8, 4, 3, 6, 5}, {4, 14, 8, 7}, {6, 10, 8, 7}, {9, 11, 8, 0}, {1, 14, 8, 0}, {13, 12, 8, 0}, {2, 10, 8, 0}, {15, 8, 10, 11, 12, 14}, local relations = {[0] = {8, 4, 3, 6, 5}, {4, 14, 8, 7}, {6, 10, 8, 7}, {9, 11, 8, 0}, {1, 14, 8, 0}, {13, 12, 8, 0}, {2, 10, 8, 0}, {15, 8, 10, 11, 12, 14},
@@ -81,7 +86,7 @@ end
return { return {
getTextHorizontalAlign = function(text, width, textAlign, replaceChar) getTextHorizontalAlign = function(text, width, textAlign, replaceChar)
text = string.sub(text, 1, width) text = sub(text, 1, width)
local offset = width - string.len(text) local offset = width - string.len(text)
if (textAlign == "right") then if (textAlign == "right") then
text = string.rep(replaceChar or " ", offset) .. text text = string.rep(replaceChar or " ", offset) .. text
@@ -132,22 +137,16 @@ splitString = splitString,
createText = function(str, width) createText = function(str, width)
local uniqueLines = splitString(str, "\n") local uniqueLines = splitString(str, "\n")
local lines = {} local result = {}
for k,v in pairs(uniqueLines)do for k,v in pairs(uniqueLines)do
local line = "" if(#v==0)then table.insert(result, "") end
local words = splitString(v, " ") local start = 1
for a,b in pairs(words)do while start <= #v do
if(#line+#b <= width)then table.insert(result, sub(v, start, start + width - 1))
line = line=="" and b or line.." "..b start = start + width
if(a==#words)then table.insert(lines, line) end
else
table.insert(lines, line)
line = b:sub(1,width)
if(a==#words)then table.insert(lines, line) end
end
end end
end end
return lines return result
end, end,
getValueFromXML = function(name, tab) getValueFromXML = function(name, tab)
@@ -219,4 +218,4 @@ shrink = function(image, bgCol)
return results return results
end, end,
} }