Lua 處理 UTF-8 字串
Posted on Mon 17 October 2016 in note • Tagged with lua, utf8
最近要用 Lua 處理 UTF-8 字串,但是 Lua 到了 5.3 才開始支援 UTF-8。
UTF-8 字串長度及複製
我的需求很簡單,只要判斷字串長度及類似 strncpy
的字串複製。所以直接利用 Lua 5.3 手冊 提到的 charpattern 來進行字串的操作。
[\0-\x7F\xC2-\xF4][\x80-\xBF]*
utf8len
function utf8len(s)
_, len = string.gsub(s, '([\0-\x7F\xC2-\xF4][\x80-\xBF]*)', '%1')
return len …
Continue reading