-2

这是我用来注册库名称的函数名称注册闭包:

池对象:

function FooBarPool()
  local Names = {}
  local self = {}
    function self:Register(fFoo,sName)
      if(fFoo) then
        Names[fFoo] = sName
      end
    end
    function self:GetName(fFoo)
      return Names[fFoo] or "N/A"
    end
  return self
end

创建一个池对象

local Pool = FooBarPool()

注册已知的库函数

Pool:Register(string.sub,"string.sub")
Pool:Register(string.gsub,"string.gsub")
Pool:Register(string.match,"string.match")
Pool:Register(string.gmatch,"string.gmatch")
Pool:Register(string.find,"string.find")
Pool:Register(string.gfind,"string.gfind")
Pool:Register(string.format,"string.format")
Pool:Register(string.byte,"string.byte")
Pool:Register(string.char,"string.char")
Pool:Register(string.len,"string.len")
Pool:Register(string.lower,"string.lower")
Pool:Register(string.upper,"string.upper")
Pool:Register(string.rep,"string.rep")
Pool:Register(string.reverse,"string.reverse")

for k,v in pairs(string) do
  print(tostring(v) .. " : "..Pool:GetName(v))
end
4

2 回答 2

2

如果您添加print(k,v)到最后一个循环,您会看到您缺少string.dump.

功能string.gfind不标准。它存在于 Lua 5.0 中,但string.gmatch在 Lua 5.1 中被重命名为。

您可以使用以下命令获取string库中的所有名称

for k in pairs(string) do
    print(k)
end

或查看手册

于 2015-02-03T10:19:19.750 回答
0

为什么不在数组中列出字符串库字符串函数,然后使用pairs函数对其进行迭代,因此:strings = {"string.sub", "string.gsub", ..., "string.reverse"}; 对于 _k,v 成对(字符串)做 print(v) 结束;我发现这比上面给出的答案更容易。顺便说一句,是否有更好的方法来解压缩所有字符串库字符串函数,而无需将它们列在表中?

于 2015-04-01T14:08:15.837 回答