Skip to content

Commit

Permalink
Showing 2 changed files with 15 additions and 0 deletions.
12 changes: 12 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
@@ -1151,6 +1151,18 @@ describe "String" do
str2.bytesize.should eq(8)
str2.size.should eq(6)
end

it "does when right is empty" do
str1 = "foo"
str2 = ""
(str1 + str2).should be(str1)
end

it "does when left is empty" do
str1 = ""
str2 = "foo"
(str1 + str2).should be(str2)
end
end

it "does %" do
3 changes: 3 additions & 0 deletions src/string.cr
Original file line number Diff line number Diff line change
@@ -1936,6 +1936,9 @@ class String
# "abc" + 'd' # => "abcd"
# ```
def +(other : self)
return self if other.empty?
return other if self.empty?

size = bytesize + other.bytesize
String.new(size) do |buffer|
buffer.copy_from(to_unsafe, bytesize)

0 comments on commit 9df0011

Please sign in to comment.