Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Remove redundant checks for valid character regexp in ActiveSupport::…

…Multibyte#clean and #verify.

[#3181 state:committed]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
  • Loading branch information...
commit b4a0d1b21bca69635e43601e721122bb84a9197e 1 parent 9af2823
@bohford bohford authored jeremy committed
Showing with 49 additions and 35 deletions.
  1. +49 −35 activesupport/lib/active_support/multibyte/utils.rb
View
84 activesupport/lib/active_support/multibyte/utils.rb
@@ -1,39 +1,53 @@
module ActiveSupport #:nodoc:
- module Multibyte #:nodoc:
- # Returns a regular expression that matches valid characters in the current encoding
- def self.valid_character
- case $KCODE
- when 'UTF8'
- VALID_CHARACTER['UTF-8']
- when 'SJIS'
- VALID_CHARACTER['Shift_JIS']
- end
- end
+ module Multibyte #:nodoc:
+ # Returns a regular expression that matches valid characters in the current encoding
+ def self.valid_character
+ case $KCODE
+ when 'UTF8'
+ VALID_CHARACTER['UTF-8']
+ when 'SJIS'
+ VALID_CHARACTER['Shift_JIS']
+ end
+ end
- # Verifies the encoding of a string
- def self.verify(string)
- if expression = valid_character
- for c in string.split(//)
- return false unless valid_character.match(c)
- end
- end
- true
- end
+ if 'string'.respond_to?(:valid_encoding?)
+ # Verifies the encoding of a string
+ def self.verify(string)
+ string.valid_encoding?
+ end
+ else
+ def self.verify(string)
+ if expression = valid_character
+ for c in string.split(//)
+ return false unless expression.match(c)
+ end
+ end
+ true
+ end
+ end
- # Verifies the encoding of the string and raises an exception when it's not valid
- def self.verify!(string)
- raise ActiveSupport::Multibyte::Handlers::EncodingError.new("Found characters with invalid encoding") unless verify(string)
- end
+ # Verifies the encoding of the string and raises an exception when it's not valid
+ def self.verify!(string)
+ raise ActiveSupport::Multibyte::Handlers::EncodingError.new("Found characters with invalid encoding") unless verify(string)
+ end
- # Removes all invalid characters from the string
- def self.clean(string)
- if expression = valid_character
- stripped = []; for c in string.split(//)
- stripped << c if valid_character.match(c)
- end; stripped.join
- else
- string
- end
- end
- end
-end
+ 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)
+ if expression = valid_character
+ stripped = []; for c in string.split(//)
+ stripped << c if expression.match(c)
+ end; stripped.join
+ else
+ string
+ end
+ end
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.