Skip to content

Commit

Permalink
fix empty case in chopsuffix
Browse files Browse the repository at this point in the history
  • Loading branch information
kcajf committed May 29, 2021
1 parent 7f1f36c commit 8457935
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,10 +242,10 @@ julia> chopsuffix("Hamburger", "hotdog")
```
"""
function chopsuffix(s::AbstractString, suffix::AbstractString)
if endswith(s, suffix)
SubString(s, firstindex(s), prevind(s, lastindex(s), length(suffix)))
else
if isempty(s) || !endswith(s, suffix)
SubString(s)
else
SubString(s, firstindex(s), prevind(s, lastindex(s), length(suffix)))
end
end

Expand Down
2 changes: 2 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ end
@test chopprefix("εfoo", "ε") == "foo"
@test chopprefix("ofoε", "o") == "foε"
@test chopprefix("∃∃∃∃", "") == "∃∃∃"
@test chopprefix("∃∃∃∃", "") == "∃∃∃∃"

@test chopsuffix("fo∀\n", "bog") == "fo∀\n"
@test chopsuffix("fo∀\n", "\n∀foΔ") == "fo∀\n"
Expand All @@ -376,6 +377,7 @@ end
@test chopsuffix("fooε", "ε") == "foo"
@test chopsuffix("εofo", "o") == "εof"
@test chopsuffix("∃∃∃∃", "") == "∃∃∃"
@test chopsuffix("∃∃∃∃", "") == "∃∃∃∃"

@test isa(chomp("foo"), SubString)
@test isa(chop("foo"), SubString)
Expand Down

0 comments on commit 8457935

Please sign in to comment.