aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
authorSergey Nartimov <just.lest@gmail.com>2011-12-25 14:34:58 +0300
committerSergey Nartimov <just.lest@gmail.com>2011-12-25 14:34:58 +0300
commit1e9e88fcd335c7d5a99159d592c3e1b605510a16 (patch)
treed4ccf417d4ed7c999b574ba941cf9ecb68f7882c /activesupport
parent4c1701c0ca6ae77a8274f59460751d1b1d83ce1b (diff)
downloadrails-1e9e88fcd335c7d5a99159d592c3e1b605510a16.tar.gz
rails-1e9e88fcd335c7d5a99159d592c3e1b605510a16.tar.bz2
rails-1e9e88fcd335c7d5a99159d592c3e1b605510a16.zip
remove checks for encodings availability
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/lib/active_support/core_ext/string/access.rb112
-rw-r--r--activesupport/lib/active_support/json/encoding.rb8
-rw-r--r--activesupport/lib/active_support/multibyte/chars.rb4
-rw-r--r--activesupport/lib/active_support/multibyte/utils.rb55
-rw-r--r--activesupport/test/buffered_logger_test.rb12
-rw-r--r--activesupport/test/caching_test.rb40
-rw-r--r--activesupport/test/json/encoding_test.rb4
-rw-r--r--activesupport/test/multibyte_test_helpers.rb5
8 files changed, 64 insertions, 176 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
diff --git a/activesupport/test/buffered_logger_test.rb b/activesupport/test/buffered_logger_test.rb
index 1540e8df9b..b9891c74c8 100644
--- a/activesupport/test/buffered_logger_test.rb
+++ b/activesupport/test/buffered_logger_test.rb
@@ -32,9 +32,7 @@ class BufferedLoggerTest < Test::Unit::TestCase
logger.level = Logger::DEBUG
str = "\x80"
- if str.respond_to?(:force_encoding)
- str.force_encoding("ASCII-8BIT")
- end
+ str.force_encoding("ASCII-8BIT")
logger.add Logger::DEBUG, str
ensure
@@ -52,9 +50,7 @@ class BufferedLoggerTest < Test::Unit::TestCase
logger.level = Logger::DEBUG
str = "\x80"
- if str.respond_to?(:force_encoding)
- str.force_encoding("ASCII-8BIT")
- end
+ str.force_encoding("ASCII-8BIT")
logger.add Logger::DEBUG, str
ensure
@@ -124,9 +120,7 @@ class BufferedLoggerTest < Test::Unit::TestCase
@logger.info(BYTE_STRING)
assert @output.string.include?(UNICODE_STRING)
byte_string = @output.string.dup
- if byte_string.respond_to?(:force_encoding)
- byte_string.force_encoding("ASCII-8BIT")
- end
+ byte_string.force_encoding("ASCII-8BIT")
assert byte_string.include?(BYTE_STRING)
end
end
diff --git a/activesupport/test/caching_test.rb b/activesupport/test/caching_test.rb
index 5488070d8c..aa6fb14e7b 100644
--- a/activesupport/test/caching_test.rb
+++ b/activesupport/test/caching_test.rb
@@ -398,22 +398,9 @@ end
# The error is caused by charcter encodings that can't be compared with ASCII-8BIT regular expressions and by special
# characters like the umlaut in UTF-8.
module EncodedKeyCacheBehavior
- if defined?(Encoding)
- Encoding.list.each do |encoding|
- define_method "test_#{encoding.name.underscore}_encoded_values" do
- key = "foo".force_encoding(encoding)
- assert @cache.write(key, "1", :raw => true)
- assert_equal "1", @cache.read(key)
- assert_equal "1", @cache.fetch(key)
- assert @cache.delete(key)
- assert_equal "2", @cache.fetch(key, :raw => true) { "2" }
- assert_equal 3, @cache.increment(key)
- assert_equal 2, @cache.decrement(key)
- end
- end
-
- def test_common_utf8_values
- key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
+ Encoding.list.each do |encoding|
+ define_method "test_#{encoding.name.underscore}_encoded_values" do
+ key = "foo".force_encoding(encoding)
assert @cache.write(key, "1", :raw => true)
assert_equal "1", @cache.read(key)
assert_equal "1", @cache.fetch(key)
@@ -422,12 +409,23 @@ module EncodedKeyCacheBehavior
assert_equal 3, @cache.increment(key)
assert_equal 2, @cache.decrement(key)
end
+ end
- def test_retains_encoding
- key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
- assert @cache.write(key, "1", :raw => true)
- assert_equal Encoding::UTF_8, key.encoding
- end
+ def test_common_utf8_values
+ key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
+ assert @cache.write(key, "1", :raw => true)
+ assert_equal "1", @cache.read(key)
+ assert_equal "1", @cache.fetch(key)
+ assert @cache.delete(key)
+ assert_equal "2", @cache.fetch(key, :raw => true) { "2" }
+ assert_equal 3, @cache.increment(key)
+ assert_equal 2, @cache.decrement(key)
+ end
+
+ def test_retains_encoding
+ key = "\xC3\xBCmlaut".force_encoding(Encoding::UTF_8)
+ assert @cache.write(key, "1", :raw => true)
+ assert_equal Encoding::UTF_8, key.encoding
end
end
diff --git a/activesupport/test/json/encoding_test.rb b/activesupport/test/json/encoding_test.rb
index ad9a497515..a6435a763a 100644
--- a/activesupport/test/json/encoding_test.rb
+++ b/activesupport/test/json/encoding_test.rb
@@ -91,11 +91,11 @@ class TestJSONEncoding < Test::Unit::TestCase
def test_utf8_string_encoded_properly
result = ActiveSupport::JSON.encode('€2.99')
assert_equal '"\\u20ac2.99"', result
- assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding)
+ assert_equal(Encoding::UTF_8, result.encoding)
result = ActiveSupport::JSON.encode('✎☺')
assert_equal '"\\u270e\\u263a"', result
- assert_equal(Encoding::UTF_8, result.encoding) if result.respond_to?(:encoding)
+ assert_equal(Encoding::UTF_8, result.encoding)
end
def test_non_utf8_string_transcodes
diff --git a/activesupport/test/multibyte_test_helpers.rb b/activesupport/test/multibyte_test_helpers.rb
index 8839b75601..fdbe2f4350 100644
--- a/activesupport/test/multibyte_test_helpers.rb
+++ b/activesupport/test/multibyte_test_helpers.rb
@@ -3,10 +3,7 @@
module MultibyteTestHelpers
UNICODE_STRING = 'こにちわ'
ASCII_STRING = 'ohayo'
- BYTE_STRING = "\270\236\010\210\245"
- if BYTE_STRING.respond_to?(:force_encoding)
- BYTE_STRING.force_encoding("ASCII-8BIT")
- end
+ BYTE_STRING = "\270\236\010\210\245".force_encoding("ASCII-8BIT")
def chars(str)
ActiveSupport::Multibyte::Chars.new(str)