daachorse 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/daachorse.rb ADDED
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "daachorse/version"
4
+
5
+ # Fast multi-pattern string search backed by the daachorse double-array
6
+ # Aho-Corasick automaton (https://github.com/daac-tools/daachorse).
7
+ module Daachorse
8
+ # Base class of every error raised by this library.
9
+ class Error < StandardError; end
10
+
11
+ # Raised by Automaton.load when the input is not a valid dump.
12
+ class FormatError < Error; end
13
+ end
14
+
15
+ begin
16
+ ruby_version = RUBY_VERSION[/\A\d+\.\d+/]
17
+ require_relative "daachorse/#{ruby_version}/daachorse"
18
+ rescue LoadError
19
+ require_relative "daachorse/daachorse"
20
+ end
21
+
22
+ module Daachorse
23
+ # A compiled set of byte-string patterns.
24
+ #
25
+ # Every pattern's value is its index in the array given to Automaton.new.
26
+ # When a pattern occurs more than once, the first index wins. All offsets
27
+ # are byte offsets into the searched string.
28
+ class Automaton
29
+ MATCH_KINDS = %i[standard leftmost_longest leftmost_first].freeze
30
+
31
+ # Builds an automaton.
32
+ #
33
+ # patterns - Array of non-empty Strings (compared as bytes).
34
+ # match_kind: - :standard, :leftmost_longest or :leftmost_first.
35
+ # ascii_case_insensitive - fold ASCII letters A-Z to a-z in patterns and texts.
36
+ def self.new(patterns, match_kind: :standard, ascii_case_insensitive: false)
37
+ kind_id = MATCH_KINDS.index(match_kind)
38
+ raise ArgumentError, "unknown match_kind #{match_kind.inspect}, expected one of #{MATCH_KINDS.inspect}" unless kind_id
39
+
40
+ _build(patterns, kind_id, ascii_case_insensitive ? true : false)
41
+ end
42
+
43
+ # Restores an automaton from a String produced by #dump.
44
+ # Raises Daachorse::FormatError when the bytes are not a valid dump.
45
+ def self.load(bytes)
46
+ _load(bytes)
47
+ end
48
+
49
+ # The match kind the automaton was built with.
50
+ def match_kind
51
+ MATCH_KINDS.fetch(_match_kind_id)
52
+ end
53
+
54
+ # Yields start, end and value of every match. Returns an Enumerator without a block.
55
+ def each_match(text)
56
+ return enum_for(:each_match, text) unless block_given?
57
+
58
+ _each_match(text) { |start, finish, value| yield start, finish, value }
59
+ self
60
+ end
61
+
62
+ # Yields every occurrence of every pattern, including overlapping ones.
63
+ # Only available for match_kind: :standard.
64
+ def each_overlapping(text)
65
+ return enum_for(:each_overlapping, text) unless block_given?
66
+
67
+ _each_overlapping(text) { |start, finish, value| yield start, finish, value }
68
+ self
69
+ end
70
+
71
+ def inspect
72
+ "#<#{self.class} match_kind=#{match_kind} ascii_case_insensitive=#{ascii_case_insensitive?} " \
73
+ "num_states=#{num_states} heap_bytes=#{heap_bytes}>"
74
+ end
75
+
76
+ private_class_method :_build, :_load
77
+ private :_each_match, :_each_overlapping, :_match_kind_id
78
+ end
79
+ end
data/sig/daachorse.rbs ADDED
@@ -0,0 +1,32 @@
1
+ module Daachorse
2
+ VERSION: String
3
+
4
+ class Error < StandardError
5
+ end
6
+
7
+ class FormatError < Error
8
+ end
9
+
10
+ class Automaton
11
+ type match_kind = :standard | :leftmost_longest | :leftmost_first
12
+
13
+ MATCH_KINDS: Array[match_kind]
14
+
15
+ def self.new: (Array[String] patterns, ?match_kind: match_kind, ?ascii_case_insensitive: boolish) -> Automaton
16
+ def self.load: (String bytes) -> Automaton
17
+
18
+ def each_match: (String text) { (Integer start, Integer finish, Integer value) -> void } -> self
19
+ | (String text) -> Enumerator[[Integer, Integer, Integer], self]
20
+ def each_overlapping: (String text) { (Integer start, Integer finish, Integer value) -> void } -> self
21
+ | (String text) -> Enumerator[[Integer, Integer, Integer], self]
22
+ def find_all: (String text) -> Array[[Integer, Integer, Integer]]
23
+ def find_all_flat: (String text) -> Array[Integer]
24
+ def find_overlapping: (String text) -> Array[[Integer, Integer, Integer]]
25
+ def match?: (String text) -> bool
26
+ def dump: () -> String
27
+ def match_kind: () -> match_kind
28
+ def ascii_case_insensitive?: () -> bool
29
+ def heap_bytes: () -> Integer
30
+ def num_states: () -> Integer
31
+ end
32
+ end
metadata ADDED
@@ -0,0 +1,76 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: daachorse
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - FoundRole
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: rb_sys
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: 0.9.130
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: 0.9.130
26
+ description: 'Ruby bindings for the daachorse Rust crate: compact double-array Aho-Corasick
27
+ automata with standard, leftmost-longest and leftmost-first matching, ASCII case
28
+ folding, and a dump/load format for sharing a built automaton between processes.'
29
+ email:
30
+ - argus@foundrole.com
31
+ executables: []
32
+ extensions:
33
+ - ext/daachorse/extconf.rb
34
+ extra_rdoc_files: []
35
+ files:
36
+ - CHANGELOG.md
37
+ - Cargo.lock
38
+ - Cargo.toml
39
+ - LICENSE-APACHE
40
+ - LICENSE-MIT
41
+ - README.md
42
+ - ext/daachorse/Cargo.toml
43
+ - ext/daachorse/build.rs
44
+ - ext/daachorse/extconf.rb
45
+ - ext/daachorse/src/lib.rs
46
+ - lib/daachorse.rb
47
+ - lib/daachorse/version.rb
48
+ - sig/daachorse.rbs
49
+ homepage: https://github.com/foundrole/daachorse-ruby
50
+ licenses:
51
+ - MIT
52
+ - Apache-2.0
53
+ metadata:
54
+ homepage_uri: https://github.com/foundrole/daachorse-ruby
55
+ source_code_uri: https://github.com/foundrole/daachorse-ruby
56
+ changelog_uri: https://github.com/foundrole/daachorse-ruby/blob/main/CHANGELOG.md
57
+ bug_tracker_uri: https://github.com/foundrole/daachorse-ruby/issues
58
+ rubygems_mfa_required: 'true'
59
+ rdoc_options: []
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: 3.3.0
67
+ required_rubygems_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ requirements: []
73
+ rubygems_version: 3.6.9
74
+ specification_version: 4
75
+ summary: Fast multi-pattern search with a serializable double-array Aho-Corasick automaton
76
+ test_files: []