Lua:字符的类型
我需要一个函数Lua:字符的类型,lua,char,Lua,Char,我需要一个函数 function getCharType(c) local i = string.byte(c) -- works only for 1 byte chars if (i > 48) and (i < 57) then return 1 end if (i > 97) and (i < 122) then return 2 end return 0 end c本身已经是小写字符:charType=getCharType(string.lo
function getCharType(c)
local i = string.byte(c) -- works only for 1 byte chars
if (i > 48) and (i < 57) then return 1 end
if (i > 97) and (i < 122) then return 2 end
return 0
end
c本身已经是小写字符:charType=getCharType(string.lower(character))
。如果可以使用Unicode字符,那就好了
对于上述
getChartType(“ö”)
为0。仅适用于ASCII字符(非Unicode)
要确定非ASCII字符是大写字母、小写字母还是数字,需要Unicode数据。Wikipedia上有这样一个函数,它使用(Unicode字符类型的数据) 下面是对模块中的
lookup\u category
函数的改编:Unicode数据。我没有包括Unicode数据(模块:Unicode数据/类别);你必须从上面的链接复制它
local category_data -- set this variable to the table in Module:Unicode data/category above
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
localcategory\u data——将此变量设置为上面模块:Unicode data/category中的表
本地楼层=数学楼层
局部函数二进制搜索(代码点,范围)
本地低、中、高
低,高=1,#范围
当你情绪低落时,你曾试图用什么方法来解决这个问题?请提供您在试图解决问题的地方编写的任何代码。请参阅已编辑的问题…谢谢。是否有机会扩展代码,以便getCharType(“ö”)
也能工作?它现在返回0。对于没有符号但不在列表中的其他特殊字符也是如此ASCII@Herbert-如果c:byte()>=0x80,则为Unicode字母或Unicode符号
function getCharType(c)
return #c:rep(3):match(".%w?%a?")-1
end
local category_data -- set this variable to the table in Module:Unicode data/category above
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