adv_chat/unicode.lua

38 lines
1.1 KiB
Lua
Raw Normal View History

2019-10-31 13:54:15 -07:00
unicode_patterns={"\\u","U+"}
2019-07-13 11:03:35 -07:00
function parse_utf8_codepoints(message, pattern)
local last_index=0
2019-10-31 13:54:15 -07:00
local begin,index=string.find(message,pattern,0,true)
2019-07-13 11:03:35 -07:00
local rope={}
while index do
local number=""
2019-10-31 13:54:15 -07:00
local i=index
while i <= i+4 do
i=i+1
local char=string.upper(string.sub(message,i,i))
if char == "" or not ((char >= "0" and char <= "9") or (char >= "A" and char <= "F")) then
2019-07-13 11:03:35 -07:00
break
end
number=number..char
end
number=tonumber(number, 16)
2019-10-31 13:54:15 -07:00
if number then
2020-02-29 03:55:23 -08:00
local utf_8_char=modlib.text.utf8(number)
2019-10-31 13:54:15 -07:00
if utf_8_char then
table.insert(rope, message:sub(last_index, begin-1))
table.insert(rope, utf_8_char)
last_index=i
end
2019-07-13 11:03:35 -07:00
end
2019-10-31 13:54:15 -07:00
begin,index=string.find(message,pattern,index,true)
2019-07-13 11:03:35 -07:00
end
table.insert(rope, message:sub(last_index))
return table.concat(rope, "")
end
function parse_unicode(message)
for _, pattern in pairs(unicode_patterns) do
message=parse_utf8_codepoints(message, pattern)
end
return message
end