1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

Make String methods return String instances when called on a subclass instance

This modifies the following String methods to return String instances
instead of subclass instances:

* String#*
* String#capitalize
* String#center
* String#chomp
* String#chop
* String#delete
* String#delete_prefix
* String#delete_suffix
* String#downcase
* String#dump
* String#each/#each_line
* String#gsub
* String#ljust
* String#lstrip
* String#partition
* String#reverse
* String#rjust
* String#rpartition
* String#rstrip
* String#scrub
* String#slice!
* String#slice/#[]
* String#split
* String#squeeze
* String#strip
* String#sub
* String#succ/#next
* String#swapcase
* String#tr
* String#tr_s
* String#upcase

This also fixes a bug in String#swapcase where it would return the
receiver instead of a copy of the receiver if the receiver was the
empty string.

Some string methods were left to return subclass instances:

* String#+@
* String#-@

Both of these methods will return the receiver (subclass instance)
in some cases, so it is best to keep the returned class consistent.

Fixes [#10845]
This commit is contained in:
Jeremy Evans 2020-10-24 11:52:30 -07:00
parent 4f5d14eb8c
commit 58325daae3
Notes: git 2020-11-21 09:30:48 +09:00
28 changed files with 591 additions and 211 deletions

View file

@ -80,12 +80,21 @@ describe "String#capitalize" do
-> { "abc".capitalize(:invalid_option) }.should raise_error(ArgumentError) -> { "abc".capitalize(:invalid_option) }.should raise_error(ArgumentError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("hello").capitalize.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("hello").capitalize.should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("Hello").capitalize.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("Hello").capitalize.should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("hello").capitalize.should be_an_instance_of(String)
StringSpecs::MyString.new("Hello").capitalize.should be_an_instance_of(String)
end
end
end
describe "String#capitalize!" do describe "String#capitalize!" do
it "capitalizes self in place" do it "capitalizes self in place" do
a = "hello" a = "hello"

View file

@ -91,6 +91,7 @@ describe "String#center with length, padding" do
-> { "hello".center(0, "") }.should raise_error(ArgumentError) -> { "hello".center(0, "") }.should raise_error(ArgumentError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on subclasses" do it "returns subclass instances when called on subclasses" do
StringSpecs::MyString.new("").center(10).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").center(10).should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").center(10).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").center(10).should be_an_instance_of(StringSpecs::MyString)
@ -99,6 +100,18 @@ describe "String#center with length, padding" do
"".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) "".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"foo".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) "foo".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on subclasses" do
StringSpecs::MyString.new("").center(10).should be_an_instance_of(String)
StringSpecs::MyString.new("foo").center(10).should be_an_instance_of(String)
StringSpecs::MyString.new("foo").center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"foo".center(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do

View file

@ -46,10 +46,19 @@ describe "String#chomp" do
end end
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
str = StringSpecs::MyString.new("hello\n").chomp str = StringSpecs::MyString.new("hello\n").chomp
str.should be_an_instance_of(StringSpecs::MyString) str.should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
str = StringSpecs::MyString.new("hello\n").chomp
str.should be_an_instance_of(String)
end
end
it "removes trailing characters that match $/ when it has been assigned a value" do it "removes trailing characters that match $/ when it has been assigned a value" do
$/ = "cdef" $/ = "cdef"

View file

@ -61,11 +61,19 @@ describe "String#chop" do
end end
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("hello\n").chop.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("hello\n").chop.should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("hello\n").chop.should be_an_instance_of(String)
end
end
end
describe "String#chop!" do describe "String#chop!" do
it "removes the final character" do it "removes the final character" do
"abc".chop!.should == "ab" "abc".chop!.should == "ab"

View file

@ -41,12 +41,21 @@ describe "String#delete_prefix" do
'hello'.delete_prefix(o).should == 'o' 'hello'.delete_prefix(o).should == 'o'
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance when called on a subclass instance" do it "returns a subclass instance when called on a subclass instance" do
s = StringSpecs::MyString.new('hello') s = StringSpecs::MyString.new('hello')
s.delete_prefix('hell').should be_an_instance_of(StringSpecs::MyString) s.delete_prefix('hell').should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns a String instance when called on a subclass instance" do
s = StringSpecs::MyString.new('hello')
s.delete_prefix('hell').should be_an_instance_of(String)
end
end
end
describe "String#delete_prefix!" do describe "String#delete_prefix!" do
it "removes the found prefix" do it "removes the found prefix" do
s = 'hello' s = 'hello'

View file

@ -93,11 +93,19 @@ describe "String#delete" do
-> { "hello world".delete(mock('x')) }.should raise_error(TypeError) -> { "hello world".delete(mock('x')) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("oh no!!!").delete("!").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("oh no!!!").delete("!").should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("oh no!!!").delete("!").should be_an_instance_of(String)
end
end
end
describe "String#delete!" do describe "String#delete!" do
it "modifies self in place and returns self" do it "modifies self in place and returns self" do
a = "hello" a = "hello"

View file

@ -41,12 +41,21 @@ describe "String#delete_suffix" do
'hello'.delete_suffix(o).should == 'h' 'hello'.delete_suffix(o).should == 'h'
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance when called on a subclass instance" do it "returns a subclass instance when called on a subclass instance" do
s = StringSpecs::MyString.new('hello') s = StringSpecs::MyString.new('hello')
s.delete_suffix('ello').should be_an_instance_of(StringSpecs::MyString) s.delete_suffix('ello').should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns a String instance when called on a subclass instance" do
s = StringSpecs::MyString.new('hello')
s.delete_suffix('ello').should be_an_instance_of(String)
end
end
end
describe "String#delete_suffix!" do describe "String#delete_suffix!" do
it "removes the found prefix" do it "removes the found prefix" do
s = 'hello' s = 'hello'

View file

@ -76,11 +76,19 @@ describe "String#downcase" do
end end
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance for subclasses" do it "returns a subclass instance for subclasses" do
StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns a String instance for subclasses" do
StringSpecs::MyString.new("FOObar").downcase.should be_an_instance_of(String)
end
end
end
describe "String#downcase!" do describe "String#downcase!" do
it "modifies self in place" do it "modifies self in place" do
a = "HeLlO" a = "HeLlO"

View file

@ -19,9 +19,17 @@ describe "String#dump" do
"foo".freeze.dump.should_not.frozen? "foo".freeze.dump.should_not.frozen?
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance" do it "returns a subclass instance" do
StringSpecs::MyString.new.dump.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new.dump.should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns a String instance" do
StringSpecs::MyString.new.dump.should be_an_instance_of(String)
end
end
it "wraps string with \"" do it "wraps string with \"" do
"foo".dump.should == '"foo"' "foo".dump.should == '"foo"'

View file

@ -236,12 +236,23 @@ describe "String#gsub with pattern and replacement" do
-> { "hello".gsub(/[aeiou]/, nil) }.should raise_error(TypeError) -> { "hello".gsub(/[aeiou]/, nil) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("").gsub(//, "").should be_an_instance_of(String)
StringSpecs::MyString.new("").gsub(/foo/, "").should be_an_instance_of(String)
StringSpecs::MyString.new("foo").gsub(/foo/, "").should be_an_instance_of(String)
StringSpecs::MyString.new("foo").gsub("foo", "").should be_an_instance_of(String)
end
end
# Note: $~ cannot be tested because mspec messes with it # Note: $~ cannot be tested because mspec messes with it

View file

@ -74,6 +74,7 @@ describe "String#ljust with length, padding" do
-> { "hello".ljust(10, '') }.should raise_error(ArgumentError) -> { "hello".ljust(10, '') }.should raise_error(ArgumentError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on subclasses" do it "returns subclass instances when called on subclasses" do
StringSpecs::MyString.new("").ljust(10).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").ljust(10).should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").ljust(10).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").ljust(10).should be_an_instance_of(StringSpecs::MyString)
@ -82,6 +83,18 @@ describe "String#ljust with length, padding" do
"".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) "".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"foo".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) "foo".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on subclasses" do
StringSpecs::MyString.new("").ljust(10).should be_an_instance_of(String)
StringSpecs::MyString.new("foo").ljust(10).should be_an_instance_of(String)
StringSpecs::MyString.new("foo").ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"foo".ljust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do

View file

@ -74,6 +74,7 @@ describe "String#rjust with length, padding" do
-> { "hello".rjust(10, '') }.should raise_error(ArgumentError) -> { "hello".rjust(10, '') }.should raise_error(ArgumentError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on subclasses" do it "returns subclass instances when called on subclasses" do
StringSpecs::MyString.new("").rjust(10).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").rjust(10).should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").rjust(10).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").rjust(10).should be_an_instance_of(StringSpecs::MyString)
@ -82,6 +83,18 @@ describe "String#rjust with length, padding" do
"".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) "".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"foo".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String) "foo".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on subclasses" do
StringSpecs::MyString.new("").rjust(10).should be_an_instance_of(String)
StringSpecs::MyString.new("foo").rjust(10).should be_an_instance_of(String)
StringSpecs::MyString.new("foo").rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
"foo".rjust(10, StringSpecs::MyString.new("x")).should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do it "when padding is tainted and self is untainted returns a tainted string if and only if length is longer than self" do

View file

@ -93,11 +93,21 @@ describe :string_each_line, shared: true do
end end
end end
ruby_version_is ''...'3.0' do
it "yields subclass instances for subclasses" do it "yields subclass instances for subclasses" do
a = [] a = []
StringSpecs::MyString.new("hello\nworld").send(@method) { |s| a << s.class } StringSpecs::MyString.new("hello\nworld").send(@method) { |s| a << s.class }
a.should == [StringSpecs::MyString, StringSpecs::MyString] a.should == [StringSpecs::MyString, StringSpecs::MyString]
end end
end
ruby_version_is '3.0' do
it "yields String instances for subclasses" do
a = []
StringSpecs::MyString.new("hello\nworld").send(@method) { |s| a << s.class }
a.should == [String, String]
end
end
it "returns self" do it "returns self" do
s = "hello\nworld" s = "hello\nworld"

View file

@ -163,12 +163,23 @@ describe :string_slice_index_length, shared: true do
-> { "hello".send(@method, 0, bignum_value) }.should raise_error(RangeError) -> { "hello".send(@method, 0, bignum_value) }.should raise_error(RangeError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.send(@method, 0,0).should be_an_instance_of(StringSpecs::MyString) s.send(@method, 0,0).should be_an_instance_of(StringSpecs::MyString)
s.send(@method, 0,4).should be_an_instance_of(StringSpecs::MyString) s.send(@method, 0,4).should be_an_instance_of(StringSpecs::MyString)
s.send(@method, 1,4).should be_an_instance_of(StringSpecs::MyString) s.send(@method, 1,4).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.send(@method, 0,0).should be_an_instance_of(String)
s.send(@method, 0,4).should be_an_instance_of(String)
s.send(@method, 1,4).should be_an_instance_of(String)
end
end
it "handles repeated application" do it "handles repeated application" do
"hello world".send(@method, 6, 5).send(@method, 0, 1).should == 'w' "hello world".send(@method, 6, 5).send(@method, 0, 1).should == 'w'
@ -252,12 +263,23 @@ describe :string_slice_range, shared: true do
end end
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.send(@method, 0...0).should be_an_instance_of(StringSpecs::MyString) s.send(@method, 0...0).should be_an_instance_of(StringSpecs::MyString)
s.send(@method, 0..4).should be_an_instance_of(StringSpecs::MyString) s.send(@method, 0..4).should be_an_instance_of(StringSpecs::MyString)
s.send(@method, 1..4).should be_an_instance_of(StringSpecs::MyString) s.send(@method, 1..4).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.send(@method, 0...0).should be_an_instance_of(String)
s.send(@method, 0..4).should be_an_instance_of(String)
s.send(@method, 1..4).should be_an_instance_of(String)
end
end
it "calls to_int on range arguments" do it "calls to_int on range arguments" do
from = mock('from') from = mock('from')
@ -348,11 +370,21 @@ describe :string_slice_regexp, shared: true do
end end
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.send(@method, //).should be_an_instance_of(StringSpecs::MyString) s.send(@method, //).should be_an_instance_of(StringSpecs::MyString)
s.send(@method, /../).should be_an_instance_of(StringSpecs::MyString) s.send(@method, /../).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.send(@method, //).should be_an_instance_of(String)
s.send(@method, /../).should be_an_instance_of(String)
end
end
it "sets $~ to MatchData when there is a match and nil when there's none" do it "sets $~ to MatchData when there is a match and nil when there's none" do
'hello'.send(@method, /./) 'hello'.send(@method, /./)
@ -436,11 +468,21 @@ describe :string_slice_regexp_index, shared: true do
-> { "hello".send(@method, /(.)(.)(.)/, nil) }.should raise_error(TypeError) -> { "hello".send(@method, /(.)(.)(.)/, nil) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.send(@method, /(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString) s.send(@method, /(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString)
s.send(@method, /(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString) s.send(@method, /(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.send(@method, /(.)(.)/, 0).should be_an_instance_of(String)
s.send(@method, /(.)(.)/, 1).should be_an_instance_of(String)
end
end
it "sets $~ to MatchData when there is a match and nil when there's none" do it "sets $~ to MatchData when there is a match and nil when there's none" do
'hello'.send(@method, /.(.)/, 0) 'hello'.send(@method, /.(.)/, 0)
@ -493,6 +535,7 @@ describe :string_slice_string, shared: true do
-> { "hello".send(@method, o) }.should raise_error(TypeError) -> { "hello".send(@method, o) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance when given a subclass instance" do it "returns a subclass instance when given a subclass instance" do
s = StringSpecs::MyString.new("el") s = StringSpecs::MyString.new("el")
r = "hello".send(@method, s) r = "hello".send(@method, s)
@ -501,6 +544,16 @@ describe :string_slice_string, shared: true do
end end
end end
ruby_version_is '3.0' do
it "returns a String instance when given a subclass instance" do
s = StringSpecs::MyString.new("el")
r = "hello".send(@method, s)
r.should == "el"
r.should be_an_instance_of(String)
end
end
end
describe :string_slice_regexp_group, shared: true do describe :string_slice_regexp_group, shared: true do
not_supported_on :opal do not_supported_on :opal do
it "returns the capture for the given name" do it "returns the capture for the given name" do
@ -567,10 +620,19 @@ describe :string_slice_regexp_group, shared: true do
-> { "hello".send(@method, /(?<q>)/, '') }.should raise_error(IndexError) -> { "hello".send(@method, /(?<q>)/, '') }.should raise_error(IndexError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.send(@method, /(?<q>.)/, 'q').should be_an_instance_of(StringSpecs::MyString) s.send(@method, /(?<q>.)/, 'q').should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.send(@method, /(?<q>.)/, 'q').should be_an_instance_of(String)
end
end
it "sets $~ to MatchData when there is a match and nil when there's none" do it "sets $~ to MatchData when there is a match and nil when there's none" do
'hello'.send(@method, /(?<hi>.(.))/, 'hi') 'hello'.send(@method, /(?<hi>.(.))/, 'hi')

View file

@ -59,11 +59,21 @@ describe :string_succ, shared: true do
"\xFF\xFF".send(@method).should == "\x01\x00\x00" "\xFF\xFF".send(@method).should == "\x01\x00\x00"
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("").send(@method).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").send(@method).should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("a").send(@method).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("a").send(@method).should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("z").send(@method).should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("z").send(@method).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("").send(@method).should be_an_instance_of(String)
StringSpecs::MyString.new("a").send(@method).should be_an_instance_of(String)
StringSpecs::MyString.new("z").send(@method).should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "taints the result if self is tainted" do it "taints the result if self is tainted" do

View file

@ -142,12 +142,21 @@ describe "String#slice! with index, length" do
"hello".slice!(obj, obj).should == "ll" "hello".slice!(obj, obj).should == "ll"
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.slice!(0, 0).should be_an_instance_of(StringSpecs::MyString) s.slice!(0, 0).should be_an_instance_of(StringSpecs::MyString)
s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString) s.slice!(0, 4).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.slice!(0, 0).should be_an_instance_of(String)
s.slice!(0, 4).should be_an_instance_of(String)
end
end
it "returns the substring given by the character offsets" do it "returns the substring given by the character offsets" do
"hellö there".slice!(1,0).should == "" "hellö there".slice!(1,0).should == ""
@ -196,11 +205,21 @@ describe "String#slice! Range" do
end end
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.slice!(0...0).should be_an_instance_of(StringSpecs::MyString) s.slice!(0...0).should be_an_instance_of(StringSpecs::MyString)
s.slice!(0..4).should be_an_instance_of(StringSpecs::MyString) s.slice!(0..4).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.slice!(0...0).should be_an_instance_of(String)
s.slice!(0..4).should be_an_instance_of(String)
end
end
it "calls to_int on range arguments" do it "calls to_int on range arguments" do
from = mock('from') from = mock('from')
@ -299,11 +318,21 @@ describe "String#slice! with Regexp" do
end end
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.slice!(//).should be_an_instance_of(StringSpecs::MyString) s.slice!(//).should be_an_instance_of(StringSpecs::MyString)
s.slice!(/../).should be_an_instance_of(StringSpecs::MyString) s.slice!(/../).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.slice!(//).should be_an_instance_of(String)
s.slice!(/../).should be_an_instance_of(String)
end
end
it "returns the matching portion of self with a multi byte character" do it "returns the matching portion of self with a multi byte character" do
"hëllo there".slice!(/[ë](.)\1/).should == "ëll" "hëllo there".slice!(/[ë](.)\1/).should == "ëll"
@ -383,11 +412,21 @@ describe "String#slice! with Regexp, index" do
"har".slice!(/(.)(.)(.)/, obj).should == "a" "har".slice!(/(.)(.)(.)/, obj).should == "a"
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
s = StringSpecs::MyString.new("hello") s = StringSpecs::MyString.new("hello")
s.slice!(/(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString) s.slice!(/(.)(.)/, 0).should be_an_instance_of(StringSpecs::MyString)
s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString) s.slice!(/(.)(.)/, 1).should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
s = StringSpecs::MyString.new("hello")
s.slice!(/(.)(.)/, 0).should be_an_instance_of(String)
s.slice!(/(.)(.)/, 1).should be_an_instance_of(String)
end
end
it "returns the encoding aware capture for the given index" do it "returns the encoding aware capture for the given index" do
"hår".slice!(/(.)(.)(.)/, 0).should == "hår" "hår".slice!(/(.)(.)(.)/, 0).should == "hår"
@ -461,12 +500,23 @@ describe "String#slice! with String" do
-> { "hello".slice!(o) }.should raise_error(TypeError) -> { "hello".slice!(o) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance when given a subclass instance" do it "returns a subclass instance when given a subclass instance" do
s = StringSpecs::MyString.new("el") s = StringSpecs::MyString.new("el")
r = "hello".slice!(s) r = "hello".slice!(s)
r.should == "el" r.should == "el"
r.should be_an_instance_of(StringSpecs::MyString) r.should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns a subclass instance when given a subclass instance" do
s = StringSpecs::MyString.new("el")
r = "hello".slice!(s)
r.should == "el"
r.should be_an_instance_of(String)
end
end
it "raises a FrozenError if self is frozen" do it "raises a FrozenError if self is frozen" do
-> { "hello hello".freeze.slice!('llo') }.should raise_error(FrozenError) -> { "hello hello".freeze.slice!('llo') }.should raise_error(FrozenError)

View file

@ -159,6 +159,7 @@ describe "String#split with String" do
"foo".split("bar", 3).should == ["foo"] "foo".split("bar", 3).should == ["foo"]
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances based on self" do it "returns subclass instances based on self" do
["", "x.y.z.", " x y "].each do |str| ["", "x.y.z.", " x y "].each do |str|
["", ".", " "].each do |pat| ["", ".", " "].each do |pat|
@ -182,6 +183,25 @@ describe "String#split with String" do
s = StringSpecs::StringWithRaisingConstructor.new('silly:string') s = StringSpecs::StringWithRaisingConstructor.new('silly:string')
s.split(':').first.should == 'silly' s.split(':').first.should == 'silly'
end end
end
ruby_version_is '3.0' do
it "returns String instances based on self" do
["", "x.y.z.", " x y "].each do |str|
["", ".", " "].each do |pat|
[-1, 0, 1, 2].each do |limit|
StringSpecs::MyString.new(str).split(pat, limit).each do |x|
x.should be_an_instance_of(String)
end
str.split(StringSpecs::MyString.new(pat), limit).each do |x|
x.should be_an_instance_of(String)
end
end
end
end
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "taints the resulting strings if self is tainted" do it "taints the resulting strings if self is tainted" do
@ -355,6 +375,7 @@ describe "String#split with Regexp" do
"foo".split(/bar/, 3).should == ["foo"] "foo".split(/bar/, 3).should == ["foo"]
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances based on self" do it "returns subclass instances based on self" do
["", "x:y:z:", " x y "].each do |str| ["", "x:y:z:", " x y "].each do |str|
[//, /:/, /\s+/].each do |pat| [//, /:/, /\s+/].each do |pat|
@ -374,6 +395,21 @@ describe "String#split with Regexp" do
s = StringSpecs::StringWithRaisingConstructor.new('silly:string') s = StringSpecs::StringWithRaisingConstructor.new('silly:string')
s.split(/:/).first.should == 'silly' s.split(/:/).first.should == 'silly'
end end
end
ruby_version_is '3.0' do
it "returns String instances based on self" do
["", "x:y:z:", " x y "].each do |str|
[//, /:/, /\s+/].each do |pat|
[-1, 0, 1, 2].each do |limit|
StringSpecs::MyString.new(str).split(pat, limit).each do |x|
x.should be_an_instance_of(String)
end
end
end
end
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "taints the resulting strings if self is tainted" do it "taints the resulting strings if self is tainted" do
@ -493,6 +529,7 @@ describe "String#split with Regexp" do
end end
describe "for a String subclass" do describe "for a String subclass" do
ruby_version_is ''...'3.0' do
it "yields instances of the same subclass" do it "yields instances of the same subclass" do
a = [] a = []
StringSpecs::MyString.new("a|b").split("|") { |str| a << str } StringSpecs::MyString.new("a|b").split("|") { |str| a << str }
@ -505,5 +542,20 @@ describe "String#split with Regexp" do
last.should == "b" last.should == "b"
end end
end end
ruby_version_is '3.0' do
it "yields instances of String" do
a = []
StringSpecs::MyString.new("a|b").split("|") { |str| a << str }
first, last = a
first.should be_an_instance_of(String)
first.should == "a"
last.should be_an_instance_of(String)
last.should == "b"
end
end
end
end end
end end

View file

@ -80,11 +80,19 @@ describe "String#squeeze" do
-> { "hello world".squeeze(mock('x')) }.should raise_error(TypeError) -> { "hello world".squeeze(mock('x')) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("oh no!!!").squeeze("!").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("oh no!!!").squeeze("!").should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("oh no!!!").squeeze("!").should be_an_instance_of(String)
end
end
end
describe "String#squeeze!" do describe "String#squeeze!" do
it "modifies self in place and returns self" do it "modifies self in place and returns self" do
a = "yellow moon" a = "yellow moon"

View file

@ -192,12 +192,23 @@ describe "String#sub with pattern, replacement" do
-> { "hello".sub(/[aeiou]/, 99) }.should raise_error(TypeError) -> { "hello".sub(/[aeiou]/, 99) }.should raise_error(TypeError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("").sub(//, "").should be_an_instance_of(String)
StringSpecs::MyString.new("").sub(/foo/, "").should be_an_instance_of(String)
StringSpecs::MyString.new("foo").sub(/foo/, "").should be_an_instance_of(String)
StringSpecs::MyString.new("foo").sub("foo", "").should be_an_instance_of(String)
end
end
it "sets $~ to MatchData of match and nil when there's none" do it "sets $~ to MatchData of match and nil when there's none" do
'hello.'.sub('hello', 'x') 'hello.'.sub('hello', 'x')

View file

@ -73,12 +73,21 @@ describe "String#swapcase" do
-> { "abc".swapcase(:invalid_option) }.should raise_error(ArgumentError) -> { "abc".swapcase(:invalid_option) }.should raise_error(ArgumentError)
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("").swapcase.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("").swapcase.should be_an_instance_of(StringSpecs::MyString)
StringSpecs::MyString.new("hello").swapcase.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("hello").swapcase.should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("").swapcase.should be_an_instance_of(String)
StringSpecs::MyString.new("hello").swapcase.should be_an_instance_of(String)
end
end
end
describe "String#swapcase!" do describe "String#swapcase!" do
it "modifies self in place" do it "modifies self in place" do
a = "cYbEr_PuNk11" a = "cYbEr_PuNk11"

View file

@ -45,9 +45,17 @@ describe "String#tr_s" do
"bla".tr_s(from_str, to_str).should == "BlA" "bla".tr_s(from_str, to_str).should == "BlA"
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("hello").tr_s("e", "a").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("hello").tr_s("e", "a").should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances when called on a subclass" do
StringSpecs::MyString.new("hello").tr_s("e", "a").should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "taints the result when self is tainted" do it "taints the result when self is tainted" do

View file

@ -57,9 +57,17 @@ describe "String#tr" do
"bla".tr(from_str, to_str).should == "BlA" "bla".tr(from_str, to_str).should == "BlA"
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances when called on a subclass" do it "returns subclass instances when called on a subclass" do
StringSpecs::MyString.new("hello").tr("e", "a").should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("hello").tr("e", "a").should be_an_instance_of(StringSpecs::MyString)
end end
end
ruby_version_is '3.0' do
it "returns Stringinstances when called on a subclass" do
StringSpecs::MyString.new("hello").tr("e", "a").should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "taints the result when self is tainted" do it "taints the result when self is tainted" do

View file

@ -73,11 +73,19 @@ describe "String#upcase" do
end end
end end
ruby_version_is ''...'3.0' do
it "returns a subclass instance for subclasses" do it "returns a subclass instance for subclasses" do
StringSpecs::MyString.new("fooBAR").upcase.should be_an_instance_of(StringSpecs::MyString) StringSpecs::MyString.new("fooBAR").upcase.should be_an_instance_of(StringSpecs::MyString)
end end
end end
ruby_version_is '3.0' do
it "returns a String instance for subclasses" do
StringSpecs::MyString.new("fooBAR").upcase.should be_an_instance_of(String)
end
end
end
describe "String#upcase!" do describe "String#upcase!" do
it "modifies self in place" do it "modifies self in place" do
a = "HeLlO" a = "HeLlO"

View file

@ -32,11 +32,21 @@ describe :string_times, shared: true do
@object.call("", max_long).should == "" @object.call("", max_long).should == ""
end end
ruby_version_is ''...'3.0' do
it "returns subclass instances" do it "returns subclass instances" do
@object.call(MyString.new("cool"), 0).should be_an_instance_of(MyString) @object.call(MyString.new("cool"), 0).should be_an_instance_of(MyString)
@object.call(MyString.new("cool"), 1).should be_an_instance_of(MyString) @object.call(MyString.new("cool"), 1).should be_an_instance_of(MyString)
@object.call(MyString.new("cool"), 2).should be_an_instance_of(MyString) @object.call(MyString.new("cool"), 2).should be_an_instance_of(MyString)
end end
end
ruby_version_is '3.0' do
it "returns String instances" do
@object.call(MyString.new("cool"), 0).should be_an_instance_of(String)
@object.call(MyString.new("cool"), 1).should be_an_instance_of(String)
@object.call(MyString.new("cool"), 2).should be_an_instance_of(String)
end
end
ruby_version_is ''...'2.7' do ruby_version_is ''...'2.7' do
it "always taints the result when self is tainted" do it "always taints the result when self is tainted" do

118
string.c
View file

@ -1228,6 +1228,13 @@ rb_str_new_frozen(VALUE orig)
return str_new_frozen(rb_obj_class(orig), orig); return str_new_frozen(rb_obj_class(orig), orig);
} }
static VALUE
rb_str_new_frozen_String(VALUE orig)
{
if (OBJ_FROZEN(orig) && rb_obj_class(orig) == rb_cString) return orig;
return str_new_frozen(rb_cString, orig);
}
VALUE VALUE
rb_str_tmp_frozen_acquire(VALUE orig) rb_str_tmp_frozen_acquire(VALUE orig)
{ {
@ -1336,6 +1343,14 @@ str_new_empty(VALUE str)
return v; return v;
} }
static VALUE
str_new_empty_String(VALUE str)
{
VALUE v = rb_str_new(0, 0);
rb_enc_copy(v, str);
return v;
}
#define STR_BUF_MIN_SIZE 63 #define STR_BUF_MIN_SIZE 63
STATIC_ASSERT(STR_BUF_MIN_SIZE, STR_BUF_MIN_SIZE > RSTRING_EMBED_LEN_MAX); STATIC_ASSERT(STR_BUF_MIN_SIZE, STR_BUF_MIN_SIZE > RSTRING_EMBED_LEN_MAX);
@ -2036,10 +2051,10 @@ rb_str_times(VALUE str, VALUE times)
int termlen; int termlen;
if (times == INT2FIX(1)) { if (times == INT2FIX(1)) {
return rb_str_dup(str); return str_duplicate(rb_cString, str);
} }
if (times == INT2FIX(0)) { if (times == INT2FIX(0)) {
str2 = str_alloc(rb_obj_class(str)); str2 = str_alloc(rb_cString);
rb_enc_copy(str2, str); rb_enc_copy(str2, str);
return str2; return str2;
} }
@ -2048,7 +2063,7 @@ rb_str_times(VALUE str, VALUE times)
rb_raise(rb_eArgError, "negative argument"); rb_raise(rb_eArgError, "negative argument");
} }
if (RSTRING_LEN(str) == 1 && RSTRING_PTR(str)[0] == 0) { if (RSTRING_LEN(str) == 1 && RSTRING_PTR(str)[0] == 0) {
str2 = str_alloc(rb_obj_class(str)); str2 = str_alloc(rb_cString);
if (!STR_EMBEDDABLE_P(len, 1)) { if (!STR_EMBEDDABLE_P(len, 1)) {
RSTRING(str2)->as.heap.aux.capa = len; RSTRING(str2)->as.heap.aux.capa = len;
RSTRING(str2)->as.heap.ptr = ZALLOC_N(char, (size_t)len + 1); RSTRING(str2)->as.heap.ptr = ZALLOC_N(char, (size_t)len + 1);
@ -2064,7 +2079,7 @@ rb_str_times(VALUE str, VALUE times)
len *= RSTRING_LEN(str); len *= RSTRING_LEN(str);
termlen = TERM_LEN(str); termlen = TERM_LEN(str);
str2 = str_new0(rb_obj_class(str), 0, len, termlen); str2 = str_new0(rb_cString, 0, len, termlen);
ptr2 = RSTRING_PTR(str2); ptr2 = RSTRING_PTR(str2);
if (len) { if (len) {
n = RSTRING_LEN(str); n = RSTRING_LEN(str);
@ -2545,13 +2560,13 @@ rb_str_subseq(VALUE str, long beg, long len)
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) &&
SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) { SHARABLE_SUBSTRING_P(beg, len, RSTRING_LEN(str))) {
long olen; long olen;
str2 = rb_str_new_shared(rb_str_new_frozen(str)); str2 = rb_str_new_shared(rb_str_new_frozen_String(str));
RSTRING(str2)->as.heap.ptr += beg; RSTRING(str2)->as.heap.ptr += beg;
olen = RSTRING(str2)->as.heap.len; olen = RSTRING(str2)->as.heap.len;
if (olen > len) RSTRING(str2)->as.heap.len = len; if (olen > len) RSTRING(str2)->as.heap.len = len;
} }
else { else {
str2 = rb_str_new_with_class(str, RSTRING_PTR(str)+beg, len); str2 = rb_str_new(RSTRING_PTR(str)+beg, len);
RB_GC_GUARD(str); RB_GC_GUARD(str);
} }
@ -2664,14 +2679,14 @@ str_substr(VALUE str, long beg, long len, int empty)
SHARABLE_SUBSTRING_P(p, len, RSTRING_END(str))) { SHARABLE_SUBSTRING_P(p, len, RSTRING_END(str))) {
long ofs = p - RSTRING_PTR(str); long ofs = p - RSTRING_PTR(str);
str2 = rb_str_new_frozen(str); str2 = rb_str_new_frozen(str);
str2 = str_new_shared(rb_obj_class(str2), str2); str2 = str_new_shared(rb_cString, str2);
RSTRING(str2)->as.heap.ptr += ofs; RSTRING(str2)->as.heap.ptr += ofs;
RSTRING(str2)->as.heap.len = len; RSTRING(str2)->as.heap.len = len;
ENC_CODERANGE_CLEAR(str2); ENC_CODERANGE_CLEAR(str2);
} }
else { else {
if (!len && !empty) return Qnil; if (!len && !empty) return Qnil;
str2 = rb_str_new_with_class(str, p, len); str2 = rb_str_new(p, len);
RB_GC_GUARD(str); RB_GC_GUARD(str);
} }
rb_enc_cr_str_copy_for_substr(str2, str); rb_enc_cr_str_copy_for_substr(str2, str);
@ -4215,7 +4230,7 @@ VALUE
rb_str_succ(VALUE orig) rb_str_succ(VALUE orig)
{ {
VALUE str; VALUE str;
str = rb_str_new_with_class(orig, RSTRING_PTR(orig), RSTRING_LEN(orig)); str = rb_str_new(RSTRING_PTR(orig), RSTRING_LEN(orig));
rb_enc_cr_str_copy_for_substr(str, orig); rb_enc_cr_str_copy_for_substr(str, orig);
return str_succ(str); return str_succ(str);
} }
@ -4444,7 +4459,7 @@ rb_str_upto_each(VALUE beg, VALUE end, int excl, int (*each)(VALUE, VALUE), VALU
if (n > 0 || (excl && n == 0)) return beg; if (n > 0 || (excl && n == 0)) return beg;
after_end = rb_funcallv(end, succ, 0, 0); after_end = rb_funcallv(end, succ, 0, 0);
current = rb_str_dup(beg); current = str_duplicate(rb_cString, beg);
while (!rb_str_equal(current, after_end)) { while (!rb_str_equal(current, after_end)) {
VALUE next = Qnil; VALUE next = Qnil;
if (excl || !rb_str_equal(current, end)) if (excl || !rb_str_equal(current, end))
@ -4492,7 +4507,7 @@ rb_str_upto_endless_each(VALUE beg, int (*each)(VALUE, VALUE), VALUE arg)
} }
} }
/* normal case */ /* normal case */
current = rb_str_dup(beg); current = str_duplicate(rb_cString, beg);
while (1) { while (1) {
VALUE next = rb_funcallv(current, succ, 0, 0); VALUE next = rb_funcallv(current, succ, 0, 0);
if ((*each)(current, arg)) break; if ((*each)(current, arg)) break;
@ -4582,7 +4597,7 @@ rb_str_aref(VALUE str, VALUE indx)
} }
else if (RB_TYPE_P(indx, T_STRING)) { else if (RB_TYPE_P(indx, T_STRING)) {
if (rb_str_index(str, indx, 0) != -1) if (rb_str_index(str, indx, 0) != -1)
return rb_str_dup(indx); return str_duplicate(rb_cString, indx);
return Qnil; return Qnil;
} }
else { else {
@ -5005,7 +5020,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
beg = rb_str_index(str, indx, 0); beg = rb_str_index(str, indx, 0);
if (beg == -1) return Qnil; if (beg == -1) return Qnil;
len = RSTRING_LEN(indx); len = RSTRING_LEN(indx);
result = rb_str_dup(indx); result = str_duplicate(rb_cString, indx);
goto squash; goto squash;
} }
else { else {
@ -5028,7 +5043,7 @@ rb_str_slice_bang(int argc, VALUE *argv, VALUE str)
beg = p - RSTRING_PTR(str); beg = p - RSTRING_PTR(str);
subseq: subseq:
result = rb_str_new_with_class(str, RSTRING_PTR(str)+beg, len); result = rb_str_new(RSTRING_PTR(str)+beg, len);
rb_enc_cr_str_copy_for_substr(result, str); rb_enc_cr_str_copy_for_substr(result, str);
squash: squash:
@ -5107,7 +5122,7 @@ rb_pat_search(VALUE pat, VALUE str, long pos, int set_backref_str)
pos = rb_strseq_index(str, pat, pos, 1); pos = rb_strseq_index(str, pat, pos, 1);
if (set_backref_str) { if (set_backref_str) {
if (pos >= 0) { if (pos >= 0) {
str = rb_str_new_frozen(str); str = rb_str_new_frozen_String(str);
rb_backref_set_string(str, pos, RSTRING_LEN(pat)); rb_backref_set_string(str, pos, RSTRING_LEN(pat));
} }
else { else {
@ -5301,7 +5316,7 @@ rb_str_sub_bang(int argc, VALUE *argv, VALUE str)
static VALUE static VALUE
rb_str_sub(int argc, VALUE *argv, VALUE str) rb_str_sub(int argc, VALUE *argv, VALUE str)
{ {
str = rb_str_dup(str); str = str_duplicate(rb_cString, str);
rb_str_sub_bang(argc, argv, str); rb_str_sub_bang(argc, argv, str);
return str; return str;
} }
@ -5341,7 +5356,7 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
beg = rb_pat_search(pat, str, 0, need_backref); beg = rb_pat_search(pat, str, 0, need_backref);
if (beg < 0) { if (beg < 0) {
if (bang) return Qnil; /* no match, no substitution */ if (bang) return Qnil; /* no match, no substitution */
return rb_str_dup(str); return str_duplicate(rb_cString, str);
} }
offset = 0; offset = 0;
@ -5422,7 +5437,6 @@ str_gsub(int argc, VALUE *argv, VALUE str, int bang)
str_shared_replace(str, dest); str_shared_replace(str, dest);
} }
else { else {
RBASIC_SET_CLASS(dest, rb_obj_class(str));
str = dest; str = dest;
} }
@ -5685,12 +5699,12 @@ str_byte_substr(VALUE str, long beg, long len, int empty)
if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(beg, len, n)) { if (!STR_EMBEDDABLE_P(len, TERM_LEN(str)) && SHARABLE_SUBSTRING_P(beg, len, n)) {
str2 = rb_str_new_frozen(str); str2 = rb_str_new_frozen(str);
str2 = str_new_shared(rb_obj_class(str2), str2); str2 = str_new_shared(rb_cString, str2);
RSTRING(str2)->as.heap.ptr += beg; RSTRING(str2)->as.heap.ptr += beg;
RSTRING(str2)->as.heap.len = len; RSTRING(str2)->as.heap.len = len;
} }
else { else {
str2 = rb_str_new_with_class(str, p, len); str2 = rb_str_new(p, len);
} }
str_enc_copy(str2, str); str_enc_copy(str2, str);
@ -5792,9 +5806,9 @@ rb_str_reverse(VALUE str)
char *s, *e, *p; char *s, *e, *p;
int cr; int cr;
if (RSTRING_LEN(str) <= 1) return rb_str_dup(str); if (RSTRING_LEN(str) <= 1) return str_duplicate(rb_cString, str);
enc = STR_ENC_GET(str); enc = STR_ENC_GET(str);
rev = rb_str_new_with_class(str, 0, RSTRING_LEN(str)); rev = rb_str_new(0, RSTRING_LEN(str));
s = RSTRING_PTR(str); e = RSTRING_END(str); s = RSTRING_PTR(str); e = RSTRING_END(str);
p = RSTRING_END(rev); p = RSTRING_END(rev);
cr = ENC_CODERANGE(str); cr = ENC_CODERANGE(str);
@ -6273,7 +6287,7 @@ rb_str_dump(VALUE str)
len += clen; len += clen;
} }
result = rb_str_new_with_class(str, 0, len); result = rb_str_new(0, len);
p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str); p = RSTRING_PTR(str); pend = p + RSTRING_LEN(str);
q = RSTRING_PTR(result); qend = q + len + 1; q = RSTRING_PTR(result); qend = q + len + 1;
@ -6696,7 +6710,7 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
size_t buffer_count = 0; size_t buffer_count = 0;
int buffer_length_or_invalid; int buffer_length_or_invalid;
if (RSTRING_LEN(source) == 0) return rb_str_dup(source); if (RSTRING_LEN(source) == 0) return str_duplicate(rb_cString, source);
source_current = (OnigUChar*)RSTRING_PTR(source); source_current = (OnigUChar*)RSTRING_PTR(source);
source_end = (OnigUChar*)RSTRING_END(source); source_end = (OnigUChar*)RSTRING_END(source);
@ -6732,12 +6746,12 @@ rb_str_casemap(VALUE source, OnigCaseFoldType *flags, rb_encoding *enc)
} }
if (buffer_count==1) { if (buffer_count==1) {
target = rb_str_new_with_class(source, (const char*)current_buffer->space, target_length); target = rb_str_new((const char*)current_buffer->space, target_length);
} }
else { else {
char *target_current; char *target_current;
target = rb_str_new_with_class(source, 0, target_length); target = rb_str_new(0, target_length);
target_current = RSTRING_PTR(target); target_current = RSTRING_PTR(target);
current_buffer = DATA_PTR(buffer_anchor); current_buffer = DATA_PTR(buffer_anchor);
while (current_buffer) { while (current_buffer) {
@ -6870,12 +6884,12 @@ rb_str_upcase(int argc, VALUE *argv, VALUE str)
flags = check_case_options(argc, argv, flags); flags = check_case_options(argc, argv, flags);
enc = str_true_enc(str); enc = str_true_enc(str);
if (case_option_single_p(flags, enc, str)) { if (case_option_single_p(flags, enc, str)) {
ret = rb_str_new_with_class(str, RSTRING_PTR(str), RSTRING_LEN(str)); ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
str_enc_copy(ret, str); str_enc_copy(ret, str);
upcase_single(ret); upcase_single(ret);
} }
else if (flags&ONIGENC_CASE_ASCII_ONLY) { else if (flags&ONIGENC_CASE_ASCII_ONLY) {
ret = rb_str_new_with_class(str, 0, RSTRING_LEN(str)); ret = rb_str_new(0, RSTRING_LEN(str));
rb_str_ascii_casemap(str, ret, &flags, enc); rb_str_ascii_casemap(str, ret, &flags, enc);
} }
else { else {
@ -6998,12 +7012,12 @@ rb_str_downcase(int argc, VALUE *argv, VALUE str)
flags = check_case_options(argc, argv, flags); flags = check_case_options(argc, argv, flags);
enc = str_true_enc(str); enc = str_true_enc(str);
if (case_option_single_p(flags, enc, str)) { if (case_option_single_p(flags, enc, str)) {
ret = rb_str_new_with_class(str, RSTRING_PTR(str), RSTRING_LEN(str)); ret = rb_str_new(RSTRING_PTR(str), RSTRING_LEN(str));
str_enc_copy(ret, str); str_enc_copy(ret, str);
downcase_single(ret); downcase_single(ret);
} }
else if (flags&ONIGENC_CASE_ASCII_ONLY) { else if (flags&ONIGENC_CASE_ASCII_ONLY) {
ret = rb_str_new_with_class(str, 0, RSTRING_LEN(str)); ret = rb_str_new(0, RSTRING_LEN(str));
rb_str_ascii_casemap(str, ret, &flags, enc); rb_str_ascii_casemap(str, ret, &flags, enc);
} }
else { else {
@ -7078,7 +7092,7 @@ rb_str_capitalize(int argc, VALUE *argv, VALUE str)
enc = str_true_enc(str); enc = str_true_enc(str);
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str; if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str;
if (flags&ONIGENC_CASE_ASCII_ONLY) { if (flags&ONIGENC_CASE_ASCII_ONLY) {
ret = rb_str_new_with_class(str, 0, RSTRING_LEN(str)); ret = rb_str_new(0, RSTRING_LEN(str));
rb_str_ascii_casemap(str, ret, &flags, enc); rb_str_ascii_casemap(str, ret, &flags, enc);
} }
else { else {
@ -7142,9 +7156,9 @@ rb_str_swapcase(int argc, VALUE *argv, VALUE str)
flags = check_case_options(argc, argv, flags); flags = check_case_options(argc, argv, flags);
enc = str_true_enc(str); enc = str_true_enc(str);
if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str; if (RSTRING_LEN(str) == 0 || !RSTRING_PTR(str)) return str_duplicate(rb_cString, str);
if (flags&ONIGENC_CASE_ASCII_ONLY) { if (flags&ONIGENC_CASE_ASCII_ONLY) {
ret = rb_str_new_with_class(str, 0, RSTRING_LEN(str)); ret = rb_str_new(0, RSTRING_LEN(str));
rb_str_ascii_casemap(str, ret, &flags, enc); rb_str_ascii_casemap(str, ret, &flags, enc);
} }
else { else {
@ -7518,7 +7532,7 @@ rb_str_tr_bang(VALUE str, VALUE src, VALUE repl)
static VALUE static VALUE
rb_str_tr(VALUE str, VALUE src, VALUE repl) rb_str_tr(VALUE str, VALUE src, VALUE repl)
{ {
str = rb_str_dup(str); str = str_duplicate(rb_cString, str);
tr_trans(str, src, repl, 0); tr_trans(str, src, repl, 0);
return str; return str;
} }
@ -7697,7 +7711,7 @@ rb_str_delete_bang(int argc, VALUE *argv, VALUE str)
static VALUE static VALUE
rb_str_delete(int argc, VALUE *argv, VALUE str) rb_str_delete(int argc, VALUE *argv, VALUE str)
{ {
str = rb_str_dup(str); str = str_duplicate(rb_cString, str);
rb_str_delete_bang(argc, argv, str); rb_str_delete_bang(argc, argv, str);
return str; return str;
} }
@ -7805,7 +7819,7 @@ rb_str_squeeze_bang(int argc, VALUE *argv, VALUE str)
static VALUE static VALUE
rb_str_squeeze(int argc, VALUE *argv, VALUE str) rb_str_squeeze(int argc, VALUE *argv, VALUE str)
{ {
str = rb_str_dup(str); str = str_duplicate(rb_cString, str);
rb_str_squeeze_bang(argc, argv, str); rb_str_squeeze_bang(argc, argv, str);
return str; return str;
} }
@ -7842,7 +7856,7 @@ rb_str_tr_s_bang(VALUE str, VALUE src, VALUE repl)
static VALUE static VALUE
rb_str_tr_s(VALUE str, VALUE src, VALUE repl) rb_str_tr_s(VALUE str, VALUE src, VALUE repl)
{ {
str = rb_str_dup(str); str = str_duplicate(rb_cString, str);
tr_trans(str, src, repl, 1); tr_trans(str, src, repl, 1);
return str; return str;
} }
@ -7986,12 +8000,12 @@ split_string(VALUE result, VALUE str, long beg, long len, long empty_count)
/* make different substrings */ /* make different substrings */
if (result) { if (result) {
do { do {
rb_ary_push(result, str_new_empty(str)); rb_ary_push(result, str_new_empty_String(str));
} while (--empty_count > 0); } while (--empty_count > 0);
} }
else { else {
do { do {
rb_yield(str_new_empty(str)); rb_yield(str_new_empty_String(str));
} while (--empty_count > 0); } while (--empty_count > 0);
} }
} }
@ -8107,7 +8121,7 @@ rb_str_split_m(int argc, VALUE *argv, VALUE str)
else if (lim == 1) { else if (lim == 1) {
if (RSTRING_LEN(str) == 0) if (RSTRING_LEN(str) == 0)
return result ? rb_ary_new2(0) : str; return result ? rb_ary_new2(0) : str;
tmp = rb_str_dup(str); tmp = str_duplicate(rb_cString, str);
if (!result) { if (!result) {
rb_yield(tmp); rb_yield(tmp);
return str; return str;
@ -9194,7 +9208,7 @@ static VALUE
rb_str_chomp(int argc, VALUE *argv, VALUE str) rb_str_chomp(int argc, VALUE *argv, VALUE str)
{ {
VALUE rs = chomp_rs(argc, argv); VALUE rs = chomp_rs(argc, argv);
if (NIL_P(rs)) return rb_str_dup(str); if (NIL_P(rs)) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, 0, chompped_length(str, rs)); return rb_str_subseq(str, 0, chompped_length(str, rs));
} }
@ -9281,7 +9295,7 @@ rb_str_lstrip(VALUE str)
long len, loffset; long len, loffset;
RSTRING_GETMEM(str, start, len); RSTRING_GETMEM(str, start, len);
loffset = lstrip_offset(str, start, start+len, STR_ENC_GET(str)); loffset = lstrip_offset(str, start, start+len, STR_ENC_GET(str));
if (loffset <= 0) return rb_str_dup(str); if (loffset <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, loffset, len - loffset); return rb_str_subseq(str, loffset, len - loffset);
} }
@ -9374,7 +9388,7 @@ rb_str_rstrip(VALUE str)
RSTRING_GETMEM(str, start, olen); RSTRING_GETMEM(str, start, olen);
roffset = rstrip_offset(str, start, start+olen, enc); roffset = rstrip_offset(str, start, start+olen, enc);
if (roffset <= 0) return rb_str_dup(str); if (roffset <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, 0, olen-roffset); return rb_str_subseq(str, 0, olen-roffset);
} }
@ -9447,7 +9461,7 @@ rb_str_strip(VALUE str)
loffset = lstrip_offset(str, start, start+olen, enc); loffset = lstrip_offset(str, start, start+olen, enc);
roffset = rstrip_offset(str, start+loffset, start+olen, enc); roffset = rstrip_offset(str, start+loffset, start+olen, enc);
if (loffset <= 0 && roffset <= 0) return rb_str_dup(str); if (loffset <= 0 && roffset <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, loffset, olen-loffset-roffset); return rb_str_subseq(str, loffset, olen-loffset-roffset);
} }
@ -9843,7 +9857,7 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
} }
} }
len = str_strlen(str, enc); /* rb_enc_check */ len = str_strlen(str, enc); /* rb_enc_check */
if (width < 0 || len >= width) return rb_str_dup(str); if (width < 0 || len >= width) return str_duplicate(rb_cString, str);
n = width - len; n = width - len;
llen = (jflag == 'l') ? 0 : ((jflag == 'r') ? n : n/2); llen = (jflag == 'l') ? 0 : ((jflag == 'r') ? n : n/2);
rlen = n - llen; rlen = n - llen;
@ -9859,7 +9873,7 @@ rb_str_justify(int argc, VALUE *argv, VALUE str, char jflag)
rb_raise(rb_eArgError, "argument too big"); rb_raise(rb_eArgError, "argument too big");
} }
len += size; len += size;
res = str_new0(rb_obj_class(str), 0, len, termlen); res = str_new0(rb_cString, 0, len, termlen);
p = RSTRING_PTR(res); p = RSTRING_PTR(res);
if (flen <= 1) { if (flen <= 1) {
memset(p, *f, llen); memset(p, *f, llen);
@ -10006,7 +10020,7 @@ rb_str_partition(VALUE str, VALUE sep)
RSTRING_LEN(str)-pos-RSTRING_LEN(sep))); RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
failed: failed:
return rb_ary_new3(3, rb_str_dup(str), str_new_empty(str), str_new_empty(str)); return rb_ary_new3(3, str_duplicate(rb_cString, str), str_new_empty_String(str), str_new_empty_String(str));
} }
/* /*
@ -10054,7 +10068,7 @@ rb_str_rpartition(VALUE str, VALUE sep)
rb_str_subseq(str, pos+RSTRING_LEN(sep), rb_str_subseq(str, pos+RSTRING_LEN(sep),
RSTRING_LEN(str)-pos-RSTRING_LEN(sep))); RSTRING_LEN(str)-pos-RSTRING_LEN(sep)));
failed: failed:
return rb_ary_new3(3, str_new_empty(str), str_new_empty(str), rb_str_dup(str)); return rb_ary_new3(3, str_new_empty_String(str), str_new_empty_String(str), str_duplicate(rb_cString, str));
} }
/* /*
@ -10200,7 +10214,7 @@ rb_str_delete_prefix(VALUE str, VALUE prefix)
long prefixlen; long prefixlen;
prefixlen = deleted_prefix_length(str, prefix); prefixlen = deleted_prefix_length(str, prefix);
if (prefixlen <= 0) return rb_str_dup(str); if (prefixlen <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, prefixlen, RSTRING_LEN(str) - prefixlen); return rb_str_subseq(str, prefixlen, RSTRING_LEN(str) - prefixlen);
} }
@ -10286,7 +10300,7 @@ rb_str_delete_suffix(VALUE str, VALUE suffix)
long suffixlen; long suffixlen;
suffixlen = deleted_suffix_length(str, suffix); suffixlen = deleted_suffix_length(str, suffix);
if (suffixlen <= 0) return rb_str_dup(str); if (suffixlen <= 0) return str_duplicate(rb_cString, str);
return rb_str_subseq(str, 0, RSTRING_LEN(str) - suffixlen); return rb_str_subseq(str, 0, RSTRING_LEN(str) - suffixlen);
} }
@ -10417,7 +10431,7 @@ rb_str_ellipsize(VALUE str, long len)
else if (len <= ellipsislen || else if (len <= ellipsislen ||
!(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) { !(e = rb_enc_step_back(p, e, e, len = ellipsislen, enc))) {
if (rb_enc_asciicompat(enc)) { if (rb_enc_asciicompat(enc)) {
ret = rb_str_new_with_class(str, ellipsis, len); ret = rb_str_new(ellipsis, len);
rb_enc_associate(ret, enc); rb_enc_associate(ret, enc);
} }
else { else {
@ -10742,7 +10756,7 @@ str_scrub(int argc, VALUE *argv, VALUE str)
{ {
VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil; VALUE repl = argc ? (rb_check_arity(argc, 0, 1), argv[0]) : Qnil;
VALUE new = rb_str_scrub(str, repl); VALUE new = rb_str_scrub(str, repl);
return NIL_P(new) ? rb_str_dup(str): new; return NIL_P(new) ? str_duplicate(rb_cString, str): new;
} }
/* /*

View file

@ -13,13 +13,13 @@ class Test_StringCStr < Test::Unit::TestCase
end end
def test_long def test_long
s = Bug::String.new("abcdef")*100000 s = Bug::String.new(Bug::String.new("abcdef")*100000)
s.cstr_unterm('x') s.cstr_unterm('x')
assert_equal(0, s.cstr_term, Bug4319) assert_equal(0, s.cstr_term, Bug4319)
end end
def test_shared def test_shared
s = Bug::String.new("abcdef")*5 s = Bug::String.new(Bug::String.new("abcdef")*5)
s = s.unterminated_substring(0, 29) s = s.unterminated_substring(0, 29)
assert_equal(0, s.cstr_term, Bug4319) assert_equal(0, s.cstr_term, Bug4319)
end end
@ -28,7 +28,7 @@ class Test_StringCStr < Test::Unit::TestCase
s0 = Bug::String.new("abcdefgh"*8) s0 = Bug::String.new("abcdefgh"*8)
[4, 4*3-1, 8*3-1, 64].each do |n| [4, 4*3-1, 8*3-1, 64].each do |n|
s = s0[0, n] s = Bug::String.new(s0[0, n])
s.cstr_unterm('x') s.cstr_unterm('x')
s.freeze s.freeze
assert_equal(0, s.cstr_term) assert_equal(0, s.cstr_term)
@ -67,7 +67,7 @@ class Test_StringCStr < Test::Unit::TestCase
n = 100 n = 100
len = str.size * n len = str.size * n
WCHARS.each do |enc| WCHARS.each do |enc|
s = Bug::String.new(str.encode(enc))*n s = Bug::String.new(Bug::String.new(str.encode(enc))*n)
s.cstr_unterm('x') s.cstr_unterm('x')
assert_nothing_raised(ArgumentError, enc.name) {s.cstr_term} assert_nothing_raised(ArgumentError, enc.name) {s.cstr_term}
s.set_len(s.bytesize / 2) s.set_len(s.bytesize / 2)

View file

@ -10,7 +10,7 @@ class Test_StringEllipsize < Test::Unit::TestCase
def assert_equal_with_class(expected, result, *rest) def assert_equal_with_class(expected, result, *rest)
assert_equal(expected.encoding, result.encoding, *rest) assert_equal(expected.encoding, result.encoding, *rest)
assert_equal(expected, result, result.encoding.name) assert_equal(expected, result, result.encoding.name)
assert_instance_of(Bug::String, result, *rest) assert_instance_of(String, result, *rest)
end end
def test_longer def test_longer

View file

@ -2089,6 +2089,8 @@ CODE
def test_swapcase def test_swapcase
assert_equal(S("hi&LOW"), S("HI&low").swapcase) assert_equal(S("hi&LOW"), S("HI&low").swapcase)
s = S("")
assert_not_same(s, s.swapcase)
end end
def test_swapcase! def test_swapcase!