Lua实现 计算 UTF8 字符串的长度

来源:互联网 发布:js foreach return 编辑:程序博客网 时间:2024/06/08 06:33

因为项目需要, 用Lua实现了一个取utf8字符串长度的方法

--此处用空间换时间local gs_utf8_look_for_table ={    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,    2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,    3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,    4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 6, 6, 1, 1}function chSize(char)     if not char then        printInfo("not char")        return 0    else        return gs_utf8_look_for_table[char]    endend-- 计算utf8字符串字符数, 各种字符都按一个字符计算function utf8Len(str)    local len = 0    local currentIndex = 1    local count_ = #str    while currentIndex <= count_ do        local char = string.byte(str, currentIndex)        local currentCharIndex_ = chSize(char);         currentIndex = currentIndex + currentCharIndex_        len = len +1    end        return lenend

后来翻看quickcocos的functions.lua发现人家早就实现了(吐血三升)

--来自quickcocos的 functions.luafunction string.utf8len(input)    local len  = string.len(input)    local left = len    local cnt  = 0    local arr  = {0, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc}    while left ~= 0 do        local tmp = string.byte(input, -left)        local i   = #arr        while arr[i] do            if tmp >= arr[i] then                left = left - i                break            end            i = i - 1        end        cnt = cnt + 1    end    return cntend
2 0
原创粉丝点击