Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
6f83a57a
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,发现更多精彩内容 >>
提交
6f83a57a
编写于
7月 08, 2010
作者:
N
Norman Clarke
提交者:
José Valim
7月 08, 2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improve bang method defs, make slice! operate in-place. [#5028 state:resolved]
Signed-off-by:
N
José Valim
<
jose.valim@gmail.com
>
上级
01629d18
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
33 addition
and
36 deletion
+33
-36
activesupport/lib/active_support/core_ext/string/multibyte.rb
...vesupport/lib/active_support/core_ext/string/multibyte.rb
+1
-1
activesupport/lib/active_support/multibyte/chars.rb
activesupport/lib/active_support/multibyte/chars.rb
+7
-19
activesupport/test/multibyte_chars_test.rb
activesupport/test/multibyte_chars_test.rb
+25
-16
未找到文件。
activesupport/lib/active_support/core_ext/string/multibyte.rb
浏览文件 @
6f83a57a
...
@@ -2,7 +2,7 @@
...
@@ -2,7 +2,7 @@
require
'active_support/multibyte'
require
'active_support/multibyte'
class
String
class
String
if
'1.9'
.
respond_to?
(
:force_encoding
)
if
RUBY_VERSION
>=
"1.9"
# == Multibyte proxy
# == Multibyte proxy
#
#
# +mb_chars+ is a multibyte safe proxy for string methods.
# +mb_chars+ is a multibyte safe proxy for string methods.
...
...
activesupport/lib/active_support/multibyte/chars.rb
浏览文件 @
6f83a57a
...
@@ -325,18 +325,6 @@ def slice(*args)
...
@@ -325,18 +325,6 @@ def slice(*args)
end
end
alias_method
:[]
,
:slice
alias_method
:[]
,
:slice
# Like <tt>String#slice!</tt>, except instead of byte offsets you specify character offsets.
#
# Example:
# s = 'こんにちは'
# s.mb_chars.slice!(2..3).to_s #=> "にち"
# s #=> "こんは"
def
slice!
(
*
args
)
slice
=
self
[
*
args
]
self
[
*
args
]
=
''
slice
end
# Limit the byte size of the string to a number of bytes without breaking characters. Usable
# Limit the byte size of the string to a number of bytes without breaking characters. Usable
# when the storage for a string is limited for some reason.
# when the storage for a string is limited for some reason.
#
#
...
@@ -425,14 +413,14 @@ def tidy_bytes(force = false)
...
@@ -425,14 +413,14 @@ def tidy_bytes(force = false)
chars
(
Unicode
.
tidy_bytes
(
@wrapped_string
,
force
))
chars
(
Unicode
.
tidy_bytes
(
@wrapped_string
,
force
))
end
end
%w(lstrip rstrip strip reverse upcase downcase tidy_bytes capitalize)
.
each
do
|
method
|
%w(capitalize downcase lstrip reverse rstrip slice strip tidy_bytes upcase)
.
each
do
|
method
|
define_method
(
"
#{
method
}
!"
)
do
|*
args
|
# Only define a corresponding bang method for methods defined in the proxy; On 1.9 the proxy will
unless
args
.
nil?
# exclude lstrip!, rstrip! and strip! because they are already work as expected on multibyte strings.
@wrapped_string
=
send
(
method
,
*
args
).
to_s
if
public_method_defined?
(
method
)
else
define_method
(
"
#{
method
}
!"
)
do
|*
args
|
@wrapped_string
=
send
(
method
).
to_s
@wrapped_string
=
send
(
args
.
nil?
?
method
:
method
,
*
args
).
to_s
self
end
end
self
end
end
end
end
...
...
activesupport/test/multibyte_chars_test.rb
浏览文件 @
6f83a57a
...
@@ -123,22 +123,30 @@ def test_indexed_insert_accepts_fixnums
...
@@ -123,22 +123,30 @@ def test_indexed_insert_accepts_fixnums
assert_equal
'こに わ'
,
@chars
assert_equal
'こに わ'
,
@chars
end
end
def
test_overridden_bang_methods_return_self
%w{capitalize downcase lstrip reverse rstrip strip upcase}
.
each
do
|
method
|
[
:rstrip!
,
:lstrip!
,
:strip!
,
:reverse!
,
:upcase!
,
:downcase!
,
:capitalize!
].
each
do
|
method
|
class_eval
(
<<-
EOTESTS
)
assert_equal
@chars
.
object_id
,
@chars
.
send
(
method
).
object_id
def test_
#{
method
}
_bang_should_return_self
end
assert_equal @chars.object_id, @chars.send("
#{
method
}
!").object_id
end
def test_
#{
method
}
_bang_should_change_wrapped_string
original = ' él piDió Un bUen café '
proxy = chars(original.dup)
proxy.send("
#{
method
}
!")
assert_not_equal original, proxy.to_s
end
EOTESTS
end
end
def
test_overridden_bang_methods_change_wrapped_string
def
test_tidy_bytes_bang_should_return_self
[
:rstrip!
,
:lstrip!
,
:strip!
,
:reverse!
,
:upcase!
,
:downcase!
].
each
do
|
method
|
assert_equal
@chars
.
object_id
,
@chars
.
tidy_bytes!
.
object_id
original
=
' Café '
end
proxy
=
chars
(
original
.
dup
)
proxy
.
send
(
method
)
def
test_tidy_bytes_bang_should_change_wrapped_string
assert_not_equal
original
,
proxy
.
to_s
original
=
" Un bUen café
\x92
"
end
proxy
=
chars
(
original
.
dup
)
proxy
=
chars
(
'òu'
)
proxy
.
tidy_bytes!
proxy
.
capitalize!
assert_not_equal
original
,
proxy
.
to_s
assert_equal
'Òu'
,
proxy
.
to_s
end
end
if
RUBY_VERSION
>=
'1.9'
if
RUBY_VERSION
>=
'1.9'
...
@@ -417,8 +425,9 @@ def test_slice_bang_returns_sliced_out_substring
...
@@ -417,8 +425,9 @@ def test_slice_bang_returns_sliced_out_substring
end
end
def
test_slice_bang_removes_the_slice_from_the_receiver
def
test_slice_bang_removes_the_slice_from_the_receiver
@chars
.
slice!
(
1
..
2
)
chars
=
'úüù'
.
mb_chars
assert_equal
'こわ'
,
@chars
chars
.
slice!
(
0
,
2
)
assert_equal
'úü'
,
chars
end
end
def
test_slice_should_throw_exceptions_on_invalid_arguments
def
test_slice_should_throw_exceptions_on_invalid_arguments
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录