Skip to content

Commit

Permalink
fix weird Regexp.union issue seen in the wild
Browse files Browse the repository at this point in the history
  • Loading branch information
seamusabshere committed Feb 22, 2012
1 parent c36bdd7 commit 0c24a7d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
8 changes: 7 additions & 1 deletion History.txt
@@ -1,4 +1,10 @@
== 0.1.0 / 2012-02-22
== 0.1.1 / 2012-02-22

* Bug fixes

* Fix edge case with Regexp.union(*ESCAPE_HTML.keys) seen in the wild with rack-1.2.5/lib/rack/utils.rb

== 0.1.0 / yanked!

* Enhancements

Expand Down
3 changes: 2 additions & 1 deletion lib/to_regexp.rb
Expand Up @@ -31,14 +31,15 @@ def as_regexp(options = {})
/\A#{delim_start}(.*)#{delim_end}([^#{delim_end}]*)\z/u =~ str
content = $1
inline_options = $2
return unless content.is_a?(::String)
content.gsub! '\\/', '/'
if inline_options
options[:ignore_case] = true if inline_options.include?('i')
options[:multiline] = true if inline_options.include?('m')
options[:extended] = true if inline_options.include?('x')
# 'n', 'N' = none, 'e', 'E' = EUC, 's', 'S' = SJIS, 'u', 'U' = UTF-8
options[:lang] = inline_options.scan(/[nesu]/i).join.downcase
end
content.gsub! '\\/', '/'
else
return
end
Expand Down
2 changes: 1 addition & 1 deletion lib/to_regexp/version.rb
@@ -1,3 +1,3 @@
module ToRegexp
VERSION = "0.1.0"
VERSION = "0.1.1"
end
25 changes: 25 additions & 0 deletions test/test_to_regexp.rb
Expand Up @@ -96,4 +96,29 @@ def test_013_combine_literal_and_ignore_case
assert_equal nil, '/(FOO)/i'.to_regexp(:literal => true).match('hello/(foo)/there')
assert '/(FOO)/i'.to_regexp(:literal => true).match('hello/(FOO)/ithere')
end

def test_014_try_convert
if RUBY_VERSION >= '1.9'
assert_equal /foo/i, Regexp.try_convert('/foo/i')
assert_equal //, Regexp.try_convert('//')
end
end

# seen in the wild - from rack-1.2.5/lib/rack/utils.rb - converted to array to preserve order in 1.8.7
ESCAPE_HTML_KEYS = [
"&",
"<",
">",
"'",
'"',
"/"
]
def test_015_union
assert_equal /penzance/, Regexp.union('penzance')
assert_equal /skiing|sledding/, Regexp.union('skiing', 'sledding')
assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union(/dogs/, /cats/i)
assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union('/dogs/', /cats/i)
assert_equal /(?-mix:dogs)|(?i-mx:cats)/, Regexp.union(/dogs/, '/cats/i')
assert_equal %r{&|<|>|'|"|\/}.inspect, Regexp.union(*ESCAPE_HTML_KEYS).inspect
end
end

0 comments on commit 0c24a7d

Please sign in to comment.