mug 0.3.1 → 0.4.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 +4 -4
- data/lib/mug.rb +2 -0
- data/lib/mug/matchdata.rb +2 -0
- data/lib/mug/matchdata/each.rb +65 -0
- data/lib/mug/matchdata/hash.rb +51 -0
- data/test/test-matchdata_each.rb +57 -0
- data/test/test-matchdata_hash.rb +47 -0
- metadata +10 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 17f7a48a6bfa945c6760d07cca504c9d368414b6
|
|
4
|
+
data.tar.gz: 3e6f798ad9030e7d8de51d805ea22092bed9af96
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b35026ab417cc017594e49fdbcb042ec9319cf5918026ba57ffb5b5f208b4e8a7222f9e99cdb8ffd09951d4985df7a913420d0ce64e0a6b441a7c6add7b2f55a
|
|
7
|
+
data.tar.gz: 210d8afdc5f5ac618eb25fe5a0e79a92dc09e124c761448d4e224f0ae4068038782ef9528b8d4372799211f7513adc949e21fdb1c7a06bec03aa9dc548fe34a1
|
data/lib/mug.rb
CHANGED
|
@@ -12,6 +12,8 @@ require_relative 'mug/hash/operations'
|
|
|
12
12
|
require_relative 'mug/iterator/for'
|
|
13
13
|
require_relative 'mug/iterator/method'
|
|
14
14
|
require_relative 'mug/loop-with'
|
|
15
|
+
require_relative 'mug/matchdata/each'
|
|
16
|
+
require_relative 'mug/matchdata/hash'
|
|
15
17
|
require_relative 'mug/maybe'
|
|
16
18
|
require_relative 'mug/negativity'
|
|
17
19
|
require_relative 'mug/rexproc'
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
|
|
2
|
+
class MatchData
|
|
3
|
+
|
|
4
|
+
# Iterates over each capture group in the MatchData object,
|
|
5
|
+
# including +$&+ (the entire matched string), yielding the
|
|
6
|
+
# captured string.
|
|
7
|
+
def each &b
|
|
8
|
+
return enum_for(:each) unless block_given?
|
|
9
|
+
to_a.each{|v| yield v }
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Iterates over each capture group in the MatchData object,
|
|
13
|
+
# yielding the capture position and captured string.
|
|
14
|
+
#
|
|
15
|
+
# The capture positions are either all Strings or all Integers,
|
|
16
|
+
# depending on whether the original Regexp had named capture
|
|
17
|
+
# groups or not.
|
|
18
|
+
def each_capture &b
|
|
19
|
+
return enum_for(:each_capture) unless block_given?
|
|
20
|
+
if names.empty?
|
|
21
|
+
captures.each_with_index{|v,i| yield i+1, v }
|
|
22
|
+
else
|
|
23
|
+
names.each{|n| yield n, self[n] }
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Iterates over each named capture group in the MatchData object,
|
|
28
|
+
# yielding the capture name and string.
|
|
29
|
+
def each_named_capture &b
|
|
30
|
+
return enum_for(:each_named_capture) unless block_given?
|
|
31
|
+
names.each{|n| yield n, self[n] }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Iterates over each positional capture group in the MatchData object,
|
|
35
|
+
# yielding the capture position and string.
|
|
36
|
+
#
|
|
37
|
+
# If +include_names+ is given and true, treats named captures
|
|
38
|
+
# as positional captures.
|
|
39
|
+
#
|
|
40
|
+
# WARNING: if mixing named and positional captures, no positional
|
|
41
|
+
# captures will be available using this method!
|
|
42
|
+
def each_positional_capture include_names: false, &b
|
|
43
|
+
return enum_for(:each_positional_capture, include_names: include_names) unless block_given?
|
|
44
|
+
return unless names.empty? || include_names
|
|
45
|
+
captures.each_with_index{|v,i| yield i+1, v }
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
=begin
|
|
51
|
+
Copyright (c) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
|
52
|
+
|
|
53
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
54
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
55
|
+
copyright notice and this permission notice appear in all copies.
|
|
56
|
+
|
|
57
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
58
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
59
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
60
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
61
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
62
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
63
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
64
|
+
=end
|
|
65
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
|
|
2
|
+
class MatchData
|
|
3
|
+
|
|
4
|
+
# Returns a Hash object of capture position => captured string.
|
|
5
|
+
#
|
|
6
|
+
# The capture positions are either all Strings or all Integers,
|
|
7
|
+
# depending on whether the original Regexp had named capture
|
|
8
|
+
# groups or not.
|
|
9
|
+
def to_h
|
|
10
|
+
if names.empty?
|
|
11
|
+
Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ]
|
|
12
|
+
else
|
|
13
|
+
Hash[ names.map{|n| [n, self[n]] } ]
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Returns a Hash object of capture name => captured string.
|
|
18
|
+
def named_captures
|
|
19
|
+
Hash[ names.map{|n| [n, self[n]] } ]
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Returns a Hash object of capture position => captured string.
|
|
23
|
+
#
|
|
24
|
+
# If +include_names+ is given and true, treats named captures
|
|
25
|
+
# as positional captures.
|
|
26
|
+
#
|
|
27
|
+
# WARNING: if mixing named and positional captures, no positional
|
|
28
|
+
# captures will be available using this method!
|
|
29
|
+
def positional_captures include_names: false
|
|
30
|
+
return {} unless names.empty? || include_names
|
|
31
|
+
Hash[ captures.each_with_index.map{|v,i| [i+1, v] } ]
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
=begin
|
|
37
|
+
Copyright (c) 2016, Matthew Kerwin <matthew@kerwin.net.au>
|
|
38
|
+
|
|
39
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
40
|
+
purpose with or without fee is hereby granted, provided that the above
|
|
41
|
+
copyright notice and this permission notice appear in all copies.
|
|
42
|
+
|
|
43
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
44
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
45
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
46
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
47
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
48
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
49
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
50
|
+
=end
|
|
51
|
+
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
$VERBOSE = true
|
|
3
|
+
|
|
4
|
+
require_relative '../lib/mug/matchdata/each'
|
|
5
|
+
class Test_matchdata_each < Test::Unit::TestCase
|
|
6
|
+
def test__matchdata__each
|
|
7
|
+
str = 'a12b'
|
|
8
|
+
[
|
|
9
|
+
[/( \d)( \d)/x.match(str), %w[ 12 1 2 ]],
|
|
10
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), %w[ 12 1 2 ]],
|
|
11
|
+
[/( \d)(?<y>\d)/x.match(str), %w[ 12 2 ]], # ):
|
|
12
|
+
].each do |m, x|
|
|
13
|
+
assert_equal( x, m.each.to_a )
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
def test__matchdata__each_capture
|
|
17
|
+
str = 'a12b'
|
|
18
|
+
[
|
|
19
|
+
[/( \d)( \d)/x.match(str), [[ 1 ,'1'],[ 2 ,'2']]],
|
|
20
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [['x','1'],['y','2']]],
|
|
21
|
+
[/( \d)(?<y>\d)/x.match(str), [ ['y','2']]], # ):
|
|
22
|
+
].each do |m, x|
|
|
23
|
+
assert_equal( x, m.each_capture.to_a )
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
def test__matchdata__each_named_capture
|
|
27
|
+
str = 'a12b'
|
|
28
|
+
[
|
|
29
|
+
[/( \d)( \d)/x.match(str), [ ]],
|
|
30
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [['x','1'],['y','2']]],
|
|
31
|
+
[/( \d)(?<y>\d)/x.match(str), [ ['y','2']]],
|
|
32
|
+
].each do |m, x|
|
|
33
|
+
assert_equal( x, m.each_named_capture.to_a )
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
def test__matchdata__each_positional_capture
|
|
37
|
+
str = 'a12b'
|
|
38
|
+
[
|
|
39
|
+
[/( \d)( \d)/x.match(str), [[ 1, '1'],[ 2 ,'2']]],
|
|
40
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [ ]],
|
|
41
|
+
[/( \d)(?<y>\d)/x.match(str), [ ]], # D:
|
|
42
|
+
].each do |m, x|
|
|
43
|
+
assert_equal( x, m.each_positional_capture.to_a )
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
def test__matchdata__each_positional_capture2
|
|
47
|
+
str = 'a12b'
|
|
48
|
+
[
|
|
49
|
+
[/( \d)( \d)/x.match(str), [[ 1, '1'],[ 2 ,'2']]],
|
|
50
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), [[ 1, '1'],[ 2 ,'2']]],
|
|
51
|
+
[/( \d)(?<y>\d)/x.match(str), [[ 1, '2']]], # ):
|
|
52
|
+
].each do |m, x|
|
|
53
|
+
assert_equal( x, m.each_positional_capture(include_names: true).to_a )
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
require 'test/unit'
|
|
2
|
+
$VERBOSE = true
|
|
3
|
+
|
|
4
|
+
require_relative '../lib/mug/matchdata/hash'
|
|
5
|
+
class Test_matchdata_hash < Test::Unit::TestCase
|
|
6
|
+
def test__matchdata__to_h
|
|
7
|
+
str = 'a12b'
|
|
8
|
+
[
|
|
9
|
+
[/( \d)( \d)/x.match(str), { 1 =>'1', 2 =>'2'}],
|
|
10
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), {'x'=>'1', 'y'=>'2'}],
|
|
11
|
+
[/( \d)(?<y>\d)/x.match(str), { 'y'=>'2'}], # ):
|
|
12
|
+
].each do |m, x|
|
|
13
|
+
assert_equal( x, m.to_h )
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
def test__matchdata__named_captures
|
|
17
|
+
str = 'a12b'
|
|
18
|
+
[
|
|
19
|
+
[/( \d)( \d)/x.match(str), { }],
|
|
20
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), {'x'=>'1', 'y'=>'2'}],
|
|
21
|
+
[/( \d)(?<y>\d)/x.match(str), { 'y'=>'2'}],
|
|
22
|
+
].each do |m, x|
|
|
23
|
+
assert_equal( x, m.named_captures )
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
def test__matchdata__positional_captures
|
|
27
|
+
str = 'a12b'
|
|
28
|
+
[
|
|
29
|
+
[/( \d)( \d)/x.match(str), { 1 =>'1', 2 =>'2'}],
|
|
30
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), { }],
|
|
31
|
+
[/( \d)(?<y>\d)/x.match(str), { }], # D:
|
|
32
|
+
].each do |m, x|
|
|
33
|
+
assert_equal( x, m.positional_captures )
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
def test__matchdata__positional_captures2
|
|
37
|
+
str = 'a12b'
|
|
38
|
+
[
|
|
39
|
+
[/( \d)( \d)/x.match(str), { 1 =>'1', 2 =>'2'}],
|
|
40
|
+
[/(?<x>\d)(?<y>\d)/x.match(str), { 1 =>'1', 2 =>'2'}],
|
|
41
|
+
[/( \d)(?<y>\d)/x.match(str), { 1 =>'2'}], # ):
|
|
42
|
+
].each do |m, x|
|
|
43
|
+
assert_equal( x, m.positional_captures(include_names: true) )
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: mug
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.4.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matthew Kerwin
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2016-01-19 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
13
|
description: |
|
|
14
14
|
== MUG: Matty's Ultimate Gem
|
|
@@ -42,6 +42,9 @@ files:
|
|
|
42
42
|
- lib/mug/iterator/method.rb
|
|
43
43
|
- lib/mug/iterator_c.rb
|
|
44
44
|
- lib/mug/loop-with.rb
|
|
45
|
+
- lib/mug/matchdata.rb
|
|
46
|
+
- lib/mug/matchdata/each.rb
|
|
47
|
+
- lib/mug/matchdata/hash.rb
|
|
45
48
|
- lib/mug/maybe.rb
|
|
46
49
|
- lib/mug/negativity.rb
|
|
47
50
|
- lib/mug/rexproc.rb
|
|
@@ -62,6 +65,8 @@ files:
|
|
|
62
65
|
- test/test-iterator-for.rb
|
|
63
66
|
- test/test-iterator-method.rb
|
|
64
67
|
- test/test-loop-with.rb
|
|
68
|
+
- test/test-matchdata_each.rb
|
|
69
|
+
- test/test-matchdata_hash.rb
|
|
65
70
|
- test/test-maybe.rb
|
|
66
71
|
- test/test-negativity.rb
|
|
67
72
|
- test/test-rexproc.rb
|
|
@@ -88,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
88
93
|
version: '0'
|
|
89
94
|
requirements: []
|
|
90
95
|
rubyforge_project:
|
|
91
|
-
rubygems_version: 2.
|
|
96
|
+
rubygems_version: 2.5.1
|
|
92
97
|
signing_key:
|
|
93
98
|
specification_version: 4
|
|
94
99
|
summary: 'MUG: Matty''s Ultimate Gem'
|
|
@@ -96,6 +101,8 @@ test_files:
|
|
|
96
101
|
- test/test-clamp.rb
|
|
97
102
|
- test/test-rexproc.rb
|
|
98
103
|
- test/test-tau.rb
|
|
104
|
+
- test/test-matchdata_each.rb
|
|
105
|
+
- test/test-matchdata_hash.rb
|
|
99
106
|
- test/test-array-extend.rb
|
|
100
107
|
- test/test-iterator-for.rb
|
|
101
108
|
- test/test-top.rb
|