【功能】修改昵称

需求:全中文模式下,最多8个汉字;其它情况最多16个字节

此处引入C#中,中英文在不同文本格式下占用的空间大小

1.ASCII中,一个英文字母(不区分大小写),占1个字节;一个汉字占2个字节

2.UTF-8文件编码格式下,一个英文字母(不区分大小写),占1个字节;一个汉字占3个字节

3.Unicode文件编码格式下,一个英文字母(不区分大小写),占1个字节;一个汉字占1个字节

4.UTF-16文件编码格式下,一个英文字母(不区分大小写),占2个字节;一个汉字占2个字节

5.UTF-32文件编码格式下,一个英文字母(不区分大小写),占4个字节;一个汉字占4个字节

XLua层实现逻辑:

local MaxBytes = 16
local MaxChineseCount = 8

function AccountInfoUI:CheckInputName(str)
    local bytesCount = 0
    local chineseCount = 0
    local currMaxBytes = MaxBytes

    for uchar in string.gmatch(str, "[%z\1-\127\194-\244][\128-\191]*") do
        if #uchar > 1 then
            chineseCount = chineseCount + 1
        end
        bytesCount = bytesCount + #uchar
    end

    --全中文
    if chineseCount >= MaxChineseCount and (bytesCount % 3 == 0) then
        currMaxBytes = 24
    end

    local currBytesCount = string.len(str)
    if currBytesCount > currMaxBytes then
        if currBytesCount > currMaxBytes then
            str = string.sub(str, 1, currMaxBytes)
        end
    end
    print("result---------------- "..str)
end

你可能感兴趣的:(项目案例,unity)