String functions len, sub, gsub, find replaced with utf8 versions

This commit is contained in:
João Lopes 2020-04-28 17:55:30 +01:00
parent d34874e892
commit cde31673a1
7 changed files with 75 additions and 74 deletions

View File

@ -1,6 +1,7 @@
================================================
Version 11.3 - Alpha (Apr 28 - 2020)
================================================
[CHANGED] String functions len, sub, gsub, find replaced with utf8 versions
[ADDED] New properties: skin.controls.smallfont and skin.controls.imagebuttonfont
================================================

View File

@ -61,7 +61,7 @@ loveframes.collisions = {}
local dir = loveframes.config["DIRECTORY"] or path
-- replace all "." with "/" in the directory setting
dir = dir:gsub("\\", "/"):gsub("(%a)%.(%a)", "%1/%2")
dir = loveframes.utf8.gsub(loveframes.utf8.gsub(dir, "\\", "/"), "(%a)%.(%a)", "%1/%2")
loveframes.config["DIRECTORY"] = dir
-- enable key repeat

View File

@ -172,7 +172,7 @@ function loveframes.GetDirectoryContents(dir, t)
table.insert(t, {
path = dir,
fullpath = dir.. "/" ..v,
requirepath = dir:gsub("/", ".") .. "." ..name,
requirepath = loveframes.utf8.gsub(dir, "/", ".") .. "." ..name,
name = name,
extension = extension
})
@ -213,7 +213,7 @@ function loveframes.SplitString(str, pat)
if pat == " " then
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
local s, e, cap = loveframes.utf8.find(str, fpat, 1)
while s do
if s ~= #str then
cap = cap .. " "
@ -222,25 +222,25 @@ function loveframes.SplitString(str, pat)
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
s, e, cap = loveframes.utf8.find(str, fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
cap = loveframes.utf8.sub(str, last_end)
table.insert(t, cap)
end
else
local fpat = "(.-)" .. pat
local last_end = 1
local s, e, cap = str:find(fpat, 1)
local s, e, cap = loveframes.utf8.find(str, fpat, 1)
while s do
if s ~= 1 or cap ~= "" then
table.insert(t,cap)
end
last_end = e+1
s, e, cap = str:find(fpat, last_end)
s, e, cap = loveframes.utf8.find(str, fpat, last_end)
end
if last_end <= #str then
cap = str:sub(last_end)
cap = loveframes.utf8.sub(str, last_end)
table.insert(t, cap)
end
end

View File

@ -253,10 +253,10 @@ function newobject:SetText(t)
})
elseif dtype == "string" then
if self.ignorenewlines then
v = v:gsub("\n", " ")
v = loveframes.utf8.gsub(v, "\n", " ")
end
v = v:gsub(string.char(9), " ")
v = v:gsub("\n", " \n ")
v = loveframes.utf8.gsub(v, string.char(9), " ")
v = loveframes.utf8.gsub(v, "\n", " \n ")
local parts = loveframes.SplitString(v, " ")
for i, j in ipairs(parts) do
table.insert(self.formattedtext, {
@ -280,10 +280,10 @@ function newobject:SetText(t)
local key = k
if width > maxw then
table.remove(self.formattedtext, k)
for n=1, string.len(data) do
local item = data:sub(n, n)
for n=1, loveframes.utf8.len(data) do
local item = loveframes.utf8.sub(data, n, n)
local itemw = v.font:getWidth(item)
if n ~= string.len(data) then
if n ~= loveframes.utf8.len(data) then
if (curw + itemw) > maxw then
table.insert(inserts, {
key = key,
@ -353,7 +353,7 @@ function newobject:SetText(t)
local text = v.text
local color = v.color
if detectlinks then
if string.len(text) > 7 and (text:sub(1, 7) == "http://" or text:sub(1, 8) == "https://") then
if loveframes.utf8.len(text) > 7 and (loveframes.utf8.sub(text, 1, 7) == "http://" or loveframes.utf8.sub(text, 1, 8) == "https://") then
v.link = true
end
end

View File

@ -154,7 +154,7 @@ function newobject:update(dt)
for k, v in ipairs(lines) do
local linewidth = 0
if masked then
linewidth = font:getWidth(v:gsub(".", maskchar))
linewidth = font:getWidth(loveframes.utf8.gsub(v, ".", maskchar))
else
linewidth = font:getWidth(v)
end
@ -537,8 +537,8 @@ end
--]]---------------------------------------------------------
function newobject:textinput(text)
if text:find("kp.") then
text = text:gsub("kp", "")
if loveframes.utf8.find(text, "kp.") then
text = loveframes.utf8.gsub(text, "kp", "")
end
self:RunKey(text, true)
@ -588,7 +588,7 @@ function newobject:RunKey(key, istext)
self:MoveIndicator(-1)
local indicatorx = self.indicatorx
if indicatorx <= x and indicatornum ~= 0 then
local width = font:getWidth(text:sub(indicatornum, indicatornum + 1))
local width = font:getWidth(loveframes.utf8.sub(text, indicatornum, indicatornum + 1))
self.offsetx = offsetx - width
elseif indicatornum == 0 and offsetx ~= 0 then
self.offsetx = 0
@ -616,7 +616,7 @@ function newobject:RunKey(key, istext)
self:MoveIndicator(1)
local indicatorx = self.indicatorx
if indicatorx >= (x + swidth) and indicatornum ~= utf8.len(text) then
local width = font:getWidth(text:sub(indicatornum, indicatornum))
local width = font:getWidth(loveframes.utf8.sub(text, indicatornum, indicatornum))
self.offsetx = offsetx + width
elseif indicatornum == utf8.len(text) and offsetx ~= ((font:getWidth(text)) - swidth + 10) and font:getWidth(text) + textoffsetx > swidth then
self.offsetx = ((font:getWidth(text)) - swidth + 10)
@ -695,7 +695,7 @@ function newobject:RunKey(key, istext)
local cwidth = 0
if masked then
local maskchar = self.maskchar
cwidth = font:getWidth(text:gsub(".", maskchar))
cwidth = font:getWidth(loveframes.utf8.gsub(text, ".", maskchar))
else
cwidth = font:getWidth(text)
end
@ -747,8 +747,8 @@ function newobject:RunKey(key, istext)
newtext = self.lines[line]
self.lines[line] = ""
elseif indicatornum > 0 and indicatornum < utf8.len(self.lines[line]) then
newtext = self.lines[line]:sub(indicatornum + 1, utf8.len(self.lines[line]))
self.lines[line] = self.lines[line]:sub(1, indicatornum)
newtext = loveframes.utf8.sub(self.lines[line], indicatornum + 1, utf8.len(self.lines[line]))
self.lines[line] = loveframes.utf8.sub(self.lines[line], 1, indicatornum)
end
if line ~= #lines then
table.insert(self.lines, line + 1, newtext)
@ -834,8 +834,8 @@ function newobject:RunKey(key, istext)
local cwidth = 0
if masked then
local maskchar = self.maskchar
twidth = font:getWidth(text:gsub(".", maskchar))
cwidth = font:getWidth(key:gsub(".", maskchar))
twidth = font:getWidth(loveframes.utf8.gsub(text, ".", maskchar))
cwidth = font:getWidth(loveframes.utf8.gsub(key, ".", maskchar))
else
twidth = font:getWidth(text)
cwidth = font:getWidth(key)
@ -937,7 +937,7 @@ function newobject:UpdateIndicator()
elseif indicatornum >= utf8.len(text) then
width = font:getWidth(text)
else
width = font:getWidth(text:sub(1, utf8.offset (text, indicatornum + 1) - 1))
width = font:getWidth(loveframes.utf8.sub(text, 1, indicatornum))
end
end
@ -966,7 +966,7 @@ function newobject:UpdateIndicator()
for k, v in ipairs(lines) do
local linewidth = 0
if self.masked then
linewidth = font:getWidth(v:gsub(".", self.maskchar))
linewidth = font:getWidth(loveframes.utf8.gsub(v, ".", self.maskchar))
else
linewidth = font:getWidth(v)
end
@ -1013,8 +1013,8 @@ function newobject:AddIntoText(t, p)
local line = self.line
local curline = lines[line]
local text = curline
local part1 = text:sub(1, utf8.offset(text, p + 1) - 1)
local part2 = text:sub(utf8.offset(text, p + 1))
local part1 = loveframes.utf8.sub(text, 1, p)
local part2 = loveframes.utf8.sub(text, p + 1)
local new = part1 .. t .. part2
return new
@ -1032,8 +1032,8 @@ function newobject:RemoveFromText(p)
local line = self.line
local curline = lines[line]
local text = curline
local part1 = text:sub(1, utf8.offset(text, p) - 1)
local part2 = text:sub(utf8.offset(text, p + 1))
local part1 = loveframes.utf8.sub(text, 1, p - 1)
local part2 = loveframes.utf8.sub(text, p + 1)
local new = part1 .. part2
return new
@ -1087,7 +1087,7 @@ function newobject:GetTextCollisions(x, y)
local line = self.line
local curline = lines[line]
for i=1, utf8.len(curline) do
local char = text:sub(i, i)
local char = loveframes.utf8.sub(text, i, i)
local width = 0
if masked then
local maskchar = self.maskchar
@ -1365,11 +1365,11 @@ function newobject:SetText(text)
local multiline = self.multiline
text = tostring(text)
text = text:gsub(string.char(9), tabreplacement)
text = text:gsub(string.char(13), "")
text = loveframes.utf8.gsub(text, string.char(9), tabreplacement)
text = loveframes.utf8.gsub(text, string.char(13), "")
if multiline then
text = text:gsub(string.char(92) .. string.char(110), string.char(10))
text = loveframes.utf8.gsub(text, string.char(92) .. string.char(110), string.char(10))
local t = loveframes.SplitString(text, string.char(10))
if #t > 0 then
self.lines = t
@ -1379,8 +1379,8 @@ function newobject:SetText(text)
self.line = #self.lines
self.indicatornum = utf8.len(self.lines[#self.lines])
else
text = text:gsub(string.char(92) .. string.char(110), "")
text = text:gsub(string.char(10), "")
text = loveframes.utf8.gsub(text, string.char(92) .. string.char(110), "")
text = loveframes.utf8.gsub(text, string.char(10), "")
self.lines = {text}
self.line = 1
self.indicatornum = utf8.len(text)
@ -1899,13 +1899,13 @@ function newobject:Paste()
if limit > 0 then
local curtext = self:GetText()
local curlength = curtext:len()
local curlength = utf8.len(curtext)
if curlength == limit then
return
else
local inputlimit = limit - curlength
if text:len() > inputlimit then
text = text:sub(1, inputlimit)
if utf8.len(text) > inputlimit then
text = loveframes.utf8.sub(text, 1, inputlimit)
end
end
end
@ -1921,7 +1921,7 @@ function newobject:Paste()
end
end
if #usable > 0 or #unusable > 0 then
text = text:gsub(".", charcheck)
text = loveframes.utf8.gsub(text, ".", charcheck)
end
if alltextselected then
self:SetText(text)
@ -1939,17 +1939,17 @@ function newobject:Paste()
local numparts = #parts
local oldlinedata = {}
local line = self.line
local first = lines[line]:sub(0, indicatornum)
local last = lines[line]:sub(indicatornum + 1)
local first = loveframes.utf8.sub(lines[line], 0, indicatornum)
local last = loveframes.utf8.sub(lines[line], indicatornum + 1)
if numparts > 1 then
for i=1, numparts do
local part = parts[i]:gsub(string.char(13), "")
part = part:gsub(string.char(9), " ")
local part = loveframes.utf8.gsub(parts[i], string.char(13), "")
part = loveframes.utf8.gsub(part, string.char(9), " ")
if i ~= 1 then
table.insert(oldlinedata, lines[line])
lines[line] = part
if i == numparts then
self.indicatornum = part:len()
self.indicatornum = utf8.len(part)
lines[line] = lines[line] .. last
self.line = line
end
@ -1966,10 +1966,10 @@ function newobject:Paste()
ontextchanged(self, text)
end
elseif numparts == 1 then
text = text:gsub(string.char(10), " ")
text = text:gsub(string.char(13), " ")
text = text:gsub(string.char(9), tabreplacement)
local length = text:len()
text = loveframes.utf8.gsub(text, string.char(10), " ")
text = loveframes.utf8.gsub(text, string.char(13), " ")
text = loveframes.utf8.gsub(text, string.char(9), tabreplacement)
local length = utf8.len(text)
local new = first .. text .. last
lines[line] = new
self.indicatornum = indicatornum + length
@ -1978,13 +1978,13 @@ function newobject:Paste()
end
end
else
text = text:gsub(string.char(10), " ")
text = text:gsub(string.char(13), " ")
text = text:gsub(string.char(9), tabreplacement)
local length = text:len()
text = loveframes.utf8.gsub(text, string.char(10), " ")
text = loveframes.utf8.gsub(text, string.char(13), " ")
text = loveframes.utf8.gsub(text, string.char(9), tabreplacement)
local length = utf8.len(text)
local linetext = lines[1]
local part1 = linetext:sub(1, indicatornum)
local part2 = linetext:sub(indicatornum + 1)
local part1 = loveframes.utf8.sub(linetext, 1, indicatornum)
local part2 = loveframes.utf8.sub(linetext, indicatornum + 1)
local new = part1 .. text .. part2
lines[1] = new
self.indicatornum = indicatornum + length

View File

@ -165,7 +165,7 @@ local function ParseHeaderText(str, hx, hwidth, tx)
if (tx + twidth) - hwidth/2 > hx + hwidth then
if #str > 1 then
return ParseHeaderText(str:sub(1, #str - 1), hx, hwidth, tx, twidth)
return ParseHeaderText(loveframes.utf8.sub(str, 1, #str - 1), hx, hwidth, tx, twidth)
else
return str
end
@ -181,7 +181,7 @@ local function ParseRowText(str, rx, rwidth, tx1, tx2)
if (tx1 + tx2) + twidth > rx + rwidth then
if #str > 1 then
return ParseRowText(str:sub(1, #str - 1), rx, rwidth, tx1, tx2)
return ParseRowText(loveframes.utf8.sub(str, 1, #str - 1), rx, rwidth, tx1, tx2)
else
return str
end
@ -405,8 +405,8 @@ function skin.button(object)
local text = object.text
local font = skin.controls.button_text_font
while font:getWidth(text) > width - object.image:getWidth() - 10 do
text =text:sub(2)
while text:byte(1, 1) > 127 do text = text:sub(2) end
text =loveframes.utf8.sub(text, 2)
while text:byte(1, 1) > 127 do text = loveframes.utf8.sub(text, 2) end
end
skin.PrintText(text, x + object.image:getWidth() + 4, y + height/2 - theight/2)
else
@ -429,8 +429,8 @@ function skin.button(object)
local text = object.text
local font = skin.controls.button_text_font
while font:getWidth(text) > width - object.image:getWidth() - 10 do
text =text:sub(2)
while text:byte(1, 1) > 127 do text = text:sub(2) end
text =loveframes.utf8.sub(text, 2)
while text:byte(1, 1) > 127 do text = loveframes.utf8.sub(text, 2) end
end
skin.PrintText(text, x + object.image:getWidth() + 4, y + height/2 - theight/2)
else
@ -453,8 +453,8 @@ function skin.button(object)
local text = object.text
local font = skin.controls.button_text_font
while font:getWidth(text) > width - object.image:getWidth() - 10 do
text =text:sub(2)
while text:byte(1, 1) > 127 do text = text:sub(2) end
text =loveframes.utf8.sub(text, 2)
while text:byte(1, 1) > 127 do text = loveframes.utf8.sub(text, 2) end
end
skin.PrintText(text, x + object.image:getWidth() + 4, y + height/2 - theight/2)
else
@ -1170,7 +1170,7 @@ function skin.textinput(object)
for i=1, #lines do
local str = lines[i]
if masked then
str = str:gsub(".", "*")
str = loveframes.utf8.gsub(str, ".", "*")
end
local twidth = font:getWidth(str)
if twidth == 0 then
@ -1184,7 +1184,7 @@ function skin.textinput(object)
local twidth = 0
if masked then
local maskchar = object:GetMaskChar()
twidth = font:getWidth(text:gsub(".", maskchar))
twidth = font:getWidth(loveframes.utf8.gsub(text, ".", maskchar))
else
twidth = font:getWidth(text)
end
@ -1264,7 +1264,7 @@ function skin.textinput(object)
str = lines[i]
if masked then
local maskchar = object:GetMaskChar()
str = str:gsub(".", maskchar)
str = loveframes.utf8.gsub(str, ".", maskchar)
end
skin.PrintText(#str > 0 and str or (#lines == 1 and placeholder or ""), textx, texty + theight * i - theight)
end
@ -1272,7 +1272,7 @@ function skin.textinput(object)
str = lines[1]
if masked then
local maskchar = object:GetMaskChar()
str = str:gsub(".", maskchar)
str = loveframes.utf8.gsub(str, ".", maskchar)
end
skin.PrintText(#str > 0 and str or placeholder, textx, texty)
end

View File

@ -45,7 +45,7 @@ local function ParseHeaderText(str, hx, hwidth, tx)
if (tx + twidth) - hwidth/2 > hx + hwidth then
if #str > 1 then
return ParseHeaderText(str:sub(1, #str - 1), hx, hwidth, tx, twidth)
return ParseHeaderText(loveframes.utf8.sub(str, 1, #str - 1), hx, hwidth, tx, twidth)
else
return str
end
@ -61,7 +61,7 @@ local function ParseRowText(str, rx, rwidth, tx1, tx2)
if (tx1 + tx2) + twidth > rx + rwidth then
if #str > 1 then
return ParseRowText(str:sub(1, #str - 1), rx, rwidth, tx1, tx2)
return ParseRowText(loveframes.utf8.sub(str, 1, #str - 1), rx, rwidth, tx1, tx2)
else
return str
end
@ -1059,7 +1059,7 @@ function skin.textinput(object)
for i=1, #lines do
local str = lines[i]
if masked then
str = str:gsub(".", "*")
str = loveframes.utf8.gsub(str, ".", "*")
end
local twidth = font:getWidth(str)
if twidth == 0 then
@ -1073,7 +1073,7 @@ function skin.textinput(object)
local twidth = 0
if masked then
local maskchar = object:GetMaskChar()
twidth = font:getWidth(text:gsub(".", maskchar))
twidth = font:getWidth(loveframes.utf8.gsub(text, ".", maskchar))
else
twidth = font:getWidth(text)
end
@ -1153,7 +1153,7 @@ function skin.textinput(object)
str = lines[i]
if masked then
local maskchar = object:GetMaskChar()
str = str:gsub(".", maskchar)
str = loveframes.utf8.gsub(str, ".", maskchar)
end
skin.PrintText(#str > 0 and str or (#lines == 1 and placeholder or ""), textx, texty + theight * i - theight)
end
@ -1161,7 +1161,7 @@ function skin.textinput(object)
str = lines[1]
if masked then
local maskchar = object:GetMaskChar()
str = str:gsub(".", maskchar)
str = loveframes.utf8.gsub(str, ".", maskchar)
end
skin.PrintText(#str > 0 and str or placeholder, textx, texty)
end