Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
1e9e88fc
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
1e9e88fc
编写于
12月 25, 2011
作者:
S
Sergey Nartimov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
remove checks for encodings availability
上级
4c1701c0
变更
25
隐藏空白更改
内联
并排
Showing
25 changed file
with
168 addition
and
298 deletion
+168
-298
actionmailer/test/base_test.rb
actionmailer/test/base_test.rb
+2
-2
actionpack/lib/action_dispatch/http/request.rb
actionpack/lib/action_dispatch/http/request.rb
+1
-1
actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
.../lib/action_dispatch/middleware/session/abstract_store.rb
+1
-1
actionpack/lib/action_view/helpers/capture_helper.rb
actionpack/lib/action_view/helpers/capture_helper.rb
+1
-1
actionpack/test/controller/routing_test.rb
actionpack/test/controller/routing_test.rb
+1
-1
actionpack/test/controller/send_file_test.rb
actionpack/test/controller/send_file_test.rb
+1
-1
actionpack/test/controller/test_test.rb
actionpack/test/controller/test_test.rb
+1
-1
actionpack/test/dispatch/request/multipart_params_parsing_test.rb
...ck/test/dispatch/request/multipart_params_parsing_test.rb
+1
-1
actionpack/test/template/output_buffer_test.rb
actionpack/test/template/output_buffer_test.rb
+7
-9
actionpack/test/template/render_test.rb
actionpack/test/template/render_test.rb
+34
-36
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
...rd/lib/active_record/connection_adapters/mysql_adapter.rb
+42
-46
activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
...d/lib/active_record/connection_adapters/sqlite_adapter.rb
+1
-1
activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
+5
-11
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
...ecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
+1
-5
activerecord/test/cases/binary_test.rb
activerecord/test/cases/binary_test.rb
+3
-3
activerecord/test/cases/fixtures_test.rb
activerecord/test/cases/fixtures_test.rb
+1
-1
activesupport/lib/active_support/core_ext/string/access.rb
activesupport/lib/active_support/core_ext/string/access.rb
+24
-88
activesupport/lib/active_support/json/encoding.rb
activesupport/lib/active_support/json/encoding.rb
+3
-5
activesupport/lib/active_support/multibyte/chars.rb
activesupport/lib/active_support/multibyte/chars.rb
+1
-3
activesupport/lib/active_support/multibyte/utils.rb
activesupport/lib/active_support/multibyte/utils.rb
+11
-44
activesupport/test/buffered_logger_test.rb
activesupport/test/buffered_logger_test.rb
+3
-9
activesupport/test/caching_test.rb
activesupport/test/caching_test.rb
+19
-21
activesupport/test/json/encoding_test.rb
activesupport/test/json/encoding_test.rb
+2
-2
activesupport/test/multibyte_test_helpers.rb
activesupport/test/multibyte_test_helpers.rb
+1
-4
railties/test/application/initializers/frameworks_test.rb
railties/test/application/initializers/frameworks_test.rb
+1
-1
未找到文件。
actionmailer/test/base_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -107,7 +107,7 @@ def teardown
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 @@ def teardown
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
...
...
actionpack/lib/action_dispatch/http/request.rb
浏览文件 @
1e9e88fc
...
...
@@ -189,7 +189,7 @@ def raw_post
# 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'
]
...
...
actionpack/lib/action_dispatch/middleware/session/abstract_store.rb
浏览文件 @
1e9e88fc
...
...
@@ -30,7 +30,7 @@ def initialize(app, options = {})
def
generate_sid
sid
=
SecureRandom
.
hex
(
16
)
sid
.
encode!
(
'UTF-8'
)
if
sid
.
respond_to?
(
:encode!
)
sid
.
encode!
(
'UTF-8'
)
sid
end
...
...
actionpack/lib/action_view/helpers/capture_helper.rb
浏览文件 @
1e9e88fc
...
...
@@ -181,7 +181,7 @@ def content_for?(name)
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
...
...
actionpack/test/controller/routing_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -483,7 +483,7 @@ def test_route_with_text_default
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
})
...
...
actionpack/test/controller/send_file_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -61,7 +61,7 @@ def test_file_stream
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
...
...
actionpack/test/controller/test_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -673,7 +673,7 @@ def test_test_uploaded_file
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
...
...
actionpack/test/dispatch/request/multipart_params_parsing_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -89,7 +89,7 @@ def teardown
# 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
...
...
actionpack/test/template/output_buffer_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -39,15 +39,13 @@ def setup
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
...
...
actionpack/test/template/render_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -410,51 +410,49 @@ def teardown
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\n
UTF-8
\n
UTF-8
\n
UTF-8
\n
"
,
result
end
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\n
UTF-8
\n
UTF-8
\n
UTF-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\n
UTF-8
\n
UTF-8
\n
UTF-8
\n
"
,
result
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\n
UTF-8
\n
UTF-8
\n
UTF-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
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
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
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
activerecord/lib/active_record/connection_adapters/mysql_adapter.rb
浏览文件 @
1e9e88fc
...
...
@@ -224,52 +224,48 @@ def clear_cache!
@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
...
...
activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
浏览文件 @
1e9e88fc
...
...
@@ -16,7 +16,7 @@ def string_to_binary(value)
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
...
...
activerecord/test/cases/adapters/mysql/mysql_adapter_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -17,11 +17,7 @@ def setup
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 @@ def test_exec_insert_string
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
...
...
activerecord/test/cases/adapters/sqlite3/sqlite3_adapter_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -142,8 +142,6 @@ def test_exec_query_typecasts_bind_vals
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 @@ def test_quote_binary_column_escapes_it
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
...
...
activerecord/test/cases/binary_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -12,7 +12,7 @@ class BinaryTest < ActiveRecord::TestCase
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 @@ def test_mixed_encoding
# 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 @@ def test_load_save
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
)
...
...
activerecord/test/cases/fixtures_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -213,7 +213,7 @@ def test_subsubdir_file_with_arbitrary_name
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
...
...
activesupport/lib/active_support/core_ext/string/access.rb
浏览文件 @
1e9e88fc
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
activesupport/lib/active_support/json/encoding.rb
浏览文件 @
1e9e88fc
...
...
@@ -119,9 +119,7 @@ def escape_html_entities_in_json=(value)
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 @@ def escape(string)
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 @@ def as_json(options = nil) #:nodoc:
strftime
(
'%Y/%m/%d %H:%M:%S %z'
)
end
end
end
\ No newline at end of file
end
activesupport/lib/active_support/multibyte/chars.rb
浏览文件 @
1e9e88fc
...
...
@@ -282,9 +282,7 @@ def translate_offset(byte_offset) #: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
...
...
activesupport/lib/active_support/multibyte/utils.rb
浏览文件 @
1e9e88fc
...
...
@@ -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 @@ def self.verify!(string)
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
activesupport/test/buffered_logger_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -32,9 +32,7 @@ def test_write_binary_data_to_existing_file
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 @@ def test_write_binary_data_create_file
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 @@ def test_buffer_multibyte
@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
activesupport/test/caching_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -398,22 +398,9 @@ def test_really_long_keys
# 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\xBC
mlaut"
.
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 @@ def test_common_utf8_values
assert_equal
3
,
@cache
.
increment
(
key
)
assert_equal
2
,
@cache
.
decrement
(
key
)
end
end
def
test_retains_encoding
key
=
"
\xC3\xBC
mlaut"
.
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\xBC
mlaut"
.
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\xBC
mlaut"
.
force_encoding
(
Encoding
::
UTF_8
)
assert
@cache
.
write
(
key
,
"1"
,
:raw
=>
true
)
assert_equal
Encoding
::
UTF_8
,
key
.
encoding
end
end
...
...
activesupport/test/json/encoding_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -91,11 +91,11 @@ def test_hash_encoding
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
...
...
activesupport/test/multibyte_test_helpers.rb
浏览文件 @
1e9e88fc
...
...
@@ -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
)
...
...
railties/test/application/initializers/frameworks_test.rb
浏览文件 @
1e9e88fc
...
...
@@ -137,7 +137,7 @@ def from_bar_helper
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
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录