to_regexp 0.0.3 → 0.1.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.
- data/Gemfile +3 -0
- data/History.txt +20 -0
- data/README.rdoc +13 -4
- data/lib/to_regexp.rb +31 -20
- data/lib/to_regexp/version.rb +1 -1
- data/test/test_to_regexp.rb +44 -0
- data/to_regexp.gemspec +0 -2
- metadata +25 -54
data/Gemfile
CHANGED
data/History.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
== 0.1.1 / 2012-02-22
|
2
|
+
|
3
|
+
* Bug fixes
|
4
|
+
|
5
|
+
* Fix edge case with Regexp.union(*ESCAPE_HTML.keys) seen in the wild with rack-1.2.5/lib/rack/utils.rb
|
6
|
+
|
7
|
+
== 0.1.0 / yanked!
|
8
|
+
|
9
|
+
* Enhancements
|
10
|
+
|
11
|
+
* New :literal option. For example, 'foo'.to_regexp(:literal => true, :ignore_case => true)
|
12
|
+
* Allow setting :ignore_case, :multiline, :extended as options passed to #to_regexp.
|
13
|
+
|
14
|
+
== 0.0.3 / 2011-04-27
|
15
|
+
|
16
|
+
* first production ready version!
|
17
|
+
|
18
|
+
== 0.0.2 / yanked!
|
19
|
+
|
20
|
+
== 0.0.1 / yanked!
|
data/README.rdoc
CHANGED
@@ -7,9 +7,18 @@ Basically a safe way to convert strings to regexps (with options).
|
|
7
7
|
new_way = str.to_regexp # provided by this gem
|
8
8
|
old_way == new_way # true
|
9
9
|
|
10
|
-
You can
|
10
|
+
You can also treat strings as literal regexps. These two are equivalent:
|
11
11
|
|
12
|
-
|
13
|
-
|
12
|
+
'/foo/'.to_regexp #=> /foo/
|
13
|
+
'foo'.to_regexp(:literal => true) #=> /foo/
|
14
|
+
|
15
|
+
If you need case insensitivity and you're using <tt>:literal</tt>, pass options like <tt>:ignore_case</tt>. These two are equivalent:
|
14
16
|
|
15
|
-
|
17
|
+
'/foo/i'.to_regexp #=> /foo/i
|
18
|
+
'foo'.to_regexp(:literal => true, :ignore_case => true) #=> /foo/i
|
19
|
+
|
20
|
+
You can get the options passed to <tt>Regexp.new</tt> with <tt>#as_regexp</tt>:
|
21
|
+
|
22
|
+
'/foo/'.to_regexp == Regexp.new('/foo/'.as_regexp) # true
|
23
|
+
|
24
|
+
Copyright 2012 Seamus Abshere
|
data/lib/to_regexp.rb
CHANGED
@@ -12,35 +12,46 @@ module ToRegexp
|
|
12
12
|
'/' => '/',
|
13
13
|
}
|
14
14
|
|
15
|
-
def to_regexp
|
16
|
-
if args = as_regexp
|
15
|
+
def to_regexp(options = {})
|
16
|
+
if args = as_regexp(options)
|
17
17
|
::Regexp.new *args
|
18
18
|
end
|
19
19
|
end
|
20
20
|
|
21
|
-
def as_regexp
|
22
|
-
|
23
|
-
|
24
|
-
# no starting delimiter found
|
25
|
-
return
|
21
|
+
def as_regexp(options = {})
|
22
|
+
unless options.is_a?(::Hash)
|
23
|
+
raise ::ArgumentError, "[to_regexp] Options must be a Hash"
|
26
24
|
end
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
25
|
+
str = self.strip
|
26
|
+
|
27
|
+
if options[:literal] == true
|
28
|
+
content = ::Regexp.escape str
|
29
|
+
elsif delim_set = REGEXP_DELIMITERS.detect { |k, v| str.start_with?(k) }
|
30
|
+
delim_start, delim_end = delim_set.map { |delim| ::Regexp.escape delim }
|
31
|
+
/\A#{delim_start}(.*)#{delim_end}([^#{delim_end}]*)\z/u =~ str
|
32
|
+
content = $1
|
33
|
+
inline_options = $2
|
34
|
+
return unless content.is_a?(::String)
|
35
|
+
content.gsub! '\\/', '/'
|
36
|
+
if inline_options
|
37
|
+
options[:ignore_case] = true if inline_options.include?('i')
|
38
|
+
options[:multiline] = true if inline_options.include?('m')
|
39
|
+
options[:extended] = true if inline_options.include?('x')
|
40
|
+
# 'n', 'N' = none, 'e', 'E' = EUC, 's', 'S' = SJIS, 'u', 'U' = UTF-8
|
41
|
+
options[:lang] = inline_options.scan(/[nesu]/i).join.downcase
|
42
|
+
end
|
43
|
+
else
|
33
44
|
return
|
34
45
|
end
|
35
|
-
|
36
|
-
ignore_case = options
|
37
|
-
multiline = options
|
38
|
-
extended = options
|
39
|
-
|
40
|
-
lang = options.scan(/[nesu]/i).join.downcase
|
46
|
+
|
47
|
+
ignore_case = options[:ignore_case] ? ::Regexp::IGNORECASE : 0
|
48
|
+
multiline = options[:multiline] ? ::Regexp::MULTILINE : 0
|
49
|
+
extended = options[:extended] ? ::Regexp::EXTENDED : 0
|
50
|
+
lang = options[:lang] || ''
|
41
51
|
if ::RUBY_VERSION > '1.9' and lang.include?('u')
|
42
|
-
lang.
|
52
|
+
lang = lang.delete 'u'
|
43
53
|
end
|
54
|
+
|
44
55
|
if lang.empty?
|
45
56
|
[ content, (ignore_case|multiline|extended) ]
|
46
57
|
else
|
data/lib/to_regexp/version.rb
CHANGED
data/test/test_to_regexp.rb
CHANGED
@@ -77,4 +77,48 @@ class TestToRegexp < Test::Unit::TestCase
|
|
77
77
|
a = /foo/
|
78
78
|
assert_equal a, a.to_regexp
|
79
79
|
end
|
80
|
+
|
81
|
+
def test_011_ignore_case_option
|
82
|
+
assert_equal nil, '/(FOO)/'.to_regexp(:ignore_case => false).match('foo')
|
83
|
+
assert_equal nil, '/(FOO)/'.to_regexp(:ignore_case => false).match('foo')
|
84
|
+
assert_equal 'foo', '/(FOO)/'.to_regexp(:ignore_case => true).match('foo').captures[0]
|
85
|
+
assert_equal 'foo', '/(FOO)/i'.to_regexp(:ignore_case => true).match('foo').captures[0]
|
86
|
+
end
|
87
|
+
|
88
|
+
def test_012_literal_option
|
89
|
+
assert '/(FOO)/'.to_regexp(:literal => true).match('hello/(FOO)/there')
|
90
|
+
end
|
91
|
+
|
92
|
+
def test_013_combine_literal_and_ignore_case
|
93
|
+
assert '/(FOO)/'.to_regexp(:literal => true, :ignore_case => true).match('hello/(foo)/there')
|
94
|
+
|
95
|
+
# can't use inline options obviously
|
96
|
+
assert_equal nil, '/(FOO)/i'.to_regexp(:literal => true).match('hello/(foo)/there')
|
97
|
+
assert '/(FOO)/i'.to_regexp(:literal => true).match('hello/(FOO)/ithere')
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_014_try_convert
|
101
|
+
if RUBY_VERSION >= '1.9'
|
102
|
+
assert_equal /foo/i, Regexp.try_convert('/foo/i')
|
103
|
+
assert_equal //, Regexp.try_convert('//')
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# seen in the wild - from rack-1.2.5/lib/rack/utils.rb - converted to array to preserve order in 1.8.7
|
108
|
+
ESCAPE_HTML_KEYS = [
|
109
|
+
"&",
|
110
|
+
"<",
|
111
|
+
">",
|
112
|
+
"'",
|
113
|
+
'"',
|
114
|
+
"/"
|
115
|
+
]
|
116
|
+
def test_015_union
|
117
|
+
assert_equal /penzance/, Regexp.union('penzance')
|
118
|
+
assert_equal /skiing|sledding/, Regexp.union('skiing', 'sledding')
|
119
|
+
assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union(/dogs/, /cats/i)
|
120
|
+
assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union('/dogs/', /cats/i)
|
121
|
+
assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union(/dogs/, '/cats/i')
|
122
|
+
assert_equal %r{&|<|>|'|"|\/}.inspect, Regexp.union(*ESCAPE_HTML_KEYS).inspect
|
123
|
+
end
|
80
124
|
end
|
data/to_regexp.gemspec
CHANGED
@@ -18,6 +18,4 @@ Gem::Specification.new do |s|
|
|
18
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
19
|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
20
|
s.require_paths = ["lib"]
|
21
|
-
|
22
|
-
s.add_development_dependency 'ensure-encoding'
|
23
21
|
end
|
metadata
CHANGED
@@ -1,48 +1,27 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: to_regexp
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 3
|
10
|
-
version: 0.0.3
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Seamus Abshere
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
prerelease: false
|
23
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
-
none: false
|
25
|
-
requirements:
|
26
|
-
- - ">="
|
27
|
-
- !ruby/object:Gem::Version
|
28
|
-
hash: 3
|
29
|
-
segments:
|
30
|
-
- 0
|
31
|
-
version: "0"
|
32
|
-
type: :development
|
33
|
-
version_requirements: *id001
|
34
|
-
description: Provides String#to_regexp, for example if you want to make regexps out of a CSV you just imported.
|
35
|
-
email:
|
12
|
+
date: 2012-02-22 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Provides String#to_regexp, for example if you want to make regexps out
|
15
|
+
of a CSV you just imported.
|
16
|
+
email:
|
36
17
|
- seamus@abshere.net
|
37
18
|
executables: []
|
38
|
-
|
39
19
|
extensions: []
|
40
|
-
|
41
20
|
extra_rdoc_files: []
|
42
|
-
|
43
|
-
files:
|
21
|
+
files:
|
44
22
|
- .gitignore
|
45
23
|
- Gemfile
|
24
|
+
- History.txt
|
46
25
|
- README.rdoc
|
47
26
|
- Rakefile
|
48
27
|
- lib/to_regexp.rb
|
@@ -52,37 +31,29 @@ files:
|
|
52
31
|
- to_regexp.gemspec
|
53
32
|
homepage: https://github.com/seamusabshere/to_regexp
|
54
33
|
licenses: []
|
55
|
-
|
56
34
|
post_install_message:
|
57
35
|
rdoc_options: []
|
58
|
-
|
59
|
-
require_paths:
|
36
|
+
require_paths:
|
60
37
|
- lib
|
61
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
39
|
none: false
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
67
|
-
|
68
|
-
- 0
|
69
|
-
version: "0"
|
70
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
45
|
none: false
|
72
|
-
requirements:
|
73
|
-
- -
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
|
76
|
-
segments:
|
77
|
-
- 0
|
78
|
-
version: "0"
|
46
|
+
requirements:
|
47
|
+
- - ! '>='
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
79
50
|
requirements: []
|
80
|
-
|
81
51
|
rubyforge_project: to_regexp
|
82
|
-
rubygems_version: 1.
|
52
|
+
rubygems_version: 1.8.15
|
83
53
|
signing_key:
|
84
54
|
specification_version: 3
|
85
55
|
summary: Provides String#to_regexp
|
86
|
-
test_files:
|
56
|
+
test_files:
|
87
57
|
- test/helper.rb
|
88
58
|
- test/test_to_regexp.rb
|
59
|
+
has_rdoc:
|