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 +1 -1
- data/lib/regex_concat.rb +4 -31
- data/regex_concat.gemspec +1 -1
- metadata +1 -1
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.0
|
1
|
+
1.1.0
|
data/lib/regex_concat.rb
CHANGED
@@ -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
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
|
data/regex_concat.gemspec
CHANGED