挖掘 Lua 程式碼

lua-users home
wiki

以下為 Lua 原始碼內容分析 [1] 的範例…

最不受歡迎的字串函式為何?

-- usage: clone LuaDist repo [1] and then do lua findescape.lua `find /tmp/Repository -name '*.lua'`
-- [1] github.com/LuaDist/Repository
-- DavidManura
 
lexer = require 'lxsh.lexers.lua'  -- https://github.com/xolox/lua-lxsh

local function readfile(filename)
  local fh = io.open(filename, 'r')
  local text; if fh then text = fh:read'*a':gsub('\r','\n') end
  fh:close()
  return text
end

local counts = {}

for _, filename in ipairs{...} do
  --print(filename)
  local text = readfile(filename)

  for kind, text in lexer.gmatch(text) do
    if kind ~= 'comment' and kind ~= 'whitespace' and kind ~= "string" and kind ~= "number" then
      counts[text] = (counts[text] or 0) + 1
    end
  end
end

local names = {}
for name, count in pairs(counts) do names[#names+1] = {name, count} end
table.sort(names, function(a,b) return a[2] < b[2] end)
for _, pair in ipairs(names) do
  print(pair[1], pair[2])
end

lua test.lua `find /tmp/Repository/ -name '*.lua'`|grep '^string\.'
...
string.gfind	27
string.dump	35
string.lower	41
string.upper	41
string.gmatch	46
string.char	83
string.match	106
string.byte	139
string.rep	148
string.len	159
string.sub	332
string.find	400
string.gsub	492
string.format	722

string.reverse。

(注意:以上忽略字串方法呼叫。)

多少比例的原始檔誤用字串跳脫序列?

請參閱 LuaList:2010-10/msg00449.html

另請參閱


近期變更 · 喜好設定
編輯 · 歷史紀錄
最後編輯時間為 2018 年 6 月 12 日上午 8:46(格林威治標準時間)(比較)