match_all 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a13c0e6b3b6d777b25dced8a0972842db698c5f7f7f98287b6b3f81a5d5528f1
4
+ data.tar.gz: 9786191dffe30e03530459dc7189b9b419b976d5956c3c9146741d5eb82903ce
5
+ SHA512:
6
+ metadata.gz: 26ceab3cb39ff712f917d924a605cfb5e202fb9cb30d5cc7316c8561235e080488d618af6a3de89d6b6d0d957d2f70fa0d418a80abed116988adb1096a91e76a
7
+ data.tar.gz: 6162f0cd7fa74742f237a54578f895abfef7fe829de7a7910dfa33d197482cab01e2594a575ddee47e207446f84c0ca162f8e832a30c885bb4e3bf32f17ac483
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/testdouble/standard
3
+ ruby_version: 2.6
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2022-11-10
4
+
5
+ - Initial release
data/Gemfile ADDED
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in match_all.gemspec
6
+ gemspec
7
+
8
+ gem "rake", "~> 13.0"
9
+
10
+ group :development, :test do
11
+ gem "standard", "~> 1.3"
12
+ gem "rspec", "~> 3.2"
13
+ gem "pry", "~> 0.13.1"
14
+ end
data/Gemfile.lock ADDED
@@ -0,0 +1,69 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ match_all (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.2)
10
+ coderay (1.1.3)
11
+ diff-lcs (1.5.0)
12
+ json (2.6.2)
13
+ method_source (1.0.0)
14
+ parallel (1.22.1)
15
+ parser (3.1.2.1)
16
+ ast (~> 2.4.1)
17
+ pry (0.13.1)
18
+ coderay (~> 1.1)
19
+ method_source (~> 1.0)
20
+ rainbow (3.1.1)
21
+ rake (13.0.6)
22
+ regexp_parser (2.6.0)
23
+ rexml (3.2.5)
24
+ rspec (3.12.0)
25
+ rspec-core (~> 3.12.0)
26
+ rspec-expectations (~> 3.12.0)
27
+ rspec-mocks (~> 3.12.0)
28
+ rspec-core (3.12.0)
29
+ rspec-support (~> 3.12.0)
30
+ rspec-expectations (3.12.0)
31
+ diff-lcs (>= 1.2.0, < 2.0)
32
+ rspec-support (~> 3.12.0)
33
+ rspec-mocks (3.12.0)
34
+ diff-lcs (>= 1.2.0, < 2.0)
35
+ rspec-support (~> 3.12.0)
36
+ rspec-support (3.12.0)
37
+ rubocop (1.38.0)
38
+ json (~> 2.3)
39
+ parallel (~> 1.10)
40
+ parser (>= 3.1.2.1)
41
+ rainbow (>= 2.2.2, < 4.0)
42
+ regexp_parser (>= 1.8, < 3.0)
43
+ rexml (>= 3.2.5, < 4.0)
44
+ rubocop-ast (>= 1.23.0, < 2.0)
45
+ ruby-progressbar (~> 1.7)
46
+ unicode-display_width (>= 1.4.0, < 3.0)
47
+ rubocop-ast (1.23.0)
48
+ parser (>= 3.1.1.0)
49
+ rubocop-performance (1.15.0)
50
+ rubocop (>= 1.7.0, < 2.0)
51
+ rubocop-ast (>= 0.4.0)
52
+ ruby-progressbar (1.11.0)
53
+ standard (1.17.0)
54
+ rubocop (= 1.38.0)
55
+ rubocop-performance (= 1.15.0)
56
+ unicode-display_width (2.3.0)
57
+
58
+ PLATFORMS
59
+ x86_64-darwin-21
60
+
61
+ DEPENDENCIES
62
+ match_all!
63
+ pry (~> 0.13.1)
64
+ rake (~> 13.0)
65
+ rspec (~> 3.2)
66
+ standard (~> 1.3)
67
+
68
+ BUNDLED WITH
69
+ 2.3.14
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Jeff Lange
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # MatchAll
2
+
3
+ The String class's native `#match` method will only return the first instance of a
4
+ pattern match. This gem provides the `#match_all` method, which returns all instances
5
+ of a pattern match within a String as an array.
6
+
7
+ ## Installation
8
+
9
+ Rails:
10
+ ```
11
+ # in your Gemfile
12
+ gem "match_all"
13
+ ```
14
+
15
+ Otherwise:
16
+ ```
17
+ $ gem install match_all
18
+ ```
19
+
20
+ ## Usage
21
+
22
+ #### Given the test string:
23
+ ```
24
+ string = "My cat is asleep on the couch. Now the cat is playing."
25
+ ```
26
+
27
+ `#match` only returns the first match:
28
+
29
+ ```
30
+ string.match(/cat/)
31
+ => #<MatchData "cat">
32
+ ```
33
+
34
+ But `#match_all` finds _all_ of them:
35
+
36
+ ```
37
+ string.match_all(/cat/)
38
+ => [
39
+ [0] #<MatchData "cat">,
40
+ [1] #<MatchData "cat">
41
+ ]
42
+ ```
43
+
44
+ If the string contains "cat" 50 times, then `#match_all` will return an array with
45
+ all 50 of those matches.
46
+
47
+ Each match is an instance of `MatchData`. This is especially useful if, e.g. you want
48
+ to interrogate the matches to find out their starting/ending indexes within the
49
+ string, etc.
50
+
51
+ ## Development
52
+
53
+ This is a very simple gem, with 0 production dependencies (just `rspec` and `pry` as
54
+ development/test dependencies for convenience).
55
+
56
+ ## Contributing
57
+
58
+ The method already does what it's supposed to do, and I don't expect any more
59
+ development to be necessary. That being said, issues and PR's are welcome if
60
+ anyone notices anything that can be improved or added.
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MatchAll
4
+ VERSION = "0.1.0"
5
+ end
data/lib/match_all.rb ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "match_all/version"
4
+ require_relative "ruby_core_extensions/string/match_all"
5
+
6
+ module MatchAll
7
+ class Error < StandardError; end
8
+
9
+ include RubyCoreExtensions::String::MatchAll
10
+ end
@@ -0,0 +1,26 @@
1
+ module RubyCoreExtensions
2
+ module String
3
+ module MatchAll
4
+ def match_all(pattern)
5
+ raise ArgumentError, "must pass a Regexp pattern" unless pattern.is_a?(Regexp)
6
+
7
+ recursive_match(pattern)
8
+ end
9
+
10
+ private
11
+
12
+ def recursive_match(pattern, matches = [], prev_match = nil)
13
+ index = prev_match.nil? ? 0 : prev_match.offset(0)[1]
14
+
15
+ matching_item = match(pattern, index)
16
+ return matches unless matching_item
17
+
18
+ # prevent infinite matches
19
+ return matches if matching_item == prev_match
20
+
21
+ matches << matching_item
22
+ recursive_match(pattern, matches, matching_item)
23
+ end
24
+ end
25
+ end
26
+ end
data/sig/match_all.rbs ADDED
@@ -0,0 +1,4 @@
1
+ module MatchAll
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,111 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: match_all
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Jeff Lange
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2022-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '3.2'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '3.2'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.13.1
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.13.1
41
+ description: |
42
+ Ruby String's native #match method will only return the first instance of a
43
+ pattern match. This gem provides the #match_all method, returns all instances
44
+ of a pattern match in a String as an array.
45
+
46
+ EXAMPLES:
47
+ # Given the test string:
48
+ string = "My cat is asleep on the couch. Now the cat is playing."
49
+
50
+ # #match only returns the first match:
51
+
52
+ string.match(/cat/)
53
+ => #<MatchData "cat">
54
+
55
+ # However, I've found I often want to match _all_ instances of the pattern, and
56
+ # then e.g. iterate through them and do something with them. #match_all does that:
57
+
58
+ string.match_all(/cat/)
59
+ => [
60
+ [0] #<MatchData "cat">,
61
+ [1] #<MatchData "cat">
62
+ ]
63
+
64
+ This is especially useful if, e.g. you want to interrogate the matches to find
65
+ out their starting/ending indexes within the string, etc
66
+ email:
67
+ - jeffersonian.lange@gmail.com
68
+ executables: []
69
+ extensions: []
70
+ extra_rdoc_files: []
71
+ files:
72
+ - ".rspec"
73
+ - ".standard.yml"
74
+ - CHANGELOG.md
75
+ - Gemfile
76
+ - Gemfile.lock
77
+ - LICENSE.txt
78
+ - README.md
79
+ - Rakefile
80
+ - lib/match_all.rb
81
+ - lib/match_all/version.rb
82
+ - lib/ruby_core_extensions/string/match_all.rb
83
+ - sig/match_all.rbs
84
+ homepage: https://github.com/jeffdlange/match_all
85
+ licenses:
86
+ - MIT
87
+ metadata:
88
+ allowed_push_host: https://rubygems.org
89
+ homepage_uri: https://github.com/jeffdlange/match_all
90
+ source_code_uri: https://github.com/jeffdlange/match_all
91
+ changelog_uri: https://github.com/jeffdlange/match_all/blob/main/CHANGELOG.md
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: 2.6.0
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - ">="
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubygems_version: 3.3.25
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: 'Provides #match_all method to String class'
111
+ test_files: []