【功能】修改昵称

需求:全中文模式下,最多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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值