Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions spec/std/string_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -3145,6 +3145,62 @@ describe "String" do
it { expect_raises(IndexError) { "セキロ:シャドウズ ダイ トゥワイス".delete_at(-19..1) } }
end
end

describe "ensure_suffix" do
context "with string suffix" do
it "adds suffix if not present" do
"foo".ensure_suffix("bar").should eq("foobar")
"foo".ensure_suffix("FOO").should eq("fooFOO")
"foo".ensure_suffix("").should eq("foo")
"foobar".ensure_suffix("arr").should eq("foobararr")
end

it "does not add suffix if already present" do
"foobar".ensure_suffix("bar").should eq("foobar")
"FOOBAR".ensure_suffix("BAR").should eq("FOOBAR")
end
end

context "with char suffix" do
it "adds suffix if not present" do
"foo".ensure_suffix('b').should eq("foob")
"foo".ensure_suffix('O').should eq("fooO")
end

it "does not add suffix if already present" do
"foob".ensure_suffix('b').should eq("foob")
"FOOB".ensure_suffix('B').should eq("FOOB")
end
end
end

describe "ensure_prefix" do
context "with string prefix" do
it "adds prefix if not present" do
"foo".ensure_prefix("bar").should eq("barfoo")
"foo".ensure_prefix("FOO").should eq("FOOfoo")
"foo".ensure_prefix("").should eq("foo")
"foo".ensure_prefix("barf").should eq("barffoo")
end

it "does not add prefix if already present" do
"foobar".ensure_prefix("foo").should eq("foobar")
"FOOBAR".ensure_prefix("FOO").should eq("FOOBAR")
end
end

context "with char prefix" do
it "adds prefix if not present" do
"foo".ensure_prefix('b').should eq("bfoo")
"foo".ensure_prefix('F').should eq("Ffoo")
end

it "does not add prefix if already present" do
"bfoo".ensure_prefix('b').should eq("bfoo")
"BFOO".ensure_prefix('B').should eq("BFOO")
end
end
end
end

class String
Expand Down
30 changes: 30 additions & 0 deletions src/string.cr
Original file line number Diff line number Diff line change
Expand Up @@ -5584,6 +5584,36 @@ class String
self
end

# Returns `self` if it starts with the given *prefix*. Otherwise, returns a new
# `String` with the *prefix* prepended.
#
# ```
# "llo!".ensure_prefix("He") # => "Hello!"
# "Hello!".ensure_prefix("He") # => "Hello!"
# "ello".ensure_prefix('H') # => "Hello!"
# "Hello!".ensure_prefix('H') # => "Hello!"
# ```
def ensure_prefix(prefix : String | Char) : self
return self if starts_with?(prefix)

"#{prefix}#{self}"
end

# Returns `self` if it ends with the given *suffix*. Otherwise, returns a new
# `String` with the *suffix* appended.
#
# ```
# "Hell".ensure_suffix("o!") # => "Hello!"
# "Hello!".ensure_suffix("o!") # => "Hello!"
# "Hello".ensure_suffix('!') # => "Hello!"
# "Hello!".ensure_suffix('!') # => "Hello!"
# ```
def ensure_suffix(suffix : String | Char) : self
return self if ends_with?(suffix)

"#{self}#{suffix}"
end

# :nodoc:
def self.check_capacity_in_bounds(capacity) : Nil
if capacity < 0
Expand Down