list_matcher 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bb59b6999ba70e7b3548710341764da658e460b3
4
- data.tar.gz: d0781937d67e22497b6c0005497c4e734a39582a
3
+ metadata.gz: 621a7ac90f1ec09b2b73fd4b233f16953c0e958e
4
+ data.tar.gz: e8512d9fb2232a7951ee8cad6608eb082675ece6
5
5
  SHA512:
6
- metadata.gz: 5e564cd5377d7018748417535b90f4fcd6347ed129ab8739590a038c8109039e1fd53b8f66ea740331cae0a2dbf48d9d55142c21f6c105106a496ff28e55f5b8
7
- data.tar.gz: 5357e73c0623e503befdf927d26c11d052105368f7ad12b9325c8e0b9eb192c4430c9e1aee920a6442e58862e94a54040e02cabdd454938b39fa5cecea40688e
6
+ metadata.gz: 8042b06b05edacc8ecceb31ac2057c14333c4a84c6c09f83f52858368e8733a7081dfc60c2afc7775fd3b62337c79b07afd96938fdf701fc9722a6d241d74a86
7
+ data.tar.gz: fcade9f49ae41f514cf8078766932068c3dc44d0cb0f53b5cedbad55c6f4e689828f8650828c58e3935d75015eab784f90edd6e286caae32eff76086e92ae475
data/README.md CHANGED
@@ -151,6 +151,22 @@ Each item should match the entire string compared against, so the boundary symbo
151
151
  List::Matcher.pattern %w(cat), bound: :string # "(?:\\Acat\\z)"
152
152
  ```
153
153
 
154
+ **NOTE** for each of these variants, `:word`, `:line`, and `:string` there are `left` and `right` subvarieties that
155
+ only append their boundary marker to that side of the word:
156
+
157
+ ```ruby
158
+ List::Matcher.pattern %w(cat), bound: :word_left # "(?:\\bcat)"
159
+ List::Matcher.pattern %w(cat), bound: :string_left # "(?:\\Acat)"
160
+ List::Matcher.pattern %w(cat), bound: :line_right # "(?:cat$)"
161
+ ```
162
+
163
+ Note also that `List::Matcher` will only append the boundary marker when it is appropriate according to
164
+ the test, so items for which the test fails will not receive a boundary marker of any sort:
165
+
166
+ ```ruby
167
+ List::Matcher.pattern %w( cat #@% ), bound: :word # "(?:\\#@%|\\bcat\\b)"
168
+ ```
169
+
154
170
  ```ruby
155
171
  bound: { test: /\d/, left: '(?<!\d)', right: '(?!\d)'}
156
172
  ```
@@ -163,6 +179,9 @@ List::Matcher.pattern (1...1000).to_a, bound: { test: /\d/, left: '(?<!\d)', rig
163
179
  # "(?:(?<!\\d)[1-9](?:\\d\\d?)?(?!\\d))"
164
180
  ```
165
181
 
182
+ As with the predefined boundaries -- `:word_left`, `:line_right`, `:string_left`, etc. -- you can bound items only at one
183
+ margin, in this case by providing only the `left:` or `right:` key-value pair.
184
+
166
185
  **NOTE** Because boundary tests cannot be applied to symbols, the bound option will give you strange results if you use it
167
186
  with a list any of whose items have a symbol at their leading or trailing margin.
168
187
 
@@ -1,3 +1,3 @@
1
1
  module ListMatcher
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
data/lib/list_matcher.rb CHANGED
@@ -49,23 +49,40 @@ module List
49
49
  @name = name
50
50
  end
51
51
  end
52
- if bound == :string
53
- @word_test = /./
54
- @left_bound = '\A'
55
- @right_bound = '\z'
56
- elsif bound == :line
57
- @word_test = /./
58
- @left_bound = '^'
59
- @right_bound = '$'
60
- elsif bound.is_a? Hash
61
- @word_test = bound[:test] || raise(SyntaxError.new('no boundary test provided'))
62
- @left_bound = bound[:left] || raise(SyntaxError.new('no left boundary expression provided'))
63
- @right_bound = bound[:right] || raise(SyntaxError.new('no right boundary expression provided'))
64
- elsif bound === true || bound == :word
52
+ case bound
53
+ when TrueClass
65
54
  @word_test = /\w/
66
55
  @left_bound = '\b'
67
56
  @right_bound = '\b'
68
- elsif !( bound === false )
57
+ when FalseClass
58
+ when Symbol
59
+ case bound
60
+ when :string, :string_left, :string_right
61
+ @word_test = /./
62
+ @left_bound = '\A'
63
+ @right_bound = '\z'
64
+ when :line, :line_left, :line_right
65
+ @word_test = /./
66
+ @left_bound = '^'
67
+ @right_bound = '$'
68
+ when :word, :word_left, :word_right
69
+ @word_test = /\w/
70
+ @left_bound = '\b'
71
+ @right_bound = '\b'
72
+ else
73
+ raise "unfamiliar value for :bound option: #{bound.inspect}"
74
+ end
75
+ if /_left/ === bound.to_s
76
+ @right_bound = nil
77
+ elsif /_right/ === bound.to_s
78
+ @left_bound = nil
79
+ end
80
+ when Hash
81
+ @word_test = bound[:test] || raise('no boundary test provided')
82
+ @left_bound = bound[:left]
83
+ @right_bound = bound[:right]
84
+ raise 'neither bound provided' unless @left_bound || @right_bound
85
+ else
69
86
  raise "unfamiliar value for :bound option: #{bound.inspect}"
70
87
  end
71
88
  symbols.keys.each do |k|
@@ -339,12 +356,16 @@ module List
339
356
  end
340
357
  end
341
358
  if engine.bound
342
- c = ( max += 1 ).chr
343
- @left = SpecialPattern.new engine, c, c, engine.left_bound
344
- @specials << @left
345
- c = ( max += 1 ).chr
346
- @right = SpecialPattern.new engine, c, c, engine.right_bound
347
- @specials << @right
359
+ if engine.left_bound
360
+ c = ( max += 1 ).chr
361
+ @left = SpecialPattern.new engine, c, c, engine.left_bound
362
+ @specials << @left
363
+ end
364
+ if engine.right_bound
365
+ c = ( max += 1 ).chr
366
+ @right = SpecialPattern.new engine, c, c, engine.right_bound
367
+ @specials << @right
368
+ end
348
369
  end
349
370
  end
350
371
 
@@ -384,11 +405,11 @@ module List
384
405
  p = specials.detect{ |sp| sp.var === p }
385
406
  special_map[p.char] = p
386
407
  if engine.bound
387
- if i == 0 && p.left
408
+ if i == 0 && engine.left_bound && p.left
388
409
  p = "#{left}#{p}" if t
389
410
  l = true
390
411
  end
391
- if i == e && p.right
412
+ if i == e && engine.right_bound && p.right
392
413
  p = "#{p}#{right}"
393
414
  r = true
394
415
  end
@@ -396,11 +417,11 @@ module List
396
417
  else
397
418
  p = p.downcase if engine.case_insensitive
398
419
  if engine.bound
399
- if i == 0 && engine.word_test === p[0]
420
+ if i == 0 && engine.left_bound && engine.word_test === p[0]
400
421
  p = "#{left}#{p}"
401
422
  l = true
402
423
  end
403
- if i == e && engine.word_test === p[-1]
424
+ if i == e && engine.right_bound && engine.word_test === p[-1]
404
425
  p = "#{p}#{right}"
405
426
  r = true
406
427
  end
data/test/basic_test.rb CHANGED
@@ -149,6 +149,16 @@ class BasicTest < Minitest::Test
149
149
  assert ' cat ' !~ rx, 'word boundaries do not suffice'
150
150
  end
151
151
 
152
+ def test_string_left_bound
153
+ rx = List::Matcher.pattern ['cat'], bound: :string_left
154
+ assert_equal '(?:\Acat)', rx
155
+ end
156
+
157
+ def test_string_right_bound
158
+ rx = List::Matcher.pattern ['cat'], bound: :string_right
159
+ assert_equal '(?:cat\z)', rx
160
+ end
161
+
152
162
  def test_line_bound
153
163
  rx = List::Matcher.pattern ['cat'], bound: :line
154
164
  assert_equal '(?:^cat$)', rx
@@ -158,6 +168,31 @@ class BasicTest < Minitest::Test
158
168
  assert ' cat ' !~ rx, 'word boundaries do not suffice'
159
169
  end
160
170
 
171
+ def test_line_left_bound
172
+ rx = List::Matcher.pattern ['cat'], bound: :line_left
173
+ assert_equal '(?:^cat)', rx
174
+ end
175
+
176
+ def test_line_right_bound
177
+ rx = List::Matcher.pattern ['cat'], bound: :line_right
178
+ assert_equal '(?:cat$)', rx
179
+ end
180
+
181
+ def test_word_bound
182
+ rx = List::Matcher.pattern %w( cat dog ), bound: :word
183
+ assert_equal '(?:\b(?:cat|dog)\b)', rx
184
+ end
185
+
186
+ def test_word_left_bound
187
+ rx = List::Matcher.pattern %w( cat dog ), bound: :word_left
188
+ assert_equal '(?:\b(?:cat|dog))', rx
189
+ end
190
+
191
+ def test_word_right_bound
192
+ rx = List::Matcher.pattern %w( cat dog ), bound: :word_right
193
+ assert_equal '(?:(?:cat|dog)\b)', rx
194
+ end
195
+
161
196
  def test_dup_atomic
162
197
  m = List::Matcher.new atomic: true
163
198
  rx = m.pattern %w( cat dog ), atomic: false
data/test/doc_test.rb CHANGED
@@ -99,6 +99,10 @@ class DocTest < Minitest::Test
99
99
  assert_nil date_20th_century.match('this is not actually a date')
100
100
  assert_equal "(?:\\#\\ is\\ sometimes\\ called\\ the\\ pound\\ symbol|cat\\ and\\ dog)", (List::Matcher.pattern [ 'cat and dog', '# is sometimes called the pound symbol' ])
101
101
  assert_equal "(?-x:cat and dog|# is sometimes called the pound symbol)", (List::Matcher.pattern [ 'cat and dog', '# is sometimes called the pound symbol' ], not_extended: true)
102
+ assert_equal "(?:\\bcat)", List::Matcher.pattern( %w(cat), bound: :word_left )
103
+ assert_equal "(?:\\Acat)", List::Matcher.pattern( %w(cat), bound: :string_left )
104
+ assert_equal "(?:cat$)", List::Matcher.pattern( %w(cat), bound: :line_right )
105
+ assert_equal "(?:\\#@%|\\bcat\\b)", List::Matcher.pattern( %w( cat #@% ), bound: :word )
102
106
  end
103
107
 
104
108
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: list_matcher
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - dfhoughton
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-18 00:00:00.000000000 Z
11
+ date: 2015-08-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler