Remove redundant checks for valid character regexp in ActiveSupport::Multibyte#clean and #verify.

[#3181 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
This commit is contained in:
Beau Harrington 2009-09-09 22:25:23 -07:00 committed by Jeremy Kemper
parent 5b8b41732f
commit 81d828a14c
1 changed files with 49 additions and 35 deletions

View File

@ -10,30 +10,44 @@ module ActiveSupport #:nodoc:
end end
end end
if 'string'.respond_to?(:valid_encoding?)
# Verifies the encoding of a string # Verifies the encoding of a string
def self.verify(string)
string.valid_encoding?
end
else
def self.verify(string) def self.verify(string)
if expression = valid_character if expression = valid_character
for c in string.split(//) for c in string.split(//)
return false unless valid_character.match(c) return false unless expression.match(c)
end end
end end
true true
end end
end
# Verifies the encoding of the string and raises an exception when it's not valid # Verifies the encoding of the string and raises an exception when it's not valid
def self.verify!(string) def self.verify!(string)
raise ActiveSupport::Multibyte::Handlers::EncodingError.new("Found characters with invalid encoding") unless verify(string) raise ActiveSupport::Multibyte::Handlers::EncodingError.new("Found characters with invalid encoding") unless verify(string)
end end
# Removes all invalid characters from the string if 'string'.respond_to?(:force_encoding)
# Removes all invalid characters from the string.
#
# Note: this method is a no-op in Ruby 1.9
def self.clean(string)
string
end
else
def self.clean(string) def self.clean(string)
if expression = valid_character if expression = valid_character
stripped = []; for c in string.split(//) stripped = []; for c in string.split(//)
stripped << c if valid_character.match(c) stripped << c if expression.match(c)
end; stripped.join end; stripped.join
else else
string string
end end
end end
end end
end
end end