-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |