Skip to content

Commit

Permalink
add tool hsfgrep.lua
Browse files Browse the repository at this point in the history
  • Loading branch information
starius committed May 17, 2016
1 parent 5fd16ac commit d42a07e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
1 change: 1 addition & 0 deletions luahs-dev-1.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Hyperscan is high-performanceregular expression matching library.
}
dependencies = {
"lua >= 5.1",
"argparse",
}
external_dependencies = {
HS = {
Expand Down
28 changes: 28 additions & 0 deletions spec/hsfgrep_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- luahs, Lua bindings to hyperscan
-- Copyright (C) 2016 Boris Nagaev
-- See the LICENSE file for terms of use.

local luahs = require 'luahs'

describe("hsfgrep.lua", function()

it("look for multiple expressions in multiple files",
function()
local expected = [[
LICENSE Copyright 9
LICENSE 20.6 18
src/luahs/luahs.h Copyright 48
src/luahs/luahs.h 20.6 57
]]
local cmd = table.concat({
'lua', 'src/bin/hsfgrep.lua',
'-p', 'Copyright', '20.6',
'-i', 'LICENSE', 'src/luahs/luahs.h',
}, ' ')
local f = io.popen(cmd, 'r')
local observed = f:read('*a')
f:close()
assert.equal(expected, observed)
end)

end)
48 changes: 48 additions & 0 deletions src/bin/hsfgrep.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
-- luahs, Lua bindings to hyperscan
-- Copyright (C) 2016 Boris Nagaev
-- See the LICENSE file for terms of use.

local argparse = require 'argparse'
local luahs = require 'luahs'

local parser = argparse('hsfgrep', 'Match multiple patterns with Hyperscan')
parser:option('-p --patterns', 'Patterns'):args('+'):count(1)
parser:option('-i --input', 'Input files'):args('+'):count(1)
local args = parser:parse()

local expressions = {}
for i, pattern in ipairs(args.patterns) do
expressions[i] = {
expression = pattern,
id = i,
}
end
local db = luahs.compile {
expressions = expressions,
flags = luahs.pattern_flags.HS_FLAG_DOTALL,
mode = luahs.compile_mode.HS_MODE_STREAM,
}
local scratch = db:makeScratch()
local stream = db:makeStream()

for _, filename in ipairs(args.input) do

local function printHits(hits)
local fmt = '%s\t%s\t%d'
for _, hit in ipairs(hits) do
print(fmt:format(filename, args.patterns[hit.id], hit.to))
end
end

local file = io.open(filename, 'r')
while true do
local chunk = file:read(8192)
if not chunk then
break
end
local hits = stream:scan(chunk, scratch)
printHits(hits)
end
local hits = stream:reset(scratch)
printHits(hits)
end

0 comments on commit d42a07e

Please sign in to comment.