range_regexp 0.0.1
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 +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +22 -0
- data/Rakefile +13 -0
- data/lib/range_regexp.rb +109 -0
- data/lib/range_regexp/version.rb +3 -0
- data/range_regexp.gemspec +23 -0
- data/test/converter_test.rb +18 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3f7d92b3ddc53072da1222a2df78805ad1c24f5b
|
4
|
+
data.tar.gz: 52adaa4497a5b323a2f99d97d7299b79ce40477d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70e02186b231d3b7fe78cc2d3ae1a1facb377eb7551611146c6a99c952b4a25fa96e796a47d739f57d8b29b2e4711f55c1c5fa5f71f0276a4dc5141f4999f24d
|
7
|
+
data.tar.gz: 684742d3c0ddc6bb14581cb23feed26414d451fbbc25e053760f0a54572bbc008e1b0ac9f03ee59bc64e2335e0f279bee7a92314653866615051005a022723e9
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Andrey Koleshko
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# Transform Ruby ranges to regexps
|
2
|
+
|
3
|
+
The solution to convert a Ruby `Range` to a `Regexp`
|
4
|
+
|
5
|
+
# Install
|
6
|
+
|
7
|
+
In the system:
|
8
|
+
|
9
|
+
`gem install range-regexp`
|
10
|
+
|
11
|
+
or add the gem to the `Gemfile`:
|
12
|
+
|
13
|
+
`gem 'range-regexp'`
|
14
|
+
|
15
|
+
and then run `bundle install`.
|
16
|
+
|
17
|
+
# Usage
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
converter = RangeRegexp::Converter.new(-9..9)
|
21
|
+
converter.convert # => /-[1-9]|\d/
|
22
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
desc 'Default: run unit tests.'
|
5
|
+
task default: :test
|
6
|
+
|
7
|
+
desc 'Run tests for the gem.'
|
8
|
+
Rake::TestTask.new(:test) do |t|
|
9
|
+
t.libs << 'lib'
|
10
|
+
t.libs << 'test'
|
11
|
+
t.pattern = 'test/**/*_test.rb'
|
12
|
+
t.verbose = true
|
13
|
+
end
|
data/lib/range_regexp.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'set'
|
2
|
+
require 'range_regexp/version'
|
3
|
+
|
4
|
+
module RangeRegexp
|
5
|
+
class Converter
|
6
|
+
def initialize(range)
|
7
|
+
@min = range.first
|
8
|
+
@max = range.last
|
9
|
+
|
10
|
+
@negative_subpatterns = []
|
11
|
+
@positive_subpatterns = []
|
12
|
+
|
13
|
+
init_negative_subpatterns
|
14
|
+
init_positive_subpatterns
|
15
|
+
end
|
16
|
+
|
17
|
+
def convert
|
18
|
+
@regexp ||= begin
|
19
|
+
negative_subpatterns_only = arrays_diff(@negative_subpatterns, @positive_subpatterns).map do |pattern|
|
20
|
+
"-#{pattern}"
|
21
|
+
end
|
22
|
+
positive_subpatterns_only = arrays_diff(@positive_subpatterns, @negative_subpatterns)
|
23
|
+
intersected_subpatterns = (@positive_subpatterns & @negative_subpatterns).map do |pattern|
|
24
|
+
"-?#{pattern}"
|
25
|
+
end
|
26
|
+
Regexp.new("#{(negative_subpatterns_only + intersected_subpatterns + positive_subpatterns_only).join('|')}")
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def arrays_diff(a, b)
|
33
|
+
a - (a & b)
|
34
|
+
end
|
35
|
+
|
36
|
+
def init_negative_subpatterns
|
37
|
+
if @min < 0
|
38
|
+
@negative_subpatterns = split_to_patterns(@max < 0 ? @max.abs : 1, @min.abs)
|
39
|
+
@min = 0
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def init_positive_subpatterns
|
44
|
+
if @max >= 0
|
45
|
+
@positive_subpatterns = split_to_patterns(@min, @max)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def split_to_patterns(min, max)
|
50
|
+
subpatterns = []
|
51
|
+
start = min
|
52
|
+
split_to_ranges(min, max).each do |stop|
|
53
|
+
subpatterns.push(range_to_pattern(start, stop))
|
54
|
+
start = stop + 1
|
55
|
+
end
|
56
|
+
subpatterns
|
57
|
+
end
|
58
|
+
|
59
|
+
def split_to_ranges(min, max)
|
60
|
+
stops = Set.new
|
61
|
+
stops.add(max)
|
62
|
+
|
63
|
+
nines_count = 1
|
64
|
+
stop = fill_by_nines(min, nines_count)
|
65
|
+
while min <= stop && stop < max
|
66
|
+
stops.add(stop)
|
67
|
+
nines_count += 1
|
68
|
+
stop = fill_by_nines(min, nines_count)
|
69
|
+
end
|
70
|
+
|
71
|
+
zeros_count = 1
|
72
|
+
stop = fill_by_zeros(max + 1, zeros_count) - 1
|
73
|
+
while min < stop && stop <= max
|
74
|
+
stops.add(stop)
|
75
|
+
zeros_count += 1
|
76
|
+
stop = fill_by_zeros(max + 1, zeros_count) - 1
|
77
|
+
end
|
78
|
+
stops.to_a.sort
|
79
|
+
end
|
80
|
+
|
81
|
+
def fill_by_nines(int, nines_count)
|
82
|
+
"#{int.to_s[0...-nines_count]}#{'9' * nines_count}".to_i
|
83
|
+
end
|
84
|
+
|
85
|
+
def fill_by_zeros(int, zeros_count)
|
86
|
+
int - int % 10 ** zeros_count
|
87
|
+
end
|
88
|
+
|
89
|
+
def range_to_pattern(start, stop)
|
90
|
+
pattern = ''
|
91
|
+
any_digit_count = 0
|
92
|
+
|
93
|
+
start.to_s.split('').zip(stop.to_s.split('')).each do |(start_digit, stop_digit)|
|
94
|
+
if start_digit == stop_digit
|
95
|
+
pattern += start_digit
|
96
|
+
elsif start_digit != '0' || stop_digit != '9'
|
97
|
+
pattern += "[#{start_digit}-#{stop_digit}]"
|
98
|
+
else
|
99
|
+
any_digit_count += 1
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
pattern += '\d' if any_digit_count > 0
|
104
|
+
pattern += "{#{any_digit_count}}" if any_digit_count > 1
|
105
|
+
|
106
|
+
pattern
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'range_regexp/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "range_regexp"
|
8
|
+
spec.version = RangeRegexp::VERSION
|
9
|
+
spec.authors = ["Andrey Koleshko"]
|
10
|
+
spec.email = ["ka8725@gmail.com"]
|
11
|
+
spec.summary = %q{Convert a Ruby range to a regexp.}
|
12
|
+
spec.description = %q{The solution to convert a Ruby range to a regexp.}
|
13
|
+
spec.homepage = ""
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
22
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require 'range_regexp'
|
3
|
+
|
4
|
+
describe RangeRegexp::Converter do
|
5
|
+
describe '#convert' do
|
6
|
+
def ensure_correct_convertation(range, regexp)
|
7
|
+
converter = RangeRegexp::Converter.new(range)
|
8
|
+
assert_equal converter.convert, regexp
|
9
|
+
end
|
10
|
+
|
11
|
+
it 'returns a range representation as a regexp' do
|
12
|
+
ensure_correct_convertation(-9..9, /-[1-9]|\d/)
|
13
|
+
ensure_correct_convertation(12..3456, /1[2-9]|[2-9]\d|[1-9]\d{2}|[1-2]\d{3}|3[0-3]\d{2}|34[0-4]\d|345[0-6]/)
|
14
|
+
ensure_correct_convertation(-2..0, /-[1-2]|0/)
|
15
|
+
ensure_correct_convertation(-3..1, /-[1-3]|[0-1]/)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: range_regexp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrey Koleshko
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-03-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
description: The solution to convert a Ruby range to a regexp.
|
42
|
+
email:
|
43
|
+
- ka8725@gmail.com
|
44
|
+
executables: []
|
45
|
+
extensions: []
|
46
|
+
extra_rdoc_files: []
|
47
|
+
files:
|
48
|
+
- ".gitignore"
|
49
|
+
- Gemfile
|
50
|
+
- LICENSE.txt
|
51
|
+
- README.md
|
52
|
+
- Rakefile
|
53
|
+
- lib/range_regexp.rb
|
54
|
+
- lib/range_regexp/version.rb
|
55
|
+
- range_regexp.gemspec
|
56
|
+
- test/converter_test.rb
|
57
|
+
homepage: ''
|
58
|
+
licenses:
|
59
|
+
- MIT
|
60
|
+
metadata: {}
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubyforge_project:
|
77
|
+
rubygems_version: 2.2.2
|
78
|
+
signing_key:
|
79
|
+
specification_version: 4
|
80
|
+
summary: Convert a Ruby range to a regexp.
|
81
|
+
test_files:
|
82
|
+
- test/converter_test.rb
|
83
|
+
has_rdoc:
|