From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.1 (2015-04-28) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS63949 64.71.152.0/24 X-Spam-Status: No, score=-2.6 required=3.0 tests=AWL,BAYES_00, RCVD_IN_DNSWL_NONE,RDNS_NONE,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=no autolearn_force=no version=3.4.1 Received: from 80x24.org (unknown [64.71.152.64]) by dcvr.yhbt.net (Postfix) with ESMTP id 104421F403 for ; Mon, 4 Jun 2018 04:27:52 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] String#uminus dedupes unconditionally Date: Mon, 4 Jun 2018 04:27:51 +0000 Message-Id: <20180604042751.20553-1-e@80x24.org> List-Id: [Feature #14478] [ruby-core:85669] Thanks-to: Sam Saffron --- spec/ruby/core/string/uminus_spec.rb | 8 +++----- string.c | 14 +++++--------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/spec/ruby/core/string/uminus_spec.rb b/spec/ruby/core/string/uminus_spec.rb index f79683cb74..f5f77f0d2e 100644 --- a/spec/ruby/core/string/uminus_spec.rb +++ b/spec/ruby/core/string/uminus_spec.rb @@ -31,14 +31,12 @@ (-"unfrozen string").should_not equal(-"another unfrozen string") 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).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 equal(-"this string is frozen".freeze) end end end diff --git a/string.c b/string.c index 36126eb805..088df21b40 100644 --- a/string.c +++ b/string.c @@ -2607,20 +2607,16 @@ str_uplus(VALUE str) * call-seq: * -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 - * copy of it. + * String will be deduplicated as long as it is not tainted, + * or has any instance vars set on it. */ static VALUE str_uminus(VALUE str) { - if (OBJ_FROZEN(str)) { - return str; - } - else { - return rb_fstring(str); - } + return rb_fstring(str); } RUBY_ALIAS_FUNCTION(rb_str_dup_frozen(VALUE str), rb_str_new_frozen, (str)) -- EW