提交 6f83a57a 编写于 作者: N Norman Clarke 提交者: José Valim

Improve bang method defs, make slice! operate in-place. [#5028 state:resolved]

Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 01629d18
...@@ -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.
......
...@@ -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
......
...@@ -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.
先完成此消息的编辑!
想要评论请 注册