diff options
author | Sergey Nartimov <just.lest@gmail.com> | 2011-12-25 14:34:58 +0300 |
---|---|---|
committer | Sergey Nartimov <just.lest@gmail.com> | 2011-12-25 14:34:58 +0300 |
commit | 1e9e88fcd335c7d5a99159d592c3e1b605510a16 (patch) | |
tree | d4ccf417d4ed7c999b574ba941cf9ecb68f7882c /activesupport/lib/active_support | |
parent | 4c1701c0ca6ae77a8274f59460751d1b1d83ce1b (diff) | |
download | rails-1e9e88fcd335c7d5a99159d592c3e1b605510a16.tar.gz rails-1e9e88fcd335c7d5a99159d592c3e1b605510a16.tar.bz2 rails-1e9e88fcd335c7d5a99159d592c3e1b605510a16.zip |
remove checks for encodings availability
Diffstat (limited to 'activesupport/lib/active_support')
4 files changed, 39 insertions, 140 deletions
diff --git a/activesupport/lib/active_support/core_ext/string/access.rb b/activesupport/lib/active_support/core_ext/string/access.rb index c0d5cdf2d5..9b5266c58c 100644 --- a/activesupport/lib/active_support/core_ext/string/access.rb +++ b/activesupport/lib/active_support/core_ext/string/access.rb @@ -1,99 +1,35 @@ require "active_support/multibyte" class String - unless '1.9'.respond_to?(:force_encoding) - # Returns the character at the +position+ treating the string as an array (where 0 is the first character). - # - # Examples: - # "hello".at(0) # => "h" - # "hello".at(4) # => "o" - # "hello".at(10) # => ERROR if < 1.9, nil in 1.9 - def at(position) - mb_chars[position, 1].to_s - end - - # Returns the remaining of the string from the +position+ treating the string as an array (where 0 is the first character). - # - # Examples: - # "hello".from(0) # => "hello" - # "hello".from(2) # => "llo" - # "hello".from(10) # => "" if < 1.9, nil in 1.9 - def from(position) - mb_chars[position..-1].to_s - end - - # Returns the beginning of the string up to the +position+ treating the string as an array (where 0 is the first character). - # - # Examples: - # "hello".to(0) # => "h" - # "hello".to(2) # => "hel" - # "hello".to(10) # => "hello" - def to(position) - mb_chars[0..position].to_s - end - - # Returns the first character of the string or the first +limit+ characters. - # - # Examples: - # "hello".first # => "h" - # "hello".first(2) # => "he" - # "hello".first(10) # => "hello" - def first(limit = 1) - if limit == 0 - '' - elsif limit >= size - self - else - mb_chars[0...limit].to_s - end - end - - # Returns the last character of the string or the last +limit+ characters. - # - # Examples: - # "hello".last # => "o" - # "hello".last(2) # => "lo" - # "hello".last(10) # => "hello" - def last(limit = 1) - if limit == 0 - '' - elsif limit >= size - self - else - mb_chars[(-limit)..-1].to_s - end - end - else - def at(position) - self[position] - end + def at(position) + self[position] + end - def from(position) - self[position..-1] - end + def from(position) + self[position..-1] + end - def to(position) - self[0..position] - end + def to(position) + self[0..position] + end - def first(limit = 1) - if limit == 0 - '' - elsif limit >= size - self - else - to(limit - 1) - end + def first(limit = 1) + if limit == 0 + '' + elsif limit >= size + self + else + to(limit - 1) end + end - def last(limit = 1) - if limit == 0 - '' - elsif limit >= size - self - else - from(-limit) - end + def last(limit = 1) + if limit == 0 + '' + elsif limit >= size + self + else + from(-limit) end end end diff --git a/activesupport/lib/active_support/json/encoding.rb b/activesupport/lib/active_support/json/encoding.rb index 925fa2a2c7..d7181035d3 100644 --- a/activesupport/lib/active_support/json/encoding.rb +++ b/activesupport/lib/active_support/json/encoding.rb @@ -119,9 +119,7 @@ module ActiveSupport end def escape(string) - if string.respond_to?(:force_encoding) - string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY) - end + string = string.encode(::Encoding::UTF_8, :undef => :replace).force_encoding(::Encoding::BINARY) json = string. gsub(escape_regex) { |s| ESCAPED_CHARS[s] }. gsub(/([\xC0-\xDF][\x80-\xBF]| @@ -130,7 +128,7 @@ module ActiveSupport s.unpack("U*").pack("n*").unpack("H*")[0].gsub(/.{4}/n, '\\\\u\&') } json = %("#{json}") - json.force_encoding(::Encoding::UTF_8) if json.respond_to?(:force_encoding) + json.force_encoding(::Encoding::UTF_8) json end end @@ -281,4 +279,4 @@ class DateTime strftime('%Y/%m/%d %H:%M:%S %z') end end -end
\ No newline at end of file +end diff --git a/activesupport/lib/active_support/multibyte/chars.rb b/activesupport/lib/active_support/multibyte/chars.rb index ba35b515f2..dcc176e93f 100644 --- a/activesupport/lib/active_support/multibyte/chars.rb +++ b/activesupport/lib/active_support/multibyte/chars.rb @@ -282,9 +282,7 @@ module ActiveSupport #:nodoc: return nil if byte_offset.nil? return 0 if @wrapped_string == '' - if @wrapped_string.respond_to?(:force_encoding) - @wrapped_string = @wrapped_string.dup.force_encoding(Encoding::ASCII_8BIT) - end + @wrapped_string = @wrapped_string.dup.force_encoding(Encoding::ASCII_8BIT) begin @wrapped_string[0...byte_offset].unpack('U*').length diff --git a/activesupport/lib/active_support/multibyte/utils.rb b/activesupport/lib/active_support/multibyte/utils.rb index 94b393cee2..bd6d4bad41 100644 --- a/activesupport/lib/active_support/multibyte/utils.rb +++ b/activesupport/lib/active_support/multibyte/utils.rb @@ -2,36 +2,14 @@ module ActiveSupport #:nodoc: module Multibyte #:nodoc: - if Kernel.const_defined?(:Encoding) - # Returns a regular expression that matches valid characters in the current encoding - def self.valid_character - VALID_CHARACTER[Encoding.default_external.to_s] - end - else - def self.valid_character - case $KCODE - when 'UTF8' - VALID_CHARACTER['UTF-8'] - when 'SJIS' - VALID_CHARACTER['Shift_JIS'] - end - end + # Returns a regular expression that matches valid characters in the current encoding + def self.valid_character + VALID_CHARACTER[Encoding.default_external.to_s] 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 - # Splits the string on character boundaries, which are determined based on $KCODE. - string.split(//).all? { |c| expression =~ c } - else - true - end - end + # Verifies the encoding of a string + def self.verify(string) + string.valid_encoding? end # Verifies the encoding of the string and raises an exception when it's not valid @@ -39,22 +17,11 @@ module ActiveSupport #:nodoc: raise EncodingError.new("Found characters with invalid encoding") unless verify(string) 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 - # Splits the string on character boundaries, which are determined based on $KCODE. - string.split(//).grep(expression).join - else - string - end - end + # Removes all invalid characters from the string. + # + # Note: this method is a no-op in Ruby 1.9 + def self.clean(string) + string end end end |