InputText: fix search on multi-bytes UTF-8 (#7981)

Also fix crashes when text is invalid UTF-8.
This commit is contained in:
hius07
2021-07-15 22:57:12 +03:00
committed by GitHub
parent 9d2c6b07aa
commit 41f2a6f8a4

View File

@@ -673,19 +673,19 @@ end
-- if start_pos not set, starts a search from the next to cursor position
-- returns first found position or 0 if not found
function InputText:searchString(str, case_sensitive, start_pos)
local str_len = string.len(str)
local str_charlist = util.splitToChars(str)
local str_len = #str_charlist
local char_pos, found = 0, 0
start_pos = start_pos and (start_pos - 1) or self.charpos
if not case_sensitive then
str = Utf8Proc.lowercase(str)
end
for i = start_pos, #self.charlist - str_len do
for j = 1, str_len do
local char_txt = self.charlist[i + j]
local char_str = str_charlist[j]
if not case_sensitive then
char_txt = Utf8Proc.lowercase(char_txt)
char_txt = Utf8Proc.lowercase(util.fixUtf8(char_txt, "?"))
char_str = Utf8Proc.lowercase(util.fixUtf8(char_str, "?"))
end
if char_txt ~= string.sub(str, j, j) then
if char_txt ~= char_str then
found = 0
break
end