Lua:字符类型
我需要一个函数
function getCharType(c)
local i = string.byte(c) -- 仅适用于 1 字节字符
if (i > 48) and (i < 57) then return 1 end
if (i > 97) and (i < 122) then return 2 end
return 0
end
它应该返回
2 - 如果c是一个字母
1 - 如果c是一个数字
0 - 如果c是一个符号(其他任何符号)
c本身将已经是小写字符:charType = getCharType(string.lower(Character))。如果可能有Unicode字符,那就太好了。
使用以上内容,getCharType("ö")为0。
为了确定非ASCII字符是大写字母、小写字母或数字,您需要Unicode数据。Module:Unicode data在维基百科上有一个函数,它使用了Module:Unicode data/category(Unicode字符的通用类别数据)。
以下是从Module:Unicode data中lookup_category函数的改编。我没有包括Unicode数据(Module:Unicode data/category),您需要从上面的链接中复制它。
local category_data -- 将此变量设置为Module:Unicode data/category中的表格
local floor = math.floor
local function binary_range_search(code_point, ranges)
local low, mid, high
low, high = 1, #ranges
while low <= high do
mid = floor((low + high) / 2)
local range = ranges[mid]
if code_point < range[1] then
high = mid - 1
elseif code_point <= range[2] then
return range
else
low = mid + 1
end
end
return nil
end
function get_category(code_point)
if category_data.singles[code_point] then
return category_data.singles[code_point]
else
local range = binary_range_search(code_point, category_data.ranges)
return range and range[3] or "Cn"
end
end
函数get_category接受一个代码点(数字)并返回通用类别的名称。我猜您感兴趣的类别是Nd(数字、十进制数字)和以L开头的类别(字母)。
您需要一个函数将字符转换为代码点。如果文件使用UTF-8编码并且您使用Lua 5.3,则可以使用utf8.codepoint函数:get_category(utf8.codepoint('ö'))将得到'Ll'。您可以将类别代码转换为上面函数使用的数字值:function category_to_number(category) if category == "Nd" then return 1 elseif category:sub(1, 1) == "L" then return 2 else return 0 end end。
- Lua 虚拟机加密load(string.dump(function)) 后执行失败问题如何解决
- 我想创建一个 Nginx 规则,禁止访问
- 如何将两个不同的lua文件合成一个 东西有点长 大佬请耐心看完 我是小白研究几天了都没搞定
- 如何在roblox studio中1:1导入真实世界的地形?
- 求解,lua_resume的第二次调用继续执行协程问题。
- 【上海普陀区】内向猫网络招募【Skynet游戏框架Lua后端程序员】
- SF爱好求教:如何用lua实现游戏内调用数据库函数实现账号密码注册?
- Lua实现网站后台开发
- LUA错误显式返回,社区常见的规约是怎么样的
- lua5.3下载库失败
- 请问如何实现文本框内容和某个网页搜索框内容连接,并把网页输出来的结果反馈到另外一个文本框上
- lua lanes多线程使用
- 一个kv数据库
- openresty 有没有比较轻量的 docker 镜像
- 想问一下,有大佬用过luacurl吗
- 在Lua执行过程中使用Load函数出现问题
- 为什么 neovim 里没有显示一些特殊字符?
- Lua比较两个表的值(不考虑键的顺序)
- 有个lua简单的项目,外包,有意者加微信 liuheng600456详谈,最好在成都
- 如何在 Visual Studio 2022 中运行 Lua 代码?

function getCharType(c) return #c:rep(3):match(".%w?%a?")-1 end