Skip to content

Commit 53967e8

Browse files
author
Tom Lord
committed
implement globally configurable limiters
1 parent 44d6b31 commit 53967e8

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

lib/regexp-examples/constants.rb

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,28 @@
22
module RegexpExamples
33
# Configuration settings to limit the number/length of Regexp examples generated
44
class ResultCountLimiters
5+
class << self
6+
attr_accessor :max_repeater_variance, :max_group_results, :max_results_limit
7+
def with_configuration(max_repeater_variance: nil,
8+
max_group_results: nil,
9+
max_results_limit: nil)
10+
original_vals = [
11+
@max_repeater_variance,
12+
@max_group_results,
13+
@max_results_limit
14+
]
15+
16+
@max_repeater_variance = max_repeater_variance if max_repeater_variance
17+
@max_group_results = max_group_results if max_group_results
18+
@max_results_limit = max_results_limit if max_results_limit
19+
20+
result = yield
21+
22+
@max_repeater_variance, @max_group_results, @max_results_limit = *original_vals
23+
24+
result
25+
end
26+
end
527
# The maximum variance for any given repeater, to prevent a huge/infinite number of
628
# examples from being listed. For example, if @@max_repeater_variance = 2 then:
729
# .* is equivalent to .{0,2}
@@ -10,28 +32,21 @@ class ResultCountLimiters
1032
# .{,3} is equivalent to .{0,2}
1133
# .{3,8} is equivalent to .{3,5}
1234
MAX_REPEATER_VARIANCE_DEFAULT = 2
35+
@max_repeater_variance = MAX_REPEATER_VARIANCE_DEFAULT
1336

1437
# Maximum number of characters returned from a char set, to reduce output spam
1538
# For example, if @@max_group_results = 5 then:
1639
# \d is equivalent to [01234]
1740
# \w is equivalent to [abcde]
1841
MAX_GROUP_RESULTS_DEFAULT = 5
42+
@max_group_results = MAX_GROUP_RESULTS_DEFAULT
1943

2044
# Maximum number of results to be generated, for Regexp#examples
2145
# This is to prevent the system "freezing" when given instructions like:
2246
# /[ab]{30}/.examples
2347
# (Which would attempt to generate 2**30 == 1073741824 examples!!!)
2448
MAX_RESULTS_LIMIT_DEFAULT = 10_000
25-
class << self
26-
attr_reader :max_repeater_variance, :max_group_results, :max_results_limit
27-
def configure!(max_repeater_variance: nil,
28-
max_group_results: nil,
29-
max_results_limit: nil)
30-
@max_repeater_variance = (max_repeater_variance || MAX_REPEATER_VARIANCE_DEFAULT)
31-
@max_group_results = (max_group_results || MAX_GROUP_RESULTS_DEFAULT)
32-
@max_results_limit = (max_results_limit || MAX_RESULTS_LIMIT_DEFAULT)
33-
end
34-
end
49+
@max_results_limit = MAX_RESULTS_LIMIT_DEFAULT
3550
end
3651

3752
def self.max_repeater_variance

0 commit comments

Comments
 (0)