regexp-examples 0.4.2 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +5 -1
- data/README.md +2 -2
- data/coverage/coverage-badge.png +0 -0
- data/lib/regexp-examples/core_extensions/regexp/examples.rb +2 -2
- data/lib/regexp-examples/groups.rb +34 -3
- data/lib/regexp-examples/parser.rb +14 -8
- data/lib/regexp-examples/version.rb +1 -1
- data/spec/regexp-examples_spec.rb +8 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 460970b9b7691fe163a9334542ccd57c71241c2a
|
4
|
+
data.tar.gz: 0c4ce5df7f59e72af6de41b8538d72b52c40939d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5932e6ea64e3008dad054d67f906863244366324e28cf1d280a222966b77dc1bc816760d6af1d9f023518b40ccfbdd8ad9c634060d2811ca5d687e333604e268
|
7
|
+
data.tar.gz: fd2c136a9457aac2e953fe77c972925a6af9f420f025ca9f7500a8252636ebf1ba2b1104232d17719de5b1c53cf8e1a4316c52e789bed113b6589b8cf71c7dda
|
data/.travis.yml
CHANGED
@@ -2,4 +2,8 @@ language: ruby
|
|
2
2
|
rvm:
|
3
3
|
- 2.0.0-p598
|
4
4
|
- 2.1.5
|
5
|
-
-
|
5
|
+
- 2.2.0
|
6
|
+
# One (ruby 2.3-dev) test fails, due to a change of behaviour in Array#delete_if,
|
7
|
+
# but I don't know if this is intentional. I'll fix it once the behaviour change is documented.
|
8
|
+
# For now, I don't really care if 2.3-dev tests all pass.
|
9
|
+
# - ruby-head
|
data/README.md
CHANGED
@@ -45,9 +45,9 @@ For more detail on this, see [configuration options](#configuration-options).
|
|
45
45
|
|
46
46
|
## Bugs and Not-Yet-Supported syntax
|
47
47
|
|
48
|
-
*
|
49
|
-
* `/test/i.examples` will NOT include `"TEST"`
|
48
|
+
* Other options (besides ingnorecase), will currently just be ignored, for example:
|
50
49
|
* `/white space/x.examples` will not strip out the whitespace from the pattern, i.e. this incorrectly returns `["white space"]` rather than `["whitespace"]`
|
50
|
+
* `/./m.examples(max_group_results: 999)` will not include `"\n"`
|
51
51
|
|
52
52
|
* Nested character classes, and the use of set intersection ([See here](http://www.ruby-doc.org/core-2.2.0/Regexp.html#class-Regexp-label-Character+Classes) for the official documentation on this.) For example:
|
53
53
|
* `/[[abc]]/.examples` (which _should_ return `["a", "b", "c"]`)
|
data/coverage/coverage-badge.png
CHANGED
Binary file
|
@@ -1,9 +1,9 @@
|
|
1
1
|
module CoreExtensions
|
2
2
|
module Regexp
|
3
3
|
module Examples
|
4
|
-
def examples(
|
4
|
+
def examples(config_options={})
|
5
5
|
full_examples = RegexpExamples.map_results(
|
6
|
-
RegexpExamples::Parser.new(source, options).parse
|
6
|
+
RegexpExamples::Parser.new(source, options, config_options).parse
|
7
7
|
)
|
8
8
|
RegexpExamples::BackReferenceReplacer.new.substitute_backreferences(full_examples)
|
9
9
|
end
|
@@ -16,11 +16,32 @@ module RegexpExamples
|
|
16
16
|
def all_subgroups
|
17
17
|
[self, subgroups].flatten.reject { |subgroup| subgroup.group_id.nil? }
|
18
18
|
end
|
19
|
+
|
20
|
+
def swapcase
|
21
|
+
# Override to preserve subgroups
|
22
|
+
GroupResult.new(super.to_s, group_id, subgroups)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
module GroupWithOptions
|
27
|
+
attr_reader :options
|
28
|
+
def result
|
29
|
+
group_result = super
|
30
|
+
if options[:ignorecase]
|
31
|
+
group_result
|
32
|
+
.concat( group_result.map(&:swapcase) )
|
33
|
+
.uniq
|
34
|
+
else
|
35
|
+
group_result
|
36
|
+
end
|
37
|
+
end
|
19
38
|
end
|
20
39
|
|
21
40
|
class SingleCharGroup
|
22
|
-
|
41
|
+
prepend GroupWithOptions
|
42
|
+
def initialize(char, options)
|
23
43
|
@char = char
|
44
|
+
@options = options
|
24
45
|
end
|
25
46
|
def result
|
26
47
|
[GroupResult.new(@char)]
|
@@ -28,8 +49,10 @@ module RegexpExamples
|
|
28
49
|
end
|
29
50
|
|
30
51
|
class CharGroup
|
31
|
-
|
52
|
+
prepend GroupWithOptions
|
53
|
+
def initialize(chars, options)
|
32
54
|
@chars = chars
|
55
|
+
@options = options
|
33
56
|
if chars[0] == "^"
|
34
57
|
@negative = true
|
35
58
|
@chars = @chars[1..-1]
|
@@ -43,6 +66,7 @@ module RegexpExamples
|
|
43
66
|
|
44
67
|
def init_ranges
|
45
68
|
# save first and last "-" if present
|
69
|
+
|
46
70
|
first = nil
|
47
71
|
last = nil
|
48
72
|
first = @chars.shift if @chars.first == "-"
|
@@ -95,6 +119,11 @@ module RegexpExamples
|
|
95
119
|
end
|
96
120
|
|
97
121
|
class DotGroup
|
122
|
+
prepend GroupWithOptions
|
123
|
+
def initialize(options={})
|
124
|
+
@options = options
|
125
|
+
end
|
126
|
+
|
98
127
|
def result
|
99
128
|
CharSets::Any.map do |result|
|
100
129
|
GroupResult.new(result)
|
@@ -103,10 +132,12 @@ module RegexpExamples
|
|
103
132
|
end
|
104
133
|
|
105
134
|
class MultiGroup
|
135
|
+
prepend GroupWithOptions
|
106
136
|
attr_reader :group_id
|
107
|
-
def initialize(groups, group_id)
|
137
|
+
def initialize(groups, group_id, options)
|
108
138
|
@groups = groups
|
109
139
|
@group_id = group_id
|
140
|
+
@options = options
|
110
141
|
end
|
111
142
|
|
112
143
|
# Generates the result of each contained group
|
@@ -1,13 +1,14 @@
|
|
1
1
|
module RegexpExamples
|
2
2
|
class Parser
|
3
3
|
attr_reader :regexp_string
|
4
|
-
def initialize(regexp_string,
|
4
|
+
def initialize(regexp_string, regexp_options, config_options={})
|
5
5
|
@regexp_string = regexp_string
|
6
|
+
@ignorecase = ( regexp_options & Regexp::IGNORECASE == 1 )
|
6
7
|
@num_groups = 0
|
7
8
|
@current_position = 0
|
8
9
|
RegexpExamples::ResultCountLimiters.configure!(
|
9
|
-
|
10
|
-
|
10
|
+
config_options[:max_repeater_variance],
|
11
|
+
config_options[:max_group_results]
|
11
12
|
)
|
12
13
|
end
|
13
14
|
|
@@ -27,6 +28,10 @@ module RegexpExamples
|
|
27
28
|
|
28
29
|
private
|
29
30
|
|
31
|
+
def regexp_options
|
32
|
+
{ignorecase: @ignorecase}
|
33
|
+
end
|
34
|
+
|
30
35
|
def parse_group(repeaters)
|
31
36
|
case next_char
|
32
37
|
when '('
|
@@ -72,7 +77,8 @@ module RegexpExamples
|
|
72
77
|
group = CharGroup.new(
|
73
78
|
# Note: The `.dup` is important, as it prevents modifying the constant, in
|
74
79
|
# CharGroup#init_ranges (where the '-' is moved to the front)
|
75
|
-
BackslashCharMap[next_char].dup
|
80
|
+
BackslashCharMap[next_char].dup,
|
81
|
+
regexp_options
|
76
82
|
)
|
77
83
|
when rest_of_string =~ /\A(c|C-)(.)/ # Control character
|
78
84
|
@current_position += $1.length
|
@@ -147,7 +153,7 @@ module RegexpExamples
|
|
147
153
|
end
|
148
154
|
end
|
149
155
|
groups = parse
|
150
|
-
MultiGroup.new(groups, group_id)
|
156
|
+
MultiGroup.new(groups, group_id, regexp_options)
|
151
157
|
end
|
152
158
|
|
153
159
|
def parse_multi_end_group
|
@@ -175,11 +181,11 @@ module RegexpExamples
|
|
175
181
|
chars << next_char
|
176
182
|
@current_position += 1
|
177
183
|
end
|
178
|
-
CharGroup.new(chars)
|
184
|
+
CharGroup.new(chars, regexp_options)
|
179
185
|
end
|
180
186
|
|
181
187
|
def parse_dot_group
|
182
|
-
DotGroup.new
|
188
|
+
DotGroup.new(regexp_options)
|
183
189
|
end
|
184
190
|
|
185
191
|
def parse_or_group(left_repeaters)
|
@@ -190,7 +196,7 @@ module RegexpExamples
|
|
190
196
|
|
191
197
|
|
192
198
|
def parse_single_char_group(char)
|
193
|
-
SingleCharGroup.new(char)
|
199
|
+
SingleCharGroup.new(char, regexp_options)
|
194
200
|
end
|
195
201
|
|
196
202
|
def parse_backreference_group(match)
|
@@ -242,6 +242,7 @@ RSpec.describe Regexp, "#examples" do
|
|
242
242
|
# a{1}? should be equivalent to (?:a{1})?, i.e. NOT a "non-greedy quantifier"
|
243
243
|
it { expect(/a{1}?/.examples).to eq ["", "a"] }
|
244
244
|
end
|
245
|
+
|
245
246
|
context "max_repeater_variance option" do
|
246
247
|
it do
|
247
248
|
expect(/a+/.examples(max_repeater_variance: 5))
|
@@ -252,12 +253,19 @@ RSpec.describe Regexp, "#examples" do
|
|
252
253
|
.to eq %w(aaaa)
|
253
254
|
end
|
254
255
|
end
|
256
|
+
|
255
257
|
context "max_group_results option" do
|
256
258
|
it do
|
257
259
|
expect(/\d/.examples(max_group_results: 10))
|
258
260
|
.to eq %w(0 1 2 3 4 5 6 7 8 9)
|
259
261
|
end
|
260
262
|
end
|
263
|
+
|
264
|
+
context "case insensitive" do
|
265
|
+
it { expect(/ab/i.examples).to eq %w(ab aB Ab AB) }
|
266
|
+
it { expect(/a+/i.examples).to eq %w(a A aa aA Aa AA aaa aaA aAa aAA Aaa AaA AAa AAA) }
|
267
|
+
it { expect(/([ab])\1/i.examples).to eq %w(aa bb AA BB) }
|
268
|
+
end
|
261
269
|
end
|
262
270
|
|
263
271
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: regexp-examples
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tom Lord
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-02-
|
11
|
+
date: 2015-02-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|