caius-regex_concat 1.0.1 → 1.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.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.0.1
1
+ 1.1.0
@@ -7,37 +7,10 @@ module RegexpConcatenation
7
7
  # Regexp.concat(/foo/i, /bar/) # => /foobar/i
8
8
  # Regexp.concat(/foo/ix, /bar/m) # => /foobar/mix
9
9
  #
10
- def concat *regexes
11
- # We need somewhere to store the flags from each regex
12
- @flags = ""
13
- # Run through the regexes in a map!
14
- regexes.map! do |regex|
15
- # Separate out the flags and the actual regex itself
16
- flags, str = regex.to_s.scan(/(?:^\(\?(m?i?x?)-?m?i?x?:(.*?)\)$)/).first
17
- # Add the flags to our store
18
- @flags += flags
19
- # And map this element to just the regex str
20
- str
21
- end
22
- # Run through the flags, strip any duplicates
23
- # and then convert them into the numerical counterpart
24
- @flags = @flags.split("").uniq.inject(0) do |sum, flag|
25
- sum += case flag
26
- when "i"
27
- 1
28
- when "x"
29
- 2
30
- when "m"
31
- 4
32
- else
33
- 0
34
- end
35
- end
36
- # Get rid of the instance variable
37
- f = @flags; @flags = nil;
38
- # And return a Regexp object created from the regex strings
39
- # complete with any flags we need specified.
40
- Regexp.new(regexes.join(""), f)
10
+ def concat(*regexes)
11
+ allflags = regexes.inject(0) { |flags, re| flags | re.options }
12
+ patterns = regexes.map { |re| re.source }
13
+ Regexp.new(patterns.join, allflags)
41
14
  end
42
15
  end
43
16
 
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{regex_concat}
8
- s.version = "1.0.1"
8
+ s.version = "1.1.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Caius Durling"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: caius-regex_concat
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Caius Durling