match_skeleton 1.0.1 → 1.0.2
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 +4 -4
- data/ChangeLog +6 -0
- data/lib/match_skeleton.rb +16 -16
- data/test/test_match_skeleton.rb +99 -31
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 52a61ea103053eb70d87995bab360db6e7557096
|
4
|
+
data.tar.gz: 28407978b5f043b2f180e7166da4b67d005ad64f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aea87a97397f62d2f4facb609a264c7f5e57720c54fd2d2914d8354566556d4fa7ea88b98fd097d99f93235f518f757be22b06bd9e9762e6779cc21554a74ea7
|
7
|
+
data.tar.gz: bb95f8a27ea564603d1a5f4438253cd11df97c4c6b28000a420ddcba89c0697790e3b7f391aef2fa7d8bc8cf57cd69c305a378a3c4e621577c6053805230291d
|
data/ChangeLog
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
+
-----
|
2
|
+
(Version: 1.0.2)
|
3
|
+
2016-05-19 Masa Sakano
|
4
|
+
* Fixed 2 bugs of (1) values_at (2) nil captures.
|
5
|
+
|
1
6
|
-----
|
2
7
|
(Version: 1.0.1)
|
3
8
|
2016-05-19 Masa Sakano
|
9
|
+
|
4
10
|
* Fixed a bug pre_match behaves incorrectly when the match starts at the beginning of the string.
|
5
11
|
|
6
12
|
-----
|
data/lib/match_skeleton.rb
CHANGED
@@ -42,10 +42,6 @@ class MatchSkeleton
|
|
42
42
|
names = md.names
|
43
43
|
ar_off = (0..(md.size-1)).map do |n|
|
44
44
|
ar = md.offset(n)
|
45
|
-
#if names[0]=='foo'
|
46
|
-
#print "DEBUG: ar=#{ar.inspect}\n"
|
47
|
-
#printf "DEBUG: n=(%d)range=%s\n", n, (ar.first...ar.last).inspect
|
48
|
-
#end
|
49
45
|
(ar.first...ar.last)
|
50
46
|
end
|
51
47
|
@offsets = {}
|
@@ -53,11 +49,7 @@ class MatchSkeleton
|
|
53
49
|
@offsets[ei] = ev
|
54
50
|
ej = ei - 1
|
55
51
|
@offsets[names[ej]] = ev if (ej >= 0 && names[ej])
|
56
|
-
#print "DEBUG: names=#{names[ei].inspect}\n"
|
57
|
-
#p names[ei], ev
|
58
|
-
#p md.offset(:foo)
|
59
52
|
end
|
60
|
-
#printf "DEBUG: offsets=%s\n", @offsets.inspect if !names.empty?
|
61
53
|
|
62
54
|
|
63
55
|
@pos_begin = pos_begin
|
@@ -86,7 +78,9 @@ class MatchSkeleton
|
|
86
78
|
if j
|
87
79
|
to_a[i, j]
|
88
80
|
elsif defined?(i.to_sym)
|
89
|
-
|
81
|
+
i = i.to_s
|
82
|
+
raise IndexError, sprintf("undefined group name reference: %s", i) if !names.include?(i)
|
83
|
+
offset2string(i)
|
90
84
|
else
|
91
85
|
to_a[i]
|
92
86
|
end
|
@@ -190,10 +184,9 @@ class MatchSkeleton
|
|
190
184
|
#
|
191
185
|
# @return [Array]
|
192
186
|
def to_a
|
193
|
-
#print 'DEBUG: '; p @offsets
|
194
187
|
indices = @offsets.keys.sort
|
195
188
|
indices.delete_if { |i| !defined?(i.divmod) }
|
196
|
-
indices.map { |i|
|
189
|
+
indices.map { |i| offset2string(i) }
|
197
190
|
end
|
198
191
|
|
199
192
|
# The same as {MatchData#to_s}
|
@@ -208,11 +201,18 @@ class MatchSkeleton
|
|
208
201
|
# @param *rest [Integer, Symbol, String]
|
209
202
|
# @return [Array]
|
210
203
|
def values_at(*rest)
|
204
|
+
locary = to_a
|
211
205
|
rest.map do |i|
|
212
|
-
|
213
|
-
# printf "DEBUG(%s): offsets=%s string=%s i=%s key=%s r=%s\n", __method__, @offsets.inspect,string.inspect,i.inspect,key.inspect,(string[key].inspect rescue 'nil') if !names.empty?
|
214
|
-
raise IndexError, sprintf("undefined group name reference: %s", i) if !key
|
215
|
-
string[key]
|
206
|
+
locary[i]
|
216
207
|
end
|
217
208
|
end
|
218
|
-
|
209
|
+
|
210
|
+
######################### private #########################
|
211
|
+
|
212
|
+
private
|
213
|
+
|
214
|
+
def offset2string(i)
|
215
|
+
k = @offsets[i]
|
216
|
+
(k.first && k.last) ? string[k] : nil
|
217
|
+
end
|
218
|
+
end # class MatchSkeleton
|
data/test/test_match_skeleton.rb
CHANGED
@@ -9,38 +9,36 @@ arlibdir = %w(match_skeleton)
|
|
9
9
|
arlibbase = [''] # match_skeleton.rb is read.
|
10
10
|
|
11
11
|
arlibbase.each do |elibbase|
|
12
|
-
arlibdir.each do |elibdir|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
12
|
+
arlibdir.each do |elibdir|
|
13
|
+
|
14
|
+
arAllPaths = []
|
15
|
+
er=nil
|
16
|
+
pathnow = nil
|
17
|
+
['../lib', 'lib', ''].each do |dirroot|
|
18
|
+
begin
|
19
|
+
# eg., %w(../lib/rangeary lib/rangeary rangeary)
|
20
|
+
s = [dirroot, elibdir, elibbase].join('/').sub(%r@^/@, '').sub(%r@/$@, '')
|
21
|
+
next if s.empty?
|
22
|
+
arAllPaths.push(s)
|
23
|
+
require s
|
24
|
+
pathnow = s
|
25
|
+
break
|
26
|
+
rescue LoadError => er
|
27
|
+
end
|
28
|
+
end # ['../lib', 'lib', ''].each do |dirroot|
|
29
|
+
|
30
|
+
if pathnow.nil?
|
31
|
+
warn "Warning: All the attempts to load the following files have failed. Abort..."
|
32
|
+
warn arAllPaths.inspect
|
33
|
+
warn " NOTE: It may be because a require statement in that file failed, rather than requiring the file itself. Check with
|
34
|
+
% ruby -r#{File.basename(elibbase)} -e p
|
35
|
+
or maybe add env RUBYLIB=$RUBYLIB:`pwd`"
|
36
|
+
# p $LOADED_FEATURES.grep(/#{Regexp.quote(File.basename(elibbase)+'.rb')}$/)
|
37
|
+
raise er
|
38
|
+
else
|
39
|
+
arlibrelpath.push pathnow
|
28
40
|
end
|
29
|
-
end #
|
30
|
-
|
31
|
-
if pathnow.nil?
|
32
|
-
warn "Warning: All the attempts to load the following files have failed. Abort..."
|
33
|
-
warn arAllPaths.inspect
|
34
|
-
warn " NOTE: It may be because a require statement in that file failed,
|
35
|
-
rather than requiring the file itself.
|
36
|
-
Check with % ruby -r#{File.basename(elibbase)} -e p
|
37
|
-
or maybe add env RUBYLIB=$RUBYLIB:`pwd`"
|
38
|
-
# p $LOADED_FEATURES.grep(/#{Regexp.quote(File.basename(elibbase)+'.rb')}$/)
|
39
|
-
raise er
|
40
|
-
else
|
41
|
-
arlibrelpath.push pathnow
|
42
|
-
end
|
43
|
-
end # arlibdir.each do |elibdir|
|
41
|
+
end # arlibdir.each do |elibdir|
|
44
42
|
end # arlibbase.each do |elibbase|
|
45
43
|
|
46
44
|
print "NOTE: Library relative paths: "; p arlibrelpath
|
@@ -206,6 +204,15 @@ class TestMatchSkeleton < Minitest::Test
|
|
206
204
|
assert_equal ["H", "X", "113"], ms1[1..3]
|
207
205
|
end
|
208
206
|
|
207
|
+
def test_squarebracket_range_nil
|
208
|
+
s = "efhi"
|
209
|
+
re = /(ef)(g)?(hi)/
|
210
|
+
md1 = re.match(s)
|
211
|
+
ms1 = MatchSkeleton.new(md1, s)
|
212
|
+
assert_equal md1[1..3], ms1[1..3]
|
213
|
+
assert_equal ["ef", nil, "hi"], ms1[1..3]
|
214
|
+
end
|
215
|
+
|
209
216
|
def test_squarebracket_name
|
210
217
|
s = "ccaaab"
|
211
218
|
md1 = /(?<foo>a+)b/.match(s) #=> #<MatchData "aaab" foo:"aaa">
|
@@ -218,6 +225,16 @@ class TestMatchSkeleton < Minitest::Test
|
|
218
225
|
assert_raises(IndexError) { ms1.begin(:naiyo) }
|
219
226
|
end
|
220
227
|
|
228
|
+
def test_squarebracket_name_nil01
|
229
|
+
s = "efhi"
|
230
|
+
re = /(?<foo>ef)(?<baa>g)?(?<baz>hi)/
|
231
|
+
md1 = re.match(s)
|
232
|
+
ms1 = MatchSkeleton.new(md1, s)
|
233
|
+
assert_equal md1['foo'], ms1['foo']
|
234
|
+
assert_equal md1['baa'], ms1['baa']
|
235
|
+
assert_equal md1[:baa], ms1[:baa]
|
236
|
+
end
|
237
|
+
|
221
238
|
def test_begin_end_int01
|
222
239
|
s ="THX1138."
|
223
240
|
md1 = /(.)(.)(\d+)(\d)/.match(s)
|
@@ -248,6 +265,17 @@ class TestMatchSkeleton < Minitest::Test
|
|
248
265
|
assert_raises(IndexError) { ms1.end(:naiyo) }
|
249
266
|
end
|
250
267
|
|
268
|
+
def test_begin_end_nil01
|
269
|
+
s = "efhi"
|
270
|
+
re = /(ef)(g)?(hi)/
|
271
|
+
md1 = re.match(s)
|
272
|
+
ms1 = MatchSkeleton.new(md1, s)
|
273
|
+
assert_equal md1.begin(2), ms1.begin(2)
|
274
|
+
assert_equal nil, ms1.begin(2)
|
275
|
+
assert_equal md1.end(2), ms1.end(2)
|
276
|
+
assert_equal nil, ms1.end(2)
|
277
|
+
end
|
278
|
+
|
251
279
|
def test_captures01
|
252
280
|
s ="THX1138."
|
253
281
|
md1 = /(.)(.)(\d+)(\d)/.match(s)
|
@@ -262,6 +290,14 @@ class TestMatchSkeleton < Minitest::Test
|
|
262
290
|
assert_equal '8', ms1.captures[3]
|
263
291
|
end
|
264
292
|
|
293
|
+
def test_captures_nil01
|
294
|
+
s = "efhi"
|
295
|
+
re = /(ef)(g)?(hi)/
|
296
|
+
md1 = re.match(s)
|
297
|
+
ms1 = MatchSkeleton.new(md1, s)
|
298
|
+
assert_equal md1.captures, ms1.captures
|
299
|
+
end
|
300
|
+
|
265
301
|
def test_inspect01
|
266
302
|
s ="foo"
|
267
303
|
md1 = /.$/.match(s)
|
@@ -293,6 +329,19 @@ class TestMatchSkeleton < Minitest::Test
|
|
293
329
|
assert_equal [6, 7], ms1.offset(4)
|
294
330
|
end
|
295
331
|
|
332
|
+
def test_offset_nil01
|
333
|
+
s = "efhi"
|
334
|
+
re = /(ef)(g)?(hi)/
|
335
|
+
md1 = re.match(s)
|
336
|
+
ms1 = MatchSkeleton.new(md1, s)
|
337
|
+
assert_equal md1.offset(0), ms1.offset(0)
|
338
|
+
assert_equal md1.offset(1), ms1.offset(1)
|
339
|
+
assert_equal [0, 2], ms1.offset(1)
|
340
|
+
assert_equal md1.offset(2), ms1.offset(2)
|
341
|
+
assert_equal [nil, nil], ms1.offset(2) # (nil..nil)==@offsets[2]
|
342
|
+
assert_equal md1.offset(3), ms1.offset(3)
|
343
|
+
end
|
344
|
+
|
296
345
|
def test_offset_name01
|
297
346
|
s = "hoge"
|
298
347
|
md1 = /(?<foo>.)(.)(?<bar>.)/.match(s)
|
@@ -354,6 +403,15 @@ class TestMatchSkeleton < Minitest::Test
|
|
354
403
|
assert_equal ["HX1138", "H", "X", "113", "8"], ms1.to_a
|
355
404
|
end
|
356
405
|
|
406
|
+
def test_to_a_nil01
|
407
|
+
s = "efhi"
|
408
|
+
re = /(ef)(g)?(hi)/
|
409
|
+
md1 = re.match(s)
|
410
|
+
ms1 = MatchSkeleton.new(md1, s)
|
411
|
+
assert_equal md1.to_a, ms1.to_a
|
412
|
+
assert_equal ["efhi", "ef", nil, "hi"], ms1.to_a
|
413
|
+
end
|
414
|
+
|
357
415
|
def test_to_s01
|
358
416
|
s = "THX1138."
|
359
417
|
md1 = /(.)(.)(\d+)(\d)/.match(s)
|
@@ -364,6 +422,16 @@ class TestMatchSkeleton < Minitest::Test
|
|
364
422
|
assert_equal ms1[0], ms1.to_s
|
365
423
|
end
|
366
424
|
|
425
|
+
def test_values_at_nil
|
426
|
+
s = "efhi"
|
427
|
+
re = /(ef)(g)?(hi)/
|
428
|
+
md1 = re.match(s)
|
429
|
+
ms1 = MatchSkeleton.new(md1, s)
|
430
|
+
assert_equal md1.values_at(2,3), ms1.values_at(2,3)
|
431
|
+
assert_equal [nil, "hi"], ms1.values_at(2,3)
|
432
|
+
assert_equal md1.values_at(2), ms1.values_at(2)
|
433
|
+
assert_raises(TypeError) { ms1.values_at('foo') }
|
434
|
+
end
|
367
435
|
end # class TestMatchSkeleton < Minitest::Test
|
368
436
|
|
369
437
|
|