daachorse 0.1.0-aarch64-linux-musl
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.
- checksums.yaml +7 -0
- data/CHANGELOG.md +24 -0
- data/LICENSE-APACHE +176 -0
- data/LICENSE-MIT +25 -0
- data/README.md +186 -0
- data/lib/daachorse/3.3/daachorse.so +0 -0
- data/lib/daachorse/3.4/daachorse.so +0 -0
- data/lib/daachorse/4.0/daachorse.so +0 -0
- data/lib/daachorse/version.rb +5 -0
- data/lib/daachorse.rb +79 -0
- data/sig/daachorse.rbs +32 -0
- metadata +64 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: a1915d9c11c83154ab9cfa1d2a4327b7a531ac9855b4b39b13d9d3b5dec92215
|
|
4
|
+
data.tar.gz: 3cc506634b07813306670e502cc763b6669a8e96359497639b9ac27afe5e8acb
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 8a1786920cd5035796714ebe91c25ab296b4aa6469ed00f174e21e2ca02ca620048a2a62dc7cd4665ab1fa945d86c5653b145e264cf6208a60fa2c81113fcd84
|
|
7
|
+
data.tar.gz: ccf15b68118863c0436fe04ec2f70f37ef751c8c86270bf9fe1492646245baf67e6511bf8c77b449d6763e20f6b2c1e6176b6a62766837a449fea78831c9e033
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
All notable changes to this project are documented in this file. The format
|
|
4
|
+
follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the
|
|
5
|
+
project uses [Semantic Versioning](https://semver.org/).
|
|
6
|
+
|
|
7
|
+
## [0.1.0] - Unreleased
|
|
8
|
+
|
|
9
|
+
### Added
|
|
10
|
+
|
|
11
|
+
- `Daachorse::Automaton` binding daachorse 5.1.0 with the `:standard`,
|
|
12
|
+
`:leftmost_longest` and `:leftmost_first` match kinds.
|
|
13
|
+
- ASCII case-insensitive matching that keeps byte offsets valid for the
|
|
14
|
+
original text.
|
|
15
|
+
- Search methods: `each_match`, `find_all`, `find_all_flat`, `match?`, and for
|
|
16
|
+
`:standard` automata `each_overlapping` and `find_overlapping`.
|
|
17
|
+
- `dump` / `Automaton.load` with a versioned header (dump format version 1).
|
|
18
|
+
- `heap_bytes` and `num_states`.
|
|
19
|
+
- The GVL is released while an automaton is built and while long texts are
|
|
20
|
+
searched by `find_all`, `find_all_flat` and `find_overlapping`.
|
|
21
|
+
- Precompiled native gems for x86_64/aarch64 Linux (glibc and musl) and
|
|
22
|
+
x86_64/arm64 macOS, Ruby 3.3 to 4.0.
|
|
23
|
+
|
|
24
|
+
[0.1.0]: https://github.com/foundrole/daachorse-ruby/releases/tag/v0.1.0
|
data/LICENSE-APACHE
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
data/LICENSE-MIT
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
Copyright (c) 2026 FoundRole
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any
|
|
4
|
+
person obtaining a copy of this software and associated
|
|
5
|
+
documentation files (the "Software"), to deal in the
|
|
6
|
+
Software without restriction, including without
|
|
7
|
+
limitation the rights to use, copy, modify, merge,
|
|
8
|
+
publish, distribute, sublicense, and/or sell copies of
|
|
9
|
+
the Software, and to permit persons to whom the Software
|
|
10
|
+
is furnished to do so, subject to the following
|
|
11
|
+
conditions:
|
|
12
|
+
|
|
13
|
+
The above copyright notice and this permission notice
|
|
14
|
+
shall be included in all copies or substantial portions
|
|
15
|
+
of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
|
|
18
|
+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
|
|
19
|
+
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
|
|
20
|
+
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
|
|
21
|
+
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
22
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
23
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
|
|
24
|
+
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
25
|
+
DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
# daachorse
|
|
2
|
+
|
|
3
|
+
Fast multi-pattern string search for Ruby, backed by
|
|
4
|
+
[daachorse](https://github.com/daac-tools/daachorse), a compact double-array
|
|
5
|
+
Aho-Corasick automaton written in Rust.
|
|
6
|
+
|
|
7
|
+
- Hundreds of thousands of patterns in a few tens of megabytes.
|
|
8
|
+
- Standard, leftmost-longest and leftmost-first match semantics.
|
|
9
|
+
- Optional ASCII case-insensitive matching.
|
|
10
|
+
- `dump` / `load` so a built automaton can be saved to a file or shared
|
|
11
|
+
between processes and loaded in milliseconds instead of rebuilt.
|
|
12
|
+
- Precompiled native gems for Linux (glibc and musl, x86_64 and aarch64) and
|
|
13
|
+
macOS (x86_64 and arm64), Ruby 3.3 to 4.0. Other platforms compile from
|
|
14
|
+
source and need a Rust toolchain.
|
|
15
|
+
|
|
16
|
+
## Installation
|
|
17
|
+
|
|
18
|
+
```sh
|
|
19
|
+
bundle add daachorse
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Usage
|
|
23
|
+
|
|
24
|
+
```ruby
|
|
25
|
+
require "daachorse"
|
|
26
|
+
|
|
27
|
+
automaton = Daachorse::Automaton.new(%w[he she his hers], match_kind: :leftmost_longest)
|
|
28
|
+
|
|
29
|
+
automaton.find_all("ushers") # => [[1, 4, 1]]
|
|
30
|
+
automaton.find_all_flat("ushers") # => [1, 4, 1]
|
|
31
|
+
automaton.match?("this") # => true
|
|
32
|
+
|
|
33
|
+
automaton.each_match("she said his") do |start, finish, value|
|
|
34
|
+
# start/finish are byte offsets, value is the pattern's index
|
|
35
|
+
end
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Values and offsets
|
|
39
|
+
|
|
40
|
+
The value reported for a match is the index of the pattern in the array given
|
|
41
|
+
to `Automaton.new`. If the same pattern appears more than once, the first
|
|
42
|
+
index wins. Patterns must be non-empty strings; an empty pattern raises
|
|
43
|
+
`ArgumentError`.
|
|
44
|
+
|
|
45
|
+
Patterns and texts are compared as raw bytes, whatever their encoding. Every
|
|
46
|
+
offset is a byte offset, so use `String#byteslice` to extract a match:
|
|
47
|
+
|
|
48
|
+
```ruby
|
|
49
|
+
text = "привет, мир"
|
|
50
|
+
start, finish, = Daachorse::Automaton.new(["мир"]).find_all(text).first
|
|
51
|
+
text.byteslice(start, finish - start) # => "мир"
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
### Match kinds
|
|
55
|
+
|
|
56
|
+
| `match_kind:` | Reports |
|
|
57
|
+
|---------------------|-------------------------------------------------------------------------------------------------------------------|
|
|
58
|
+
| `:standard` (default) | Non-overlapping matches in standard Aho-Corasick order: the match that ends first, then the search continues after it. |
|
|
59
|
+
| `:leftmost_longest` | The match that starts leftmost; among those, the longest pattern. |
|
|
60
|
+
| `:leftmost_first` | The match that starts leftmost; among those, the pattern that comes first in the array. |
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
patterns = %w[ab a abcd]
|
|
64
|
+
Daachorse::Automaton.new(patterns).find_all("abcd") # => [[0, 1, 1]]
|
|
65
|
+
Daachorse::Automaton.new(patterns, match_kind: :leftmost_longest).find_all("abcd") # => [[0, 4, 2]]
|
|
66
|
+
Daachorse::Automaton.new(patterns, match_kind: :leftmost_first).find_all("abcd") # => [[0, 2, 0]]
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Only `:standard` automata can report every occurrence, overlaps included:
|
|
70
|
+
|
|
71
|
+
```ruby
|
|
72
|
+
Daachorse::Automaton.new(patterns).find_overlapping("abcd")
|
|
73
|
+
# => [[0, 1, 1], [0, 2, 0], [0, 4, 2]]
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Calling `each_overlapping` or `find_overlapping` on a leftmost automaton raises
|
|
77
|
+
`Daachorse::Error`.
|
|
78
|
+
|
|
79
|
+
### Case-insensitive matching
|
|
80
|
+
|
|
81
|
+
```ruby
|
|
82
|
+
automaton = Daachorse::Automaton.new(%w[ruby], ascii_case_insensitive: true)
|
|
83
|
+
automaton.find_all("Ruby RUBY ruby") # => [[0, 4, 0], [5, 9, 0], [10, 14, 0]]
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Only the ASCII letters `A`–`Z` are folded. Other bytes, including every byte of
|
|
87
|
+
a multi-byte UTF-8 character, are compared exactly, so offsets stay valid for
|
|
88
|
+
the original text and `"É"` does not match `"é"`.
|
|
89
|
+
|
|
90
|
+
### Methods
|
|
91
|
+
|
|
92
|
+
| Method | Returns |
|
|
93
|
+
|-------------------------------|-------------------------------------------------------------------------|
|
|
94
|
+
| `each_match(text) { \|start, finish, value\| }` | `self`; an `Enumerator` without a block. No array is allocated per match. |
|
|
95
|
+
| `find_all(text)` | `[[start, finish, value], ...]` |
|
|
96
|
+
| `find_all_flat(text)` | `[start, finish, value, start, finish, value, ...]` |
|
|
97
|
+
| `match?(text)` | `true` if any pattern occurs |
|
|
98
|
+
| `each_overlapping(text)`, `find_overlapping(text)` | every occurrence (`:standard` only) |
|
|
99
|
+
| `dump` | binary `String` |
|
|
100
|
+
| `Automaton.load(bytes)` | a new `Automaton` |
|
|
101
|
+
| `match_kind`, `ascii_case_insensitive?` | the build options |
|
|
102
|
+
| `heap_bytes` | bytes of heap memory held by the automaton |
|
|
103
|
+
| `num_states` | number of automaton states |
|
|
104
|
+
|
|
105
|
+
## Serialization
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
File.binwrite("patterns.daac", automaton.dump)
|
|
109
|
+
automaton = Daachorse::Automaton.load(File.binread("patterns.daac"))
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
A dump is an 8-byte header followed by daachorse's own serialization:
|
|
113
|
+
|
|
114
|
+
| Bytes | Content |
|
|
115
|
+
|-------|----------------------------------------------------------------------|
|
|
116
|
+
| 0–3 | magic `DAAC` |
|
|
117
|
+
| 4 | dump format version, currently `1` |
|
|
118
|
+
| 5 | flags; bit 0 set means ASCII case-insensitive |
|
|
119
|
+
| 6 | match kind: 0 standard, 1 leftmost-longest, 2 leftmost-first |
|
|
120
|
+
| 7 | reserved, always 0 |
|
|
121
|
+
| 8– | `DoubleArrayAhoCorasick::serialize` output of the bundled daachorse |
|
|
122
|
+
|
|
123
|
+
`load` rejects a wrong magic, an unknown format version, unknown flags, an
|
|
124
|
+
unknown or inconsistent match kind, truncated data and trailing bytes with
|
|
125
|
+
`Daachorse::FormatError`. The automaton itself is read with daachorse's
|
|
126
|
+
checked `deserialize`, which guarantees memory safety for any input. It does
|
|
127
|
+
not prove that the data is a well-formed automaton: crafted bytes can still
|
|
128
|
+
produce wrong matches or a search that never ends. Load only dumps you
|
|
129
|
+
produced yourself.
|
|
130
|
+
|
|
131
|
+
The format version is bumped whenever the header or the bundled daachorse
|
|
132
|
+
serialization changes, so a dump written by one version of this gem can be
|
|
133
|
+
loaded by any version that reports the same format version.
|
|
134
|
+
|
|
135
|
+
## Threads and the GVL
|
|
136
|
+
|
|
137
|
+
`Automaton.new` copies the patterns and then builds the automaton with the GVL
|
|
138
|
+
released, so other Ruby threads keep running during a long build.
|
|
139
|
+
`find_all`, `find_all_flat` and `find_overlapping` copy texts of 16 KiB or more
|
|
140
|
+
and search the copy with the GVL released. Shorter texts are searched in
|
|
141
|
+
place, because releasing the GVL costs more than the search.
|
|
142
|
+
|
|
143
|
+
`each_match` and `each_overlapping` keep the GVL, since they call the block for
|
|
144
|
+
every match. They search a private copy of the text, so a block may safely
|
|
145
|
+
modify the string it is iterating over.
|
|
146
|
+
|
|
147
|
+
An automaton is immutable after it is built and can be shared between threads.
|
|
148
|
+
|
|
149
|
+
## Benchmarks
|
|
150
|
+
|
|
151
|
+
Measured by the author on an Apple M1 Max, macOS, Ruby 3.4.9, release build of
|
|
152
|
+
this gem, with 200,000 random ASCII patterns of 4 to 16 characters drawn from
|
|
153
|
+
`a`–`z`, space, `-` and `.` (2.0 MB of pattern text), `:leftmost_longest`:
|
|
154
|
+
|
|
155
|
+
| Measurement | Value |
|
|
156
|
+
|-------------------------------------|-------------|
|
|
157
|
+
| Build | 0.45 s |
|
|
158
|
+
| `heap_bytes` | 18.5 MB |
|
|
159
|
+
| States | 1,402,017 |
|
|
160
|
+
| Dump size | 18.5 MB |
|
|
161
|
+
| `dump` | 9 ms |
|
|
162
|
+
| `load` (best of 5) | 7 ms |
|
|
163
|
+
| Process RSS growth after `load` only | 18.6 MB |
|
|
164
|
+
| `find_all` over 1 MB of random text | 25 ms |
|
|
165
|
+
|
|
166
|
+
Building needs far more memory than the finished automaton (the process RSS
|
|
167
|
+
grew by about 190 MB during the build above), which is one reason to build
|
|
168
|
+
once and `load` the dump everywhere else.
|
|
169
|
+
|
|
170
|
+
## Development
|
|
171
|
+
|
|
172
|
+
```sh
|
|
173
|
+
bundle install
|
|
174
|
+
bundle exec rake compile test
|
|
175
|
+
cargo fmt --all -- --check
|
|
176
|
+
cargo clippy --all-targets -- -D warnings
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
## License
|
|
180
|
+
|
|
181
|
+
Licensed under either of
|
|
182
|
+
|
|
183
|
+
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE))
|
|
184
|
+
- MIT license ([LICENSE-MIT](LICENSE-MIT))
|
|
185
|
+
|
|
186
|
+
at your option, the same terms as daachorse itself.
|
|
Binary file
|
|
Binary file
|
|
Binary file
|
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,64 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: daachorse
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: aarch64-linux-musl
|
|
6
|
+
authors:
|
|
7
|
+
- FoundRole
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2026-12-02 00:00:00.000000000 Z
|
|
12
|
+
dependencies: []
|
|
13
|
+
description: 'Ruby bindings for the daachorse Rust crate: compact double-array Aho-Corasick
|
|
14
|
+
automata with standard, leftmost-longest and leftmost-first matching, ASCII case
|
|
15
|
+
folding, and a dump/load format for sharing a built automaton between processes.'
|
|
16
|
+
email:
|
|
17
|
+
- argus@foundrole.com
|
|
18
|
+
executables: []
|
|
19
|
+
extensions: []
|
|
20
|
+
extra_rdoc_files: []
|
|
21
|
+
files:
|
|
22
|
+
- CHANGELOG.md
|
|
23
|
+
- LICENSE-APACHE
|
|
24
|
+
- LICENSE-MIT
|
|
25
|
+
- README.md
|
|
26
|
+
- lib/daachorse.rb
|
|
27
|
+
- lib/daachorse/3.3/daachorse.so
|
|
28
|
+
- lib/daachorse/3.4/daachorse.so
|
|
29
|
+
- lib/daachorse/4.0/daachorse.so
|
|
30
|
+
- lib/daachorse/version.rb
|
|
31
|
+
- sig/daachorse.rbs
|
|
32
|
+
homepage: https://github.com/foundrole/daachorse-ruby
|
|
33
|
+
licenses:
|
|
34
|
+
- MIT
|
|
35
|
+
- Apache-2.0
|
|
36
|
+
metadata:
|
|
37
|
+
homepage_uri: https://github.com/foundrole/daachorse-ruby
|
|
38
|
+
source_code_uri: https://github.com/foundrole/daachorse-ruby
|
|
39
|
+
changelog_uri: https://github.com/foundrole/daachorse-ruby/blob/main/CHANGELOG.md
|
|
40
|
+
bug_tracker_uri: https://github.com/foundrole/daachorse-ruby/issues
|
|
41
|
+
rubygems_mfa_required: 'true'
|
|
42
|
+
post_install_message:
|
|
43
|
+
rdoc_options: []
|
|
44
|
+
require_paths:
|
|
45
|
+
- lib
|
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
47
|
+
requirements:
|
|
48
|
+
- - ">="
|
|
49
|
+
- !ruby/object:Gem::Version
|
|
50
|
+
version: '3.3'
|
|
51
|
+
- - "<"
|
|
52
|
+
- !ruby/object:Gem::Version
|
|
53
|
+
version: 4.1.dev
|
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - ">="
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: 3.3.22
|
|
59
|
+
requirements: []
|
|
60
|
+
rubygems_version: 3.5.23
|
|
61
|
+
signing_key:
|
|
62
|
+
specification_version: 4
|
|
63
|
+
summary: Fast multi-pattern search with a serializable double-array Aho-Corasick automaton
|
|
64
|
+
test_files: []
|