lua - 把字符分割并分别打上Unicode长度标记
2018-04-10 03:47:58 -0400

function stringToChars(str)
    local list = {}
    local len = string.len(str)
    local i = 1
    while i <= len do
        local c = string.byte(str, i)
        local shift = 1
        if c > 0 and c <= 127 then
            shift = 1
        elseif (c >= 192 and c <= 223) then
            shift = 2
        elseif (c >= 224 and c <= 239) then
            shift = 3
        elseif (c >= 240 and c <= 247) then
            shift = 4
        end
        local char = string.sub(str, i, i + shift - 1)
        i = i + shift
        table.insert(list, {char = char, shift = shift})
    end
    return list
end
«Newer      Older»
Comment:
Name:
Back to home

Subscribe | Register | Login | N