mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
String#uminus dedupes unconditionally
[Feature #14478] [ruby-core:85669] Thanks-to: Sam Saffron <sam.saffron@gmail.com> git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
b3cb6dbafc
commit
256411b47f
2 changed files with 8 additions and 14 deletions
|
@ -31,14 +31,12 @@ describe 'String#-@' do
|
||||||
(-"unfrozen string").should_not equal(-"another unfrozen string")
|
(-"unfrozen string").should_not equal(-"another unfrozen string")
|
||||||
end
|
end
|
||||||
|
|
||||||
it "is an identity function if the string is frozen" do
|
it "deduplicates frozen strings" do
|
||||||
dynamic = %w(this string is frozen).join(' ').freeze
|
dynamic = %w(this string is frozen).join(' ').freeze
|
||||||
|
|
||||||
(-dynamic).should equal(dynamic)
|
|
||||||
|
|
||||||
dynamic.should_not equal("this string is frozen".freeze)
|
dynamic.should_not equal("this string is frozen".freeze)
|
||||||
(-dynamic).should_not equal("this string is frozen".freeze)
|
(-dynamic).should equal("this string is frozen".freeze)
|
||||||
(-dynamic).should_not equal(-"this string is frozen".freeze)
|
(-dynamic).should equal(-"this string is frozen".freeze)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
14
string.c
14
string.c
|
@ -2607,20 +2607,16 @@ str_uplus(VALUE str)
|
||||||
* call-seq:
|
* call-seq:
|
||||||
* -str -> str (frozen)
|
* -str -> str (frozen)
|
||||||
*
|
*
|
||||||
* If the string is frozen, then return the string itself.
|
* Return a frozen, possibly pre-existing
|
||||||
|
* copy of the string.
|
||||||
*
|
*
|
||||||
* If the string is not frozen, return a frozen, possibly pre-existing
|
* String will be deduplicated as long as it is not tainted,
|
||||||
* copy of it.
|
* or has any instance vars set on it.
|
||||||
*/
|
*/
|
||||||
static VALUE
|
static VALUE
|
||||||
str_uminus(VALUE str)
|
str_uminus(VALUE str)
|
||||||
{
|
{
|
||||||
if (OBJ_FROZEN(str)) {
|
return rb_fstring(str);
|
||||||
return str;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return rb_fstring(str);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
|
RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue