Lua:角色的类型

Lua:角色的类型,第1张

概述我需要一个功能 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 0end 应该返回 我需要一个功能

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 0end

应该返回

2 - if c is a letter1 - if c is a digit0 - if c is  a symbol (anything else)

c本身已经是小写字符:charType = getCharType(string.lower(Character)).如果可以使用Unicode字符,那就没问题了.

上面的getCharType(“ö”)是0.

解决方法 要确定非ASCII字符是大写还是小写字母还是数字,您需要Unicode数据.维基百科上的 Module:Unicode data具有这样的函数,它使用 Module:Unicode data/category( General Category的Unicode字符的数据).

以下是Module:Unicode数据中lookup_category函数的改编.我没有包含Unicode数据(模块:Unicode数据/类别);你必须从上面的链接复制它.

local category_data -- set this variable to the text of Module:Unicode data/category abovelocal floor = math.floorlocal 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,mID        else            low = mID + 1        end    end    return nil,mIDendfunction 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"    endend

函数get_category采用代码点(数字)并返回常规类别的名称.我猜你感兴趣的类别是Nd(数字,十进制数字)和以L(字母)开头的类别.

您将需要一个将字符转换为代码点的函数.如果文件以UTF-8编码并且您使用的是Lua 5.3,则可以使用utf8.codepoint函数:get_category(utf8.codepoint(‘ö’))将导致’Ll’.您可以将类别代码转换为上述函数使用的数字值:函数category_to_number(类别)如果category ==“Nd”则返回1 elseif类别:sub(1,1)==“L”然后返回2否则返回0结束.

总结

以上是内存溢出为你收集整理的Lua:角色类型全部内容,希望文章能够帮你解决Lua:角色的类型所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1219381.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存