mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
remove checks for encodings availability
This commit is contained in:
parent
4c1701c0ca
commit
1e9e88fcd3
25 changed files with 177 additions and 307 deletions
|
@ -107,7 +107,7 @@ class BaseTest < ActiveSupport::TestCase
|
|||
assert_equal(1, email.attachments.length)
|
||||
assert_equal('invoice.jpg', email.attachments[0].filename)
|
||||
expected = "\312\213\254\232)b"
|
||||
expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding)
|
||||
expected.force_encoding(Encoding::BINARY)
|
||||
assert_equal expected, email.attachments['invoice.jpg'].decoded
|
||||
end
|
||||
|
||||
|
@ -116,7 +116,7 @@ class BaseTest < ActiveSupport::TestCase
|
|||
assert_equal(1, email.attachments.length)
|
||||
assert_equal('invoice.jpg', email.attachments[0].filename)
|
||||
expected = "\312\213\254\232)b"
|
||||
expected.force_encoding(Encoding::BINARY) if '1.9'.respond_to?(:force_encoding)
|
||||
expected.force_encoding(Encoding::BINARY)
|
||||
assert_equal expected, email.attachments['invoice.jpg'].decoded
|
||||
end
|
||||
|
||||
|
|
|
@ -189,7 +189,7 @@ module ActionDispatch
|
|||
# variable is already set, wrap it in a StringIO.
|
||||
def body
|
||||
if raw_post = @env['RAW_POST_DATA']
|
||||
raw_post.force_encoding(Encoding::BINARY) if raw_post.respond_to?(:force_encoding)
|
||||
raw_post.force_encoding(Encoding::BINARY)
|
||||
StringIO.new(raw_post)
|
||||
else
|
||||
@env['rack.input']
|
||||
|
|
|
@ -30,7 +30,7 @@ module ActionDispatch
|
|||
|
||||
def generate_sid
|
||||
sid = SecureRandom.hex(16)
|
||||
sid.encode!('UTF-8') if sid.respond_to?(:encode!)
|
||||
sid.encode!('UTF-8')
|
||||
sid
|
||||
end
|
||||
|
||||
|
|
|
@ -181,7 +181,7 @@ module ActionView
|
|||
def with_output_buffer(buf = nil) #:nodoc:
|
||||
unless buf
|
||||
buf = ActionView::OutputBuffer.new
|
||||
buf.force_encoding(output_buffer.encoding) if output_buffer.respond_to?(:encoding) && buf.respond_to?(:force_encoding)
|
||||
buf.force_encoding(output_buffer.encoding) if output_buffer
|
||||
end
|
||||
self.output_buffer, old_buffer = buf, output_buffer
|
||||
yield
|
||||
|
|
|
@ -483,7 +483,7 @@ class LegacyRouteSetTests < Test::Unit::TestCase
|
|||
assert_equal({ :controller => "content", :action => 'show_page', :id => 'foo' }, rs.recognize_path("/page/foo"))
|
||||
|
||||
token = "\321\202\320\265\320\272\321\201\321\202" # 'text' in Russian
|
||||
token.force_encoding(Encoding::BINARY) if token.respond_to?(:force_encoding)
|
||||
token.force_encoding(Encoding::BINARY)
|
||||
escaped_token = CGI::escape(token)
|
||||
|
||||
assert_equal '/page/' + escaped_token, url_for(rs, { :controller => 'content', :action => 'show_page', :id => token })
|
||||
|
|
|
@ -61,7 +61,7 @@ class SendFileTest < ActionController::TestCase
|
|||
require 'stringio'
|
||||
output = StringIO.new
|
||||
output.binmode
|
||||
output.string.force_encoding(file_data.encoding) if output.string.respond_to?(:force_encoding)
|
||||
output.string.force_encoding(file_data.encoding)
|
||||
assert_nothing_raised { response.body_parts.each { |part| output << part.to_s } }
|
||||
assert_equal file_data, output.string
|
||||
end
|
||||
|
|
|
@ -673,7 +673,7 @@ XML
|
|||
path = "#{FILES_DIR}/#{filename}"
|
||||
content_type = 'image/png'
|
||||
expected = File.read(path)
|
||||
expected.force_encoding(Encoding::BINARY) if expected.respond_to?(:force_encoding)
|
||||
expected.force_encoding(Encoding::BINARY)
|
||||
|
||||
file = Rack::Test::UploadedFile.new(path, content_type)
|
||||
assert_equal filename, file.original_filename
|
||||
|
|
|
@ -89,7 +89,7 @@ class MultipartParamsParsingTest < ActionDispatch::IntegrationTest
|
|||
|
||||
# Rack doesn't handle multipart/mixed for us.
|
||||
files = params['files']
|
||||
files.force_encoding('ASCII-8BIT') if files.respond_to?(:force_encoding)
|
||||
files.force_encoding('ASCII-8BIT')
|
||||
assert_equal 19756, files.size
|
||||
end
|
||||
|
||||
|
|
|
@ -39,15 +39,13 @@ class OutputBufferTest < ActionController::TestCase
|
|||
assert_equal ['foo', 'bar'], body_parts
|
||||
end
|
||||
|
||||
if '1.9'.respond_to?(:force_encoding)
|
||||
test 'flushing preserves output buffer encoding' do
|
||||
original_buffer = ' '.force_encoding(Encoding::EUC_JP)
|
||||
@vc.output_buffer = original_buffer
|
||||
@vc.flush_output_buffer
|
||||
assert_equal ['foo', original_buffer], body_parts
|
||||
assert_not_equal original_buffer, output_buffer
|
||||
assert_equal Encoding::EUC_JP, output_buffer.encoding
|
||||
end
|
||||
test 'flushing preserves output buffer encoding' do
|
||||
original_buffer = ' '.force_encoding(Encoding::EUC_JP)
|
||||
@vc.output_buffer = original_buffer
|
||||
@vc.flush_output_buffer
|
||||
assert_equal ['foo', original_buffer], body_parts
|
||||
assert_not_equal original_buffer, output_buffer
|
||||
assert_equal Encoding::EUC_JP, output_buffer.encoding
|
||||
end
|
||||
|
||||
protected
|
||||
|
|
|
@ -410,51 +410,49 @@ class LazyViewRenderTest < ActiveSupport::TestCase
|
|||
GC.start
|
||||
end
|
||||
|
||||
if '1.9'.respond_to?(:force_encoding)
|
||||
def test_render_utf8_template_with_magic_comment
|
||||
with_external_encoding Encoding::ASCII_8BIT do
|
||||
result = @view.render(:file => "test/utf8_magic", :formats => [:html], :layouts => "layouts/yield")
|
||||
assert_equal Encoding::UTF_8, result.encoding
|
||||
assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_utf8_template_with_default_external_encoding
|
||||
with_external_encoding Encoding::UTF_8 do
|
||||
result = @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
|
||||
assert_equal Encoding::UTF_8, result.encoding
|
||||
assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_utf8_template_with_incompatible_external_encoding
|
||||
with_external_encoding Encoding::SHIFT_JIS do
|
||||
begin
|
||||
@view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
|
||||
flunk 'Should have raised incompatible encoding error'
|
||||
rescue ActionView::Template::Error => error
|
||||
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_utf8_template_with_partial_with_incompatible_encoding
|
||||
with_external_encoding Encoding::SHIFT_JIS do
|
||||
begin
|
||||
@view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield")
|
||||
flunk 'Should have raised incompatible encoding error'
|
||||
rescue ActionView::Template::Error => error
|
||||
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def with_external_encoding(encoding)
|
||||
old = Encoding.default_external
|
||||
silence_warnings { Encoding.default_external = encoding }
|
||||
yield
|
||||
ensure
|
||||
silence_warnings { Encoding.default_external = old }
|
||||
def test_render_utf8_template_with_magic_comment
|
||||
with_external_encoding Encoding::ASCII_8BIT do
|
||||
result = @view.render(:file => "test/utf8_magic", :formats => [:html], :layouts => "layouts/yield")
|
||||
assert_equal Encoding::UTF_8, result.encoding
|
||||
assert_equal "\nРусский \nтекст\n\nUTF-8\nUTF-8\nUTF-8\n", result
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_utf8_template_with_default_external_encoding
|
||||
with_external_encoding Encoding::UTF_8 do
|
||||
result = @view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
|
||||
assert_equal Encoding::UTF_8, result.encoding
|
||||
assert_equal "Русский текст\n\nUTF-8\nUTF-8\nUTF-8\n", result
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_utf8_template_with_incompatible_external_encoding
|
||||
with_external_encoding Encoding::SHIFT_JIS do
|
||||
begin
|
||||
@view.render(:file => "test/utf8", :formats => [:html], :layouts => "layouts/yield")
|
||||
flunk 'Should have raised incompatible encoding error'
|
||||
rescue ActionView::Template::Error => error
|
||||
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_render_utf8_template_with_partial_with_incompatible_encoding
|
||||
with_external_encoding Encoding::SHIFT_JIS do
|
||||
begin
|
||||
@view.render(:file => "test/utf8_magic_with_bare_partial", :formats => [:html], :layouts => "layouts/yield")
|
||||
flunk 'Should have raised incompatible encoding error'
|
||||
rescue ActionView::Template::Error => error
|
||||
assert_match 'Your template was not saved as valid Shift_JIS', error.original_exception.message
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def with_external_encoding(encoding)
|
||||
old = Encoding.default_external
|
||||
silence_warnings { Encoding.default_external = encoding }
|
||||
yield
|
||||
ensure
|
||||
silence_warnings { Encoding.default_external = old }
|
||||
end
|
||||
end
|
||||
|
|
|
@ -224,52 +224,48 @@ module ActiveRecord
|
|||
@statements.clear
|
||||
end
|
||||
|
||||
if "<3".respond_to?(:encode)
|
||||
# Taken from here:
|
||||
# https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
|
||||
# Author: TOMITA Masahiro <tommy@tmtm.org>
|
||||
ENCODINGS = {
|
||||
"armscii8" => nil,
|
||||
"ascii" => Encoding::US_ASCII,
|
||||
"big5" => Encoding::Big5,
|
||||
"binary" => Encoding::ASCII_8BIT,
|
||||
"cp1250" => Encoding::Windows_1250,
|
||||
"cp1251" => Encoding::Windows_1251,
|
||||
"cp1256" => Encoding::Windows_1256,
|
||||
"cp1257" => Encoding::Windows_1257,
|
||||
"cp850" => Encoding::CP850,
|
||||
"cp852" => Encoding::CP852,
|
||||
"cp866" => Encoding::IBM866,
|
||||
"cp932" => Encoding::Windows_31J,
|
||||
"dec8" => nil,
|
||||
"eucjpms" => Encoding::EucJP_ms,
|
||||
"euckr" => Encoding::EUC_KR,
|
||||
"gb2312" => Encoding::EUC_CN,
|
||||
"gbk" => Encoding::GBK,
|
||||
"geostd8" => nil,
|
||||
"greek" => Encoding::ISO_8859_7,
|
||||
"hebrew" => Encoding::ISO_8859_8,
|
||||
"hp8" => nil,
|
||||
"keybcs2" => nil,
|
||||
"koi8r" => Encoding::KOI8_R,
|
||||
"koi8u" => Encoding::KOI8_U,
|
||||
"latin1" => Encoding::ISO_8859_1,
|
||||
"latin2" => Encoding::ISO_8859_2,
|
||||
"latin5" => Encoding::ISO_8859_9,
|
||||
"latin7" => Encoding::ISO_8859_13,
|
||||
"macce" => Encoding::MacCentEuro,
|
||||
"macroman" => Encoding::MacRoman,
|
||||
"sjis" => Encoding::SHIFT_JIS,
|
||||
"swe7" => nil,
|
||||
"tis620" => Encoding::TIS_620,
|
||||
"ucs2" => Encoding::UTF_16BE,
|
||||
"ujis" => Encoding::EucJP_ms,
|
||||
"utf8" => Encoding::UTF_8,
|
||||
"utf8mb4" => Encoding::UTF_8,
|
||||
}
|
||||
else
|
||||
ENCODINGS = Hash.new { |h,k| h[k] = k }
|
||||
end
|
||||
# Taken from here:
|
||||
# https://github.com/tmtm/ruby-mysql/blob/master/lib/mysql/charset.rb
|
||||
# Author: TOMITA Masahiro <tommy@tmtm.org>
|
||||
ENCODINGS = {
|
||||
"armscii8" => nil,
|
||||
"ascii" => Encoding::US_ASCII,
|
||||
"big5" => Encoding::Big5,
|
||||
"binary" => Encoding::ASCII_8BIT,
|
||||
"cp1250" => Encoding::Windows_1250,
|
||||
"cp1251" => Encoding::Windows_1251,
|
||||
"cp1256" => Encoding::Windows_1256,
|
||||
"cp1257" => Encoding::Windows_1257,
|
||||
"cp850" => Encoding::CP850,
|
||||
"cp852" => Encoding::CP852,
|
||||
"cp866" => Encoding::IBM866,
|
||||
"cp932" => Encoding::Windows_31J,
|
||||
"dec8" => nil,
|
||||
"eucjpms" => Encoding::EucJP_ms,
|
||||
"euckr" => Encoding::EUC_KR,
|
||||
"gb2312" => Encoding::EUC_CN,
|
||||
"gbk" => Encoding::GBK,
|
||||
"geostd8" => nil,
|
||||
"greek" => Encoding::ISO_8859_7,
|
||||
"hebrew" => Encoding::ISO_8859_8,
|
||||
"hp8" => nil,
|
||||
"keybcs2" => nil,
|
||||
"koi8r" => Encoding::KOI8_R,
|
||||
"koi8u" => Encoding::KOI8_U,
|
||||
"latin1" => Encoding::ISO_8859_1,
|
||||
"latin2" => Encoding::ISO_8859_2,
|
||||
"latin5" => Encoding::ISO_8859_9,
|
||||
"latin7" => Encoding::ISO_8859_13,
|
||||
"macce" => Encoding::MacCentEuro,
|
||||
"macroman" => Encoding::MacRoman,
|
||||
"sjis" => Encoding::SHIFT_JIS,
|
||||
"swe7" => nil,
|
||||
"tis620" => Encoding::TIS_620,
|
||||
"ucs2" => Encoding::UTF_16BE,
|
||||
"ujis" => Encoding::EucJP_ms,
|
||||
"utf8" => Encoding::UTF_8,
|
||||
"utf8mb4" => Encoding::UTF_8,
|
||||
}
|
||||
|
||||
# Get the client encoding for this database
|
||||
def client_encoding
|
||||
|
|
|
@ -16,7 +16,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def binary_to_string(value)
|
||||
if value.respond_to?(:force_encoding) && value.encoding != Encoding::ASCII_8BIT
|
||||
if value.encoding != Encoding::ASCII_8BIT
|
||||
value = value.force_encoding(Encoding::ASCII_8BIT)
|
||||
end
|
||||
|
||||
|
|
|
@ -17,11 +17,7 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_client_encoding
|
||||
if "<3".respond_to?(:encoding)
|
||||
assert_equal Encoding::UTF_8, @conn.client_encoding
|
||||
else
|
||||
assert_equal 'utf8', @conn.client_encoding
|
||||
end
|
||||
assert_equal Encoding::UTF_8, @conn.client_encoding
|
||||
end
|
||||
|
||||
def test_exec_insert_number
|
||||
|
@ -41,13 +37,11 @@ module ActiveRecord
|
|||
|
||||
value = result.rows.last.last
|
||||
|
||||
if "<3".respond_to?(:encoding)
|
||||
# FIXME: this should probably be inside the mysql AR adapter?
|
||||
value.force_encoding(@conn.client_encoding)
|
||||
# FIXME: this should probably be inside the mysql AR adapter?
|
||||
value.force_encoding(@conn.client_encoding)
|
||||
|
||||
# The strings in this file are utf-8, so transcode to utf-8
|
||||
value.encode!(Encoding::UTF_8)
|
||||
end
|
||||
# The strings in this file are utf-8, so transcode to utf-8
|
||||
value.encode!(Encoding::UTF_8)
|
||||
|
||||
assert_equal str, value
|
||||
end
|
||||
|
|
|
@ -142,8 +142,6 @@ module ActiveRecord
|
|||
end
|
||||
|
||||
def test_quote_binary_column_escapes_it
|
||||
return unless "<3".respond_to?(:encode)
|
||||
|
||||
DualEncoding.connection.execute(<<-eosql)
|
||||
CREATE TABLE dual_encodings (
|
||||
id integer PRIMARY KEY AUTOINCREMENT,
|
||||
|
@ -157,9 +155,7 @@ module ActiveRecord
|
|||
assert_equal str, binary.data
|
||||
|
||||
ensure
|
||||
if "<3".respond_to?(:encode)
|
||||
DualEncoding.connection.drop_table('dual_encodings')
|
||||
end
|
||||
DualEncoding.connection.drop_table('dual_encodings')
|
||||
end
|
||||
|
||||
def test_execute
|
||||
|
|
|
@ -12,7 +12,7 @@ unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter)
|
|||
|
||||
def test_mixed_encoding
|
||||
str = "\x80"
|
||||
str.force_encoding('ASCII-8BIT') if str.respond_to?(:force_encoding)
|
||||
str.force_encoding('ASCII-8BIT')
|
||||
|
||||
binary = Binary.new :name => 'いただきます!', :data => str
|
||||
binary.save!
|
||||
|
@ -23,7 +23,7 @@ unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter)
|
|||
|
||||
# Mysql adapter doesn't properly encode things, so we have to do it
|
||||
if current_adapter?(:MysqlAdapter)
|
||||
name.force_encoding('UTF-8') if name.respond_to?(:force_encoding)
|
||||
name.force_encoding('UTF-8')
|
||||
end
|
||||
assert_equal 'いただきます!', name
|
||||
end
|
||||
|
@ -33,7 +33,7 @@ unless current_adapter?(:SybaseAdapter, :DB2Adapter, :FirebirdAdapter)
|
|||
|
||||
FIXTURES.each do |filename|
|
||||
data = File.read(ASSETS_ROOT + "/#{filename}")
|
||||
data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
|
||||
data.force_encoding('ASCII-8BIT')
|
||||
data.freeze
|
||||
|
||||
bin = Binary.new(:data => data)
|
||||
|
|
|
@ -213,7 +213,7 @@ class FixturesTest < ActiveRecord::TestCase
|
|||
|
||||
def test_binary_in_fixtures
|
||||
data = File.open(ASSETS_ROOT + "/flowers.jpg", 'rb') { |f| f.read }
|
||||
data.force_encoding('ASCII-8BIT') if data.respond_to?(:force_encoding)
|
||||
data.force_encoding('ASCII-8BIT')
|
||||
data.freeze
|
||||
assert_equal data, @flowers.data
|
||||
end
|
||||
|
|
|
@ -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
|
||||
def at(position)
|
||||
self[position]
|
||||
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
|
||||
def from(position)
|
||||
self[position..-1]
|
||||
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
|
||||
def to(position)
|
||||
self[0..position]
|
||||
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
|
||||
def first(limit = 1)
|
||||
if limit == 0
|
||||
''
|
||||
elsif limit >= size
|
||||
self
|
||||
else
|
||||
to(limit - 1)
|
||||
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 from(position)
|
||||
self[position..-1]
|
||||
end
|
||||
|
||||
def to(position)
|
||||
self[0..position]
|
||||
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
|
||||
|
|
|
@ -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
|
||||
end
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -137,7 +137,7 @@ module ApplicationTests
|
|||
end
|
||||
|
||||
test "assignment config.encoding to default_charset" do
|
||||
charset = "ruby".respond_to?(:force_encoding) ? 'Shift_JIS' : 'UTF8'
|
||||
charset = 'Shift_JIS'
|
||||
add_to_config "config.encoding = '#{charset}'"
|
||||
require "#{app_path}/config/environment"
|
||||
assert_equal charset, ActionDispatch::Response.default_charset
|
||||
|
|
Loading…
Reference in a new issue