-module(boyer_moore_ffi). -export([build_pattern/1, search/3]). build_pattern(Pattern) -> case bit_size(Pattern) of NonAligned when (NonAligned rem 8) /= 0 -> {error, nil}; Aligned -> case Aligned of 0 -> {error, nil}; _ -> CompiledPattern = binary:compile_pattern(Pattern), {ok, {searcher, fun(Data, Bounds) -> search(Data, CompiledPattern, Bounds) end}} end end. search(Data, CompiledPattern, {bounds, Start, End}) -> DataSize = byte_size(Data), RealStart = case Start of {some, N} when N < 0 -> 0; {some, N} when N >= DataSize -> DataSize; {some, N} -> N; none -> 0 end, RealEnd = case End of {some, N2} when N2 < RealStart -> RealStart; {some, N2} when N2 >= DataSize -> DataSize; {some, N2} -> N2; none -> DataSize end, Length = RealEnd - RealStart, case binary:match(Data, CompiledPattern, [{scope, {RealStart, Length}}]) of nomatch -> {error, nil}; {Pos, _Length} -> {ok, Pos} end.