Created
August 9, 2013 08:06
-
-
Save anupnivargi/6191939 to your computer and use it in GitHub Desktop.
Regexp Union
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/kernel/common/regexp19.rb b/kernel/common/regexp19.rb | |
index 8496d78..4459921 100644 | |
--- a/kernel/common/regexp19.rb | |
+++ b/kernel/common/regexp19.rb | |
@@ -116,26 +116,6 @@ class Regexp | |
source.encoding | |
end | |
- def self.compatible_encodings(*patterns) | |
- ascii = [] | |
- non_ascii = [] | |
- first_pattern = patterns.first | |
- encoding = convert(first_pattern).encoding | |
- | |
- patterns.each do |pattern| | |
- pattern = convert(pattern) | |
- if pattern.encoding.ascii_compatible? | |
- ascii << pattern.encoding | |
- else | |
- non_ascii << pattern.encoding | |
- end | |
- end | |
- | |
- raise ArgumentError, "incompatible encodings: #{non_ascii.first} and #{ascii.first}" if !ascii.empty? && !non_ascii.empty? | |
- compatible?(ascii + non_ascii) | |
- encoding | |
- end | |
- | |
def self.convert(pattern) | |
return pattern if pattern.kind_of? Regexp | |
if pattern.kind_of? Array | |
@@ -145,7 +125,8 @@ class Regexp | |
end | |
end | |
- def self.compatible?(encodings) | |
+ def self.compatible?(*patterns) | |
+ encodings = patterns.collect{ |r| convert(r).encoding } | |
last_enc = encodings.pop | |
encodings.each do |encoding| | |
raise ArgumentError, "incompatible encodings: #{encoding} and #{last_enc}" unless Encoding.compatible?(last_enc, encoding) | |
@@ -168,7 +149,8 @@ class Regexp | |
return Regexp.new(Regexp.quote(StringValue(pat))) | |
end | |
else | |
- enc = compatible_encodings(*patterns) | |
+ compatible?(*patterns) | |
+ enc = convert(patterns.first).encoding | |
end | |
str = "".encode(enc) | |
@@ -183,6 +165,8 @@ class Regexp | |
end | |
Regexp.new(str) | |
+ rescue Encoding::CompatibilityError => e | |
+ raise ArgumentError, e.message | |
end | |
def self.escape(str) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment