list_matcher 1.0.7 → 1.3.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.
- checksums.yaml +5 -5
- data/CHANGELOG.md +68 -0
- data/README.md +12 -0
- data/lib/list_matcher/version.rb +3 -1
- data/lib/list_matcher.rb +109 -76
- data/list_matcher.gemspec +13 -5
- data/test/basic_test.rb +92 -0
- data/test/doc_test.rb +2 -2
- metadata +24 -22
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 895b21759da8028bb27576efe53424aeebc24bbb7e4c502c9e0b2f26d9848cc5
|
|
4
|
+
data.tar.gz: 843190f593c37a11a8334eaa4e673024a5f96c074b8a8d407696488091022f1a
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '099e6afe3e5974a96ad2532cbfe9961daa164624aad06bac59dcd402cd589b5934b7bc2836f3f821529da6385ce541dabce53c57486b241913b4b0dbd0312769'
|
|
7
|
+
data.tar.gz: 1a0f02e3ae50f5be8929703b0572d3ef27bbcb3896175b1961ab783f15d4ea158df03873b4572e03c7a8f1f665d9a3fb9699f9adb139e9878050a635501f7fda
|
data/CHANGELOG.md
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## v1.3.0 *2026-11-28*
|
|
4
|
+
- Modernized for current Ruby:
|
|
5
|
+
- Require Ruby >= 3.0; added `# frozen_string_literal: true`
|
|
6
|
+
- Public methods now take keyword arguments (`**opts`) instead of positional option hashes
|
|
7
|
+
- Replaced legacy idioms with modern equivalents (`Hash[]` → `to_h`, `reduce(0, :+)` → `sum`, `chars` → `each_char`, endless ranges)
|
|
8
|
+
- Single-pass list cleanup in `Matcher#pattern` (compact, stringify, strip, normalize whitespace)
|
|
9
|
+
- Optimized pattern generation: `best_prefix`/`best_suffix` now group prefix/suffix candidates without sorting single-element groups or rebuilding discarded cross products (~15-20% faster on large word lists, output unchanged)
|
|
10
|
+
- Gemspec: modern development dependencies, added `spec.metadata` URIs, squiggly heredoc; dropped unused `byebug` dev dependency
|
|
11
|
+
|
|
12
|
+
## v1.2.0 *2026-11-15*
|
|
13
|
+
- Port fixes from list-matcher-js v1.0.4:
|
|
14
|
+
- Update CharClass#cc_quote to use hex escapes for control characters 1-8 (\\x01-\\x08) for improved readability
|
|
15
|
+
- Add named escapes for control characters 9-13 and 27 (\\t, \\n, \\v, \\f, \\r, \\e)
|
|
16
|
+
- Add comprehensive Unicode tests for emoji, supplementary plane characters, zero-width characters, combined Unicode, and control characters
|
|
17
|
+
|
|
18
|
+
## v1.1.8 *2018-05-28*
|
|
19
|
+
- Renamed for clarity
|
|
20
|
+
- Removed some whitespace
|
|
21
|
+
- Suppressed some error messages thrown during rake test
|
|
22
|
+
- Tinkered with gemspec to remove all warnings
|
|
23
|
+
- Fixed problem with wide characters in character classes
|
|
24
|
+
|
|
25
|
+
## v1.1.7 *2016-01-19*
|
|
26
|
+
- Bumped version number
|
|
27
|
+
- Properly handle empty list
|
|
28
|
+
- Removed some unnecessary code
|
|
29
|
+
|
|
30
|
+
## v1.0.7 *2016-01-19*
|
|
31
|
+
- Bumped version number (release)
|
|
32
|
+
|
|
33
|
+
## v1.0.6 *2015-09-04*
|
|
34
|
+
- Bumped version number
|
|
35
|
+
- Improved mini grammar
|
|
36
|
+
- Improved doc tests to include new symbol stuff
|
|
37
|
+
- Better documentation
|
|
38
|
+
- Documentation of the symbol boundary stuff
|
|
39
|
+
- Fixed short bound word bug
|
|
40
|
+
- Made bounds work with symbols
|
|
41
|
+
- More refactoring to make code comprehensible
|
|
42
|
+
- Small simplification
|
|
43
|
+
|
|
44
|
+
## v1.0.5 *2015-09-01*
|
|
45
|
+
- Bumped version number
|
|
46
|
+
- Improved docs
|
|
47
|
+
- Implemented List::Matcher::Error
|
|
48
|
+
- Partially implemented tests
|
|
49
|
+
|
|
50
|
+
## v1.0.4 *2015-08-30*
|
|
51
|
+
- Allow bounding only at the left or right margin
|
|
52
|
+
|
|
53
|
+
## v1.0.3 *2015-08-18*
|
|
54
|
+
- Bumped version number before release
|
|
55
|
+
- Documentation improvements
|
|
56
|
+
- Removed optional symbol bug
|
|
57
|
+
|
|
58
|
+
## v1.0.2 *2015-08-16*
|
|
59
|
+
- Documentation improvements
|
|
60
|
+
- Added the not_extended option
|
|
61
|
+
- Added test to make sure all the documentation examples are good
|
|
62
|
+
|
|
63
|
+
## v1.0.1 *2015-08-16*
|
|
64
|
+
- Bumped least version number
|
|
65
|
+
- Set minimum ruby version
|
|
66
|
+
|
|
67
|
+
## v1.0.0 *2015-08-16*
|
|
68
|
+
- Initial release
|
data/README.md
CHANGED
|
@@ -401,6 +401,18 @@ List::Matcher.pattern [ 'cat and dog', '# is sometimes called the pound symbol'
|
|
|
401
401
|
|
|
402
402
|
Note that `List::Matcher` will continue to quote other white space characters.
|
|
403
403
|
|
|
404
|
+
### encoding
|
|
405
|
+
|
|
406
|
+
```ruby
|
|
407
|
+
default: Encoding::UTF_8
|
|
408
|
+
```
|
|
409
|
+
|
|
410
|
+
`List::Matcher` converts characters into integers during the construction of character classes and then
|
|
411
|
+
must convert the integers back into characters. In order to do this with characters whose code point is
|
|
412
|
+
above 255 we must give `chr` a character encoding. This is all that this is. Most likely you will never
|
|
413
|
+
need to change this default, but if it causes you trouble this parameters is provided so you can fix
|
|
414
|
+
it without having to fork the code.
|
|
415
|
+
|
|
404
416
|
## Benchmarks
|
|
405
417
|
|
|
406
418
|
Efficiency isn't the principle purpose of List::Matcher, but in almost all cases List::Matcher
|
data/lib/list_matcher/version.rb
CHANGED
data/lib/list_matcher.rb
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require "list_matcher/version"
|
|
2
4
|
|
|
3
5
|
module List
|
|
4
6
|
class Matcher
|
|
5
7
|
attr_reader :atomic, :backtracking, :bound, :case_insensitive, :strip, :left_bound,
|
|
6
|
-
:right_bound, :word_test, :normalize_whitespace, :multiline, :name, :vet, :not_extended
|
|
8
|
+
:right_bound, :word_test, :normalize_whitespace, :multiline, :name, :vet, :not_extended,
|
|
9
|
+
:encoding
|
|
7
10
|
|
|
8
11
|
# a special exception class for List::Matcher
|
|
9
12
|
class Error < ::StandardError; end
|
|
10
13
|
|
|
11
14
|
# convenience method for one-off regexen where there's no point in keeping
|
|
12
15
|
# around a pattern generator
|
|
13
|
-
def self.pattern(list, opts
|
|
16
|
+
def self.pattern(list, **opts)
|
|
14
17
|
self.new(**opts).pattern list
|
|
15
18
|
end
|
|
16
19
|
|
|
17
20
|
# like self.pattern, but returns a regex rather than a string
|
|
18
|
-
def self.rx(list, opts
|
|
21
|
+
def self.rx(list, **opts)
|
|
19
22
|
self.new(**opts).rx list
|
|
20
23
|
end
|
|
21
24
|
|
|
@@ -33,7 +36,8 @@ module List
|
|
|
33
36
|
normalize_whitespace: false,
|
|
34
37
|
symbols: {},
|
|
35
38
|
name: false,
|
|
36
|
-
vet: false
|
|
39
|
+
vet: false,
|
|
40
|
+
encoding: Encoding::UTF_8
|
|
37
41
|
)
|
|
38
42
|
@atomic = atomic
|
|
39
43
|
@backtracking = backtracking
|
|
@@ -46,6 +50,7 @@ module List
|
|
|
46
50
|
@bound = !!bound
|
|
47
51
|
@normalize_whitespace = normalize_whitespace
|
|
48
52
|
@vet = vet
|
|
53
|
+
@encoding = encoding
|
|
49
54
|
if name
|
|
50
55
|
raise Error, "name must be a string or symbol" unless name.is_a?(String) || name.is_a?(Symbol)
|
|
51
56
|
begin
|
|
@@ -54,6 +59,8 @@ module List
|
|
|
54
59
|
rescue
|
|
55
60
|
raise Error, "#{name} does not work as the name of a named group"
|
|
56
61
|
end
|
|
62
|
+
else
|
|
63
|
+
@name = nil
|
|
57
64
|
end
|
|
58
65
|
case bound
|
|
59
66
|
when TrueClass
|
|
@@ -84,7 +91,7 @@ module List
|
|
|
84
91
|
@left_bound = nil
|
|
85
92
|
end
|
|
86
93
|
when Hash
|
|
87
|
-
@word_test = bound[:test] || raise(
|
|
94
|
+
@word_test = bound[:test] || raise(Error, 'no boundary test provided')
|
|
88
95
|
@left_bound = bound[:left]
|
|
89
96
|
@right_bound = bound[:right]
|
|
90
97
|
raise Error, 'neither bound provided' unless @left_bound || @right_bound
|
|
@@ -118,7 +125,7 @@ module List
|
|
|
118
125
|
end
|
|
119
126
|
|
|
120
127
|
# returns a new pattern matcher differing from the original only in the options specified
|
|
121
|
-
def bud(opts
|
|
128
|
+
def bud(**opts)
|
|
122
129
|
opts = {
|
|
123
130
|
atomic: @atomic,
|
|
124
131
|
backtracking: @backtracking,
|
|
@@ -135,13 +142,17 @@ module List
|
|
|
135
142
|
self.class.new(**opts)
|
|
136
143
|
end
|
|
137
144
|
|
|
138
|
-
#
|
|
139
|
-
def pattern( list, opts
|
|
145
|
+
# converts list into a string representing a regex pattern suitable for inclusion in a larger regex
|
|
146
|
+
def pattern( list, **opts )
|
|
140
147
|
return '(?!)' unless list.any?
|
|
141
|
-
return bud(opts).pattern list unless opts.empty?
|
|
142
|
-
list = list.
|
|
143
|
-
|
|
144
|
-
|
|
148
|
+
return bud(**opts).pattern list unless opts.empty?
|
|
149
|
+
list = list.filter_map do |e|
|
|
150
|
+
s = e&.to_s
|
|
151
|
+
next if s.nil? || s.empty?
|
|
152
|
+
s = s.strip if strip
|
|
153
|
+
next if s.empty?
|
|
154
|
+
normalize_whitespace ? s.gsub(/\s++/, ' ') : s
|
|
155
|
+
end
|
|
145
156
|
return nil if list.empty?
|
|
146
157
|
specializer = Special.new self, @symbols, list
|
|
147
158
|
list = specializer.normalize
|
|
@@ -175,8 +186,8 @@ module List
|
|
|
175
186
|
end
|
|
176
187
|
|
|
177
188
|
# like pattern but it returns a regex instead of a string
|
|
178
|
-
def rx(list, opts
|
|
179
|
-
Regexp.new pattern(list, opts)
|
|
189
|
+
def rx(list, **opts)
|
|
190
|
+
Regexp.new pattern(list, **opts)
|
|
180
191
|
end
|
|
181
192
|
|
|
182
193
|
def pfx
|
|
@@ -195,10 +206,10 @@ module List
|
|
|
195
206
|
@wrap_size ||= pfx.length + 1
|
|
196
207
|
end
|
|
197
208
|
|
|
198
|
-
def tree(list,
|
|
209
|
+
def tree(list, specializer)
|
|
199
210
|
if list.size == 1
|
|
200
|
-
leaves = list[0].
|
|
201
|
-
|
|
211
|
+
leaves = list[0].each_char.map do |c|
|
|
212
|
+
specializer[c] || Leaf.new( self, c )
|
|
202
213
|
end
|
|
203
214
|
if leaves.length == 1
|
|
204
215
|
leaves.first
|
|
@@ -206,38 +217,38 @@ module List
|
|
|
206
217
|
Sequence.new self, *leaves
|
|
207
218
|
end
|
|
208
219
|
elsif list.all?{ |w| w.length == 1 }
|
|
209
|
-
chars = list.select{ |w| !
|
|
220
|
+
chars = list.select{ |w| !specializer[w] }
|
|
210
221
|
if chars.size > 1
|
|
211
222
|
list -= chars
|
|
212
223
|
c = CharClass.new self, chars
|
|
213
224
|
end
|
|
214
|
-
a = Alternate.new self,
|
|
225
|
+
a = Alternate.new self, specializer, list unless list.empty?
|
|
215
226
|
a.children.unshift c if a && c
|
|
216
227
|
a || c
|
|
217
228
|
elsif c = best_prefix(list) # found a fixed-width prefix pattern
|
|
218
229
|
if optional = c[1].include?('')
|
|
219
230
|
c[1].reject!{ |w| w == '' }
|
|
220
231
|
end
|
|
221
|
-
c1 = tree c[0],
|
|
222
|
-
c2 = tree c[1],
|
|
232
|
+
c1 = tree c[0], specializer
|
|
233
|
+
c2 = tree c[1], specializer
|
|
223
234
|
c2 = c2.optionalize optional
|
|
224
235
|
Sequence.new self, c1, c2
|
|
225
236
|
elsif c = best_suffix(list) # found a fixed-width suffix pattern
|
|
226
237
|
if optional = c[0].include?('')
|
|
227
238
|
c[0].reject!{ |w| w == '' } # TODO make this faster with index
|
|
228
239
|
end
|
|
229
|
-
c1 = tree c[0],
|
|
240
|
+
c1 = tree c[0], specializer
|
|
230
241
|
c1 = c1.optionalize optional
|
|
231
|
-
c2 = tree c[1],
|
|
242
|
+
c2 = tree c[1], specializer
|
|
232
243
|
Sequence.new self, c1, c2
|
|
233
244
|
else
|
|
234
245
|
grouped = list.group_by{ |w| w[0] }
|
|
235
|
-
chars = grouped.select{ |_, w| w.size == 1 && w[0].size == 1 && !
|
|
246
|
+
chars = grouped.select{ |_, w| w.size == 1 && w[0].size == 1 && !specializer[w[0]] }.map{ |v, _| v }
|
|
236
247
|
if chars.size > 1
|
|
237
248
|
list -= chars
|
|
238
249
|
c = CharClass.new self, chars
|
|
239
250
|
end
|
|
240
|
-
a = Alternate.new self,
|
|
251
|
+
a = Alternate.new self, specializer, list
|
|
241
252
|
a.children.unshift c if c
|
|
242
253
|
a
|
|
243
254
|
end
|
|
@@ -255,7 +266,7 @@ module List
|
|
|
255
266
|
|
|
256
267
|
def deep_dup(o)
|
|
257
268
|
if o.is_a?(Hash)
|
|
258
|
-
|
|
269
|
+
o.to_h { |k, v| [deep_dup(k), deep_dup(v)] }
|
|
259
270
|
elsif o.is_a?(Array)
|
|
260
271
|
o.map{ |v| deep_dup v }
|
|
261
272
|
elsif o.nil? || o.is_a?(Symbol) || o.is_a?(TrueClass) || o.is_a?(FalseClass)
|
|
@@ -265,67 +276,60 @@ module List
|
|
|
265
276
|
end
|
|
266
277
|
end
|
|
267
278
|
|
|
279
|
+
# Finds a fixed-width prefix shared (as a cross product) by every word, if one
|
|
280
|
+
# yields a more compact pattern than the alternatives. Returns [prefixes, suffixes]
|
|
281
|
+
# or nil. Prefix/suffix groups are compared as sorted arrays; single-element groups
|
|
282
|
+
# skip the sort since they are already canonical.
|
|
268
283
|
def best_prefix(list)
|
|
269
284
|
acceptable = nil
|
|
270
285
|
sizes = list.map(&:size)
|
|
271
|
-
min = sizes.
|
|
286
|
+
min = sizes.sum
|
|
272
287
|
sizes.uniq!
|
|
273
288
|
lim = sizes.count == 1 ? list[0].size - 1 : sizes.min
|
|
274
289
|
(1..lim).each do |l|
|
|
275
290
|
c = {}
|
|
276
291
|
list.each do |w|
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
end
|
|
292
|
+
( c[w[0...l]] ||= [] ) << w[l..]
|
|
293
|
+
end
|
|
294
|
+
groups = c.group_by { |_, v| v.size == 1 ? v : v.sort }
|
|
295
|
+
next unless groups.size == 1
|
|
296
|
+
suffixes = groups.keys.first
|
|
297
|
+
prefixes = groups.values.first.map { |a| a[0] }.sort
|
|
298
|
+
cnt = prefixes.sum(&:size) + suffixes.sum(&:size)
|
|
299
|
+
if cnt < min
|
|
300
|
+
min = cnt
|
|
301
|
+
acceptable = [prefixes, suffixes]
|
|
288
302
|
end
|
|
289
303
|
end
|
|
290
304
|
acceptable
|
|
291
305
|
end
|
|
292
306
|
|
|
307
|
+
# Like best_prefix, but for suffixes. Returns [prefixes, suffixes] or nil.
|
|
293
308
|
def best_suffix(list)
|
|
294
309
|
acceptable = nil
|
|
295
310
|
sizes = list.map(&:size)
|
|
296
|
-
min = sizes.
|
|
311
|
+
min = sizes.sum
|
|
297
312
|
sizes.uniq!
|
|
298
313
|
lim = sizes.count == 1 ? list[0].size - 1 : sizes.min
|
|
299
314
|
(1..lim).each do |l|
|
|
300
315
|
c = {}
|
|
301
316
|
list.each do |w|
|
|
302
|
-
i
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
end
|
|
317
|
+
i = w.length - l
|
|
318
|
+
( c[w[i..]] ||= [] ) << w[0...i]
|
|
319
|
+
end
|
|
320
|
+
groups = c.group_by { |_, v| v.size == 1 ? v : v.sort }
|
|
321
|
+
next unless groups.size == 1
|
|
322
|
+
prefixes = groups.keys.first
|
|
323
|
+
suffixes = groups.values.first.map { |a| a[0] }.sort
|
|
324
|
+
cnt = prefixes.sum(&:size) + suffixes.sum(&:size)
|
|
325
|
+
if cnt < min
|
|
326
|
+
min = cnt
|
|
327
|
+
acceptable = [prefixes, suffixes]
|
|
314
328
|
end
|
|
315
329
|
end
|
|
316
330
|
acceptable
|
|
317
331
|
end
|
|
318
332
|
|
|
319
|
-
# discover cross products -- e.g., {this, that} X {cat, dog}
|
|
320
|
-
def cross_products(c)
|
|
321
|
-
c.to_a.group_by{ |_, v| v.sort }.map{ |k,v| [ v.map{ |a| a[0] }.sort, k ] }
|
|
322
|
-
end
|
|
323
|
-
|
|
324
|
-
def count(c)
|
|
325
|
-
c = c[0]
|
|
326
|
-
c[0].map(&:size).reduce( 0, :+ ) + c[1].map(&:size).reduce( 0, :+ )
|
|
327
|
-
end
|
|
328
|
-
|
|
329
333
|
class Special
|
|
330
334
|
attr_reader :engine, :special_map
|
|
331
335
|
attr_accessor :specials, :list, :left, :right
|
|
@@ -335,7 +339,7 @@ module List
|
|
|
335
339
|
@list = list
|
|
336
340
|
max = 0
|
|
337
341
|
list.each do |w|
|
|
338
|
-
w.
|
|
342
|
+
w.each_char { |c| i = c.ord; max = i if i > max }
|
|
339
343
|
end
|
|
340
344
|
@specials = [].tap do |ar|
|
|
341
345
|
specials.sort do |a, b|
|
|
@@ -354,7 +358,7 @@ module List
|
|
|
354
358
|
s == 0 ? a.to_s <=> b.to_s : s
|
|
355
359
|
end
|
|
356
360
|
end.each do |var, opts|
|
|
357
|
-
c = ( max += 1 ).chr
|
|
361
|
+
c = ( max += 1 ).chr(engine.encoding)
|
|
358
362
|
sp = if opts.is_a? Hash
|
|
359
363
|
pat = opts.delete :pattern
|
|
360
364
|
raise Error, "symbol #{var} requires a pattern" unless pat || var.is_a?(Regexp)
|
|
@@ -372,17 +376,17 @@ module List
|
|
|
372
376
|
end
|
|
373
377
|
if engine.bound
|
|
374
378
|
if engine.left_bound
|
|
375
|
-
c = ( max += 1 ).chr
|
|
379
|
+
c = ( max += 1 ).chr(engine.encoding)
|
|
376
380
|
@left = SymbolPattern.new engine, c, c, engine.left_bound
|
|
377
381
|
@specials << @left
|
|
378
382
|
end
|
|
379
383
|
if engine.right_bound
|
|
380
|
-
c = ( max += 1 ).chr
|
|
384
|
+
c = ( max += 1 ).chr(engine.encoding)
|
|
381
385
|
@right = SymbolPattern.new engine, c, c, engine.right_bound
|
|
382
386
|
@specials << @right
|
|
383
387
|
end
|
|
384
388
|
end
|
|
385
|
-
@special_map =
|
|
389
|
+
@special_map = @specials.to_h { |s| [s.char, s] }
|
|
386
390
|
end
|
|
387
391
|
|
|
388
392
|
# confirm that all special patterns are legitimate regexen
|
|
@@ -409,7 +413,7 @@ module List
|
|
|
409
413
|
Regexp.new '(' + specials.map(&:var).map(&:to_s).join('|') + ')'
|
|
410
414
|
end
|
|
411
415
|
list = self.list.uniq.map do |w|
|
|
412
|
-
parts = w.split(rx).
|
|
416
|
+
parts = w.split(rx).reject(&:empty?)
|
|
413
417
|
e = parts.size - 1
|
|
414
418
|
(0..e).map do |i|
|
|
415
419
|
p = parts[i]
|
|
@@ -603,9 +607,10 @@ module List
|
|
|
603
607
|
dup_count.reverse.each do |repeats, seq, start, finish|
|
|
604
608
|
a = atomy? seq
|
|
605
609
|
sl = seq.length
|
|
610
|
+
# only condense if this makes things shorter
|
|
606
611
|
if ( a ? 0 : engine.wrap_size ) + 2 + repeats.to_s.length + sl < sl * repeats
|
|
607
612
|
copy[start...finish] = ( a ? seq : wrap(seq) ) + "{#{repeats}}"
|
|
608
|
-
return copy
|
|
613
|
+
return copy
|
|
609
614
|
end
|
|
610
615
|
end
|
|
611
616
|
end
|
|
@@ -614,9 +619,23 @@ module List
|
|
|
614
619
|
elements
|
|
615
620
|
end
|
|
616
621
|
|
|
617
|
-
# infer atomic patterns
|
|
622
|
+
# infer atomic patterns -- things that can safely be repeated without wrapping
|
|
618
623
|
def atomy?(s)
|
|
619
|
-
s.size == 1 ||
|
|
624
|
+
s.size == 1 || %r/
|
|
625
|
+
\A
|
|
626
|
+
(?>
|
|
627
|
+
\\\w # one of the canned character class expressions
|
|
628
|
+
| # OR
|
|
629
|
+
\[ # an ad hoc character class expression
|
|
630
|
+
(?>
|
|
631
|
+
[^\[\]\\]
|
|
632
|
+
|
|
|
633
|
+
\\.
|
|
634
|
+
)++
|
|
635
|
+
\]
|
|
636
|
+
)
|
|
637
|
+
\z
|
|
638
|
+
/x === s
|
|
620
639
|
end
|
|
621
640
|
|
|
622
641
|
# iterated repeat condensation
|
|
@@ -684,15 +703,15 @@ module List
|
|
|
684
703
|
else
|
|
685
704
|
rs = ranges(chars)
|
|
686
705
|
if rs.size == 1 && rs[0][0] == rs[0][1]
|
|
687
|
-
cc_quote rs[0][0].chr
|
|
706
|
+
cc_quote rs[0][0].chr(engine.encoding)
|
|
688
707
|
else
|
|
689
708
|
mid = rs.map do |s, e|
|
|
690
709
|
if s == e
|
|
691
|
-
cc_quote s.chr
|
|
710
|
+
cc_quote s.chr(engine.encoding)
|
|
692
711
|
elsif e == s + 1
|
|
693
|
-
"#{ cc_quote s.chr }#{ cc_quote e.chr }"
|
|
712
|
+
"#{ cc_quote s.chr(engine.encoding) }#{ cc_quote e.chr(engine.encoding) }"
|
|
694
713
|
else
|
|
695
|
-
"#{ cc_quote s.chr }-#{ cc_quote e.chr }"
|
|
714
|
+
"#{ cc_quote s.chr(engine.encoding) }-#{ cc_quote e.chr(engine.encoding) }"
|
|
696
715
|
end
|
|
697
716
|
end.join
|
|
698
717
|
end
|
|
@@ -709,6 +728,20 @@ module List
|
|
|
709
728
|
|
|
710
729
|
def cc_quote(c)
|
|
711
730
|
return Regexp.quote(c) if c =~ /\s/
|
|
731
|
+
code = c.ord
|
|
732
|
+
# Hex escape for control characters 1-8 for readability
|
|
733
|
+
if code >= 1 && code <= 8
|
|
734
|
+
return "\\x#{code.to_s(16).rjust(2, '0')}"
|
|
735
|
+
end
|
|
736
|
+
# Named escapes for other control characters
|
|
737
|
+
case code
|
|
738
|
+
when 9 then '\\t'
|
|
739
|
+
when 10 then '\\n'
|
|
740
|
+
when 11 then '\\v'
|
|
741
|
+
when 12 then '\\f'
|
|
742
|
+
when 13 then '\\r'
|
|
743
|
+
when 27 then '\\e'
|
|
744
|
+
end
|
|
712
745
|
case c
|
|
713
746
|
when '[' then '\['
|
|
714
747
|
when ']' then '\]'
|
|
@@ -739,9 +772,9 @@ module List
|
|
|
739
772
|
|
|
740
773
|
class Alternate < Node
|
|
741
774
|
|
|
742
|
-
def initialize(engine,
|
|
775
|
+
def initialize(engine, specializer, list)
|
|
743
776
|
super(engine, nil)
|
|
744
|
-
@children = list.group_by{ |s| s[0] }.values.map{ |ar| engine.tree( ar,
|
|
777
|
+
@children = list.group_by{ |s| s[0] }.values.map{ |ar| engine.tree( ar, specializer ) }
|
|
745
778
|
end
|
|
746
779
|
|
|
747
780
|
def convert
|
data/list_matcher.gemspec
CHANGED
|
@@ -9,17 +9,25 @@ Gem::Specification.new do |spec|
|
|
|
9
9
|
spec.authors = ["dfhoughton"]
|
|
10
10
|
spec.email = ["dfhoughton@gmail.com"]
|
|
11
11
|
spec.summary = %q{List::Matcher automates the generation of efficient regular expressions.}
|
|
12
|
-
spec.description =
|
|
12
|
+
spec.description = <<~END
|
|
13
|
+
List::Matcher automates the creation of regular expressions from lists, including lists of other regular
|
|
14
|
+
expressions. The expressions it generates from lists of strings are non-backtracking and compact.
|
|
15
|
+
END
|
|
13
16
|
spec.homepage = "https://github.com/dfhoughton/list_matcher"
|
|
14
17
|
spec.license = "MIT"
|
|
18
|
+
spec.metadata = {
|
|
19
|
+
"changelog_uri" => "https://github.com/dfhoughton/list_matcher/blob/master/CHANGELOG.md",
|
|
20
|
+
"source_code_uri" => "https://github.com/dfhoughton/list_matcher",
|
|
21
|
+
"bug_tracker_uri" => "https://github.com/dfhoughton/list_matcher/issues"
|
|
22
|
+
}
|
|
15
23
|
|
|
16
24
|
spec.files = `git ls-files -z`.split("\x0")
|
|
17
25
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
18
26
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
19
27
|
spec.require_paths = ["lib"]
|
|
20
|
-
spec.required_ruby_version = '>=
|
|
28
|
+
spec.required_ruby_version = '>= 3.0'
|
|
21
29
|
|
|
22
|
-
spec.add_development_dependency "bundler", "
|
|
23
|
-
spec.add_development_dependency "rake", "
|
|
24
|
-
spec.add_development_dependency "minitest"
|
|
30
|
+
spec.add_development_dependency "bundler", ">= 2.0"
|
|
31
|
+
spec.add_development_dependency "rake", ">= 13.0"
|
|
32
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
|
25
33
|
end
|
data/test/basic_test.rb
CHANGED
|
@@ -31,6 +31,14 @@ class BasicTest < Minitest::Test
|
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
33
|
|
|
34
|
+
def test_fat_chars
|
|
35
|
+
doublewides = (256...1000).map{ |i| i.chr(Encoding::UTF_8) }
|
|
36
|
+
rx = List::Matcher.rx doublewides
|
|
37
|
+
doublewides.each do |word|
|
|
38
|
+
assert rx === word, "can match #{word}"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
34
42
|
def test_word_chars_case_insensitive
|
|
35
43
|
word = (1..255).map(&:chr).select{ |c| /\w/ === c }
|
|
36
44
|
chars = word + ['+']
|
|
@@ -313,4 +321,88 @@ class BasicTest < Minitest::Test
|
|
|
313
321
|
def test_empty_list
|
|
314
322
|
assert_equal '(?!)', List::Matcher.pattern([])
|
|
315
323
|
end
|
|
324
|
+
|
|
325
|
+
# Unicode support tests (ported from list-matcher-js)
|
|
326
|
+
def test_emoji
|
|
327
|
+
emojis = ['😀', '😁', '😂']
|
|
328
|
+
rx = List::Matcher.rx emojis
|
|
329
|
+
emojis.each do |emoji|
|
|
330
|
+
assert rx === emoji, "can match emoji #{emoji}"
|
|
331
|
+
end
|
|
332
|
+
assert rx !~ '🤔', 'does not match other emoji'
|
|
333
|
+
end
|
|
334
|
+
|
|
335
|
+
def test_supplementary_plane_characters
|
|
336
|
+
# Deseret script characters (code points > 65535)
|
|
337
|
+
chars = ['𐐷', '𐐸', '𐐹']
|
|
338
|
+
rx = List::Matcher.rx chars
|
|
339
|
+
chars.each do |c|
|
|
340
|
+
assert rx === c, "can match supplementary plane char #{c}"
|
|
341
|
+
end
|
|
342
|
+
assert rx !~ '𐐺', 'does not match other supplementary plane char'
|
|
343
|
+
end
|
|
344
|
+
|
|
345
|
+
def test_mixed_ascii_and_emoji
|
|
346
|
+
words = ['cat', '😀', 'dog']
|
|
347
|
+
rx = List::Matcher.rx words
|
|
348
|
+
words.each do |w|
|
|
349
|
+
assert rx === w, "can match mixed word #{w}"
|
|
350
|
+
end
|
|
351
|
+
assert rx !~ 'bird', 'does not match other words'
|
|
352
|
+
end
|
|
353
|
+
|
|
354
|
+
def test_zero_width_characters
|
|
355
|
+
zwsp = "\u200b" # zero-width space
|
|
356
|
+
zwj = "\u200d" # zero-width joiner
|
|
357
|
+
zwnj = "\u200c" # zero-width non-joiner
|
|
358
|
+
rx = List::Matcher.rx [zwsp, zwj, zwnj]
|
|
359
|
+
assert rx === zwsp, 'matches zero-width space'
|
|
360
|
+
assert rx === zwj, 'matches zero-width joiner'
|
|
361
|
+
assert rx === zwnj, 'matches zero-width non-joiner'
|
|
362
|
+
end
|
|
363
|
+
|
|
364
|
+
def test_combined_unicode_nfc_vs_nfd
|
|
365
|
+
# é can be U+00E9 (single) or U+0065 + U+0301 (e + combining acute)
|
|
366
|
+
single = "\u00e9"
|
|
367
|
+
combined = "e\u0301"
|
|
368
|
+
rx = List::Matcher.rx [single, combined]
|
|
369
|
+
assert rx === single, 'matches NFC é'
|
|
370
|
+
assert rx === combined, 'matches NFD é'
|
|
371
|
+
end
|
|
372
|
+
|
|
373
|
+
def test_control_characters_1_to_8
|
|
374
|
+
# Control characters 1-8 should be escaped as hex
|
|
375
|
+
control_chars = (1..8).map { |i| i.chr(Encoding::UTF_8) }
|
|
376
|
+
rx = List::Matcher.rx control_chars
|
|
377
|
+
control_chars.each do |c|
|
|
378
|
+
assert rx === c, "can match control character #{c.ord}"
|
|
379
|
+
end
|
|
380
|
+
# Verify they're escaped as hex in the pattern
|
|
381
|
+
pattern = List::Matcher.pattern control_chars
|
|
382
|
+
assert pattern.include?('\\x01'), 'control char 1 should be hex escaped'
|
|
383
|
+
assert pattern.include?('\\x08'), 'control char 8 should be hex escaped'
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
def test_control_characters_9_to_13
|
|
387
|
+
# Control characters 9-13 should be properly escaped
|
|
388
|
+
control_chars = [9, 10, 11, 12, 13].map { |i| i.chr(Encoding::UTF_8) }
|
|
389
|
+
rx = List::Matcher.rx control_chars
|
|
390
|
+
control_chars.each do |c|
|
|
391
|
+
assert rx === c, "can match control character #{c.ord}"
|
|
392
|
+
end
|
|
393
|
+
# The pattern may be optimized as a character class range [\t-\r]
|
|
394
|
+
# or individual escapes, both are valid
|
|
395
|
+
pattern = List::Matcher.pattern control_chars
|
|
396
|
+
# At minimum, it should contain tab and carriage return in some form
|
|
397
|
+
assert pattern.include?('\\t') || pattern.include?('[\\t'), 'tab should be escaped'
|
|
398
|
+
assert pattern.include?('\\r') || pattern.include?('\\r'), 'carriage return should be escaped'
|
|
399
|
+
end
|
|
400
|
+
|
|
401
|
+
def test_all_control_characters
|
|
402
|
+
control_chars = (0..31).map { |i| i.chr(Encoding::UTF_8) }
|
|
403
|
+
rx = List::Matcher.rx control_chars
|
|
404
|
+
control_chars.each do |c|
|
|
405
|
+
assert rx === c, "can match all control characters"
|
|
406
|
+
end
|
|
407
|
+
end
|
|
316
408
|
end
|
data/test/doc_test.rb
CHANGED
|
@@ -16,8 +16,8 @@ class DocTest < Minitest::Test
|
|
|
16
16
|
assert_equal '(?:[b-d]ad)', (m.pattern %w( cad bad dad ))
|
|
17
17
|
assert_equal '(?:cat(?:alog)?)', (m.pattern %w( cat catalog ))
|
|
18
18
|
assert_equal '(?:[4-9]|1\d?|2\d?|3[01]?)', (m.pattern (1..31).to_a)
|
|
19
|
-
assert_equal "(?:cat|dog)",
|
|
20
|
-
assert_equal /(?:cat|dog)/, (List::Matcher.rx %w( cat dog ))
|
|
19
|
+
assert_equal "(?:cat|dog)", (List::Matcher.pattern %w( cat dog ))
|
|
20
|
+
assert_equal %r/(?:cat|dog)/, (List::Matcher.rx %w( cat dog ))
|
|
21
21
|
m = List::Matcher.new normalize_whitespace: true, bound: true, case_insensitive: true, multiline: true, atomic: false, symbols: { num: '\d++' }
|
|
22
22
|
m2 = m.bud case_insensitive: false
|
|
23
23
|
assert !m2.case_insensitive
|
metadata
CHANGED
|
@@ -1,58 +1,59 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: list_matcher
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- dfhoughton
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 2026-11-28 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: bundler
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '2.0'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '2.0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
27
|
name: rake
|
|
29
28
|
requirement: !ruby/object:Gem::Requirement
|
|
30
29
|
requirements:
|
|
31
|
-
- - "
|
|
30
|
+
- - ">="
|
|
32
31
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
32
|
+
version: '13.0'
|
|
34
33
|
type: :development
|
|
35
34
|
prerelease: false
|
|
36
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
36
|
requirements:
|
|
38
|
-
- - "
|
|
37
|
+
- - ">="
|
|
39
38
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
39
|
+
version: '13.0'
|
|
41
40
|
- !ruby/object:Gem::Dependency
|
|
42
41
|
name: minitest
|
|
43
42
|
requirement: !ruby/object:Gem::Requirement
|
|
44
43
|
requirements:
|
|
45
|
-
- - "
|
|
44
|
+
- - "~>"
|
|
46
45
|
- !ruby/object:Gem::Version
|
|
47
|
-
version: '0'
|
|
46
|
+
version: '5.0'
|
|
48
47
|
type: :development
|
|
49
48
|
prerelease: false
|
|
50
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
51
50
|
requirements:
|
|
52
|
-
- - "
|
|
51
|
+
- - "~>"
|
|
53
52
|
- !ruby/object:Gem::Version
|
|
54
|
-
version: '0'
|
|
55
|
-
description:
|
|
53
|
+
version: '5.0'
|
|
54
|
+
description: |
|
|
55
|
+
List::Matcher automates the creation of regular expressions from lists, including lists of other regular
|
|
56
|
+
expressions. The expressions it generates from lists of strings are non-backtracking and compact.
|
|
56
57
|
email:
|
|
57
58
|
- dfhoughton@gmail.com
|
|
58
59
|
executables: []
|
|
@@ -60,6 +61,7 @@ extensions: []
|
|
|
60
61
|
extra_rdoc_files: []
|
|
61
62
|
files:
|
|
62
63
|
- ".gitignore"
|
|
64
|
+
- CHANGELOG.md
|
|
63
65
|
- Gemfile
|
|
64
66
|
- LICENSE.txt
|
|
65
67
|
- README.md
|
|
@@ -76,8 +78,10 @@ files:
|
|
|
76
78
|
homepage: https://github.com/dfhoughton/list_matcher
|
|
77
79
|
licenses:
|
|
78
80
|
- MIT
|
|
79
|
-
metadata:
|
|
80
|
-
|
|
81
|
+
metadata:
|
|
82
|
+
changelog_uri: https://github.com/dfhoughton/list_matcher/blob/master/CHANGELOG.md
|
|
83
|
+
source_code_uri: https://github.com/dfhoughton/list_matcher
|
|
84
|
+
bug_tracker_uri: https://github.com/dfhoughton/list_matcher/issues
|
|
81
85
|
rdoc_options: []
|
|
82
86
|
require_paths:
|
|
83
87
|
- lib
|
|
@@ -85,16 +89,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
85
89
|
requirements:
|
|
86
90
|
- - ">="
|
|
87
91
|
- !ruby/object:Gem::Version
|
|
88
|
-
version: '
|
|
92
|
+
version: '3.0'
|
|
89
93
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
90
94
|
requirements:
|
|
91
95
|
- - ">="
|
|
92
96
|
- !ruby/object:Gem::Version
|
|
93
97
|
version: '0'
|
|
94
98
|
requirements: []
|
|
95
|
-
|
|
96
|
-
rubygems_version: 2.4.8
|
|
97
|
-
signing_key:
|
|
99
|
+
rubygems_version: 3.6.2
|
|
98
100
|
specification_version: 4
|
|
99
101
|
summary: List::Matcher automates the generation of efficient regular expressions.
|
|
100
102
|
test_files:
|