forked from rails/rails
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove redundant checks for valid character regexp in ActiveSupport::…
…Multibyte#clean and #verify. [rails#3181 state:committed] Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
- Loading branch information
Showing
1 changed file
with
49 additions
and
35 deletions.
There are no files selected for viewing
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
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -1,39 +1,53 @@ | |||
module ActiveSupport #:nodoc: | module ActiveSupport #:nodoc: | ||
module Multibyte #:nodoc: | module Multibyte #:nodoc: | ||
# Returns a regular expression that matches valid characters in the current encoding | # Returns a regular expression that matches valid characters in the current encoding | ||
def self.valid_character | def self.valid_character | ||
case $KCODE | case $KCODE | ||
when 'UTF8' | when 'UTF8' | ||
VALID_CHARACTER['UTF-8'] | VALID_CHARACTER['UTF-8'] | ||
when 'SJIS' | when 'SJIS' | ||
VALID_CHARACTER['Shift_JIS'] | VALID_CHARACTER['Shift_JIS'] | ||
end | end | ||
end | end | ||
|
|
||
# Verifies the encoding of a string | if 'string'.respond_to?(:valid_encoding?) | ||
def self.verify(string) | # Verifies the encoding of a string | ||
if expression = valid_character | def self.verify(string) | ||
for c in string.split(//) | string.valid_encoding? | ||
return false unless valid_character.match(c) | end | ||
end | else | ||
end | def self.verify(string) | ||
true | if expression = valid_character | ||
end | 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 | # 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) | ||
def self.clean(string) | # Removes all invalid characters from the string. | ||
if expression = valid_character | # | ||
stripped = []; for c in string.split(//) | # Note: this method is a no-op in Ruby 1.9 | ||
stripped << c if valid_character.match(c) | def self.clean(string) | ||
end; stripped.join | string | ||
else | end | ||
string | else | ||
end | def self.clean(string) | ||
end | if expression = valid_character | ||
end | stripped = []; for c in string.split(//) | ||
end | stripped << c if expression.match(c) | ||
end; stripped.join | |||
else | |||
string | |||
end | |||
end | |||
end | |||
end | |||
end |