From 0dded87808a2640558e0364f7e46da71ad2f3fac Mon Sep 17 00:00:00 2001 From: Kenta Sato Date: Tue, 12 Mar 2019 04:06:52 +0900 Subject: [PATCH] allow chop to take an empty string (#31312) (cherry picked from commit 023c8e4b068eff37e8dcafc9fed317f25eb871b8) --- base/strings/util.jl | 3 +++ test/strings/util.jl | 1 + 2 files changed, 4 insertions(+) diff --git a/base/strings/util.jl b/base/strings/util.jl index ad27f9c7f84e6..ff9beab76b9c5 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -96,6 +96,9 @@ julia> chop(a, head = 5, tail = 5) ``` """ function chop(s::AbstractString; head::Integer = 0, tail::Integer = 1) + if isempty(s) + return SubString(s) + end SubString(s, nextind(s, firstindex(s), head), prevind(s, lastindex(s), tail)) end diff --git a/test/strings/util.jl b/test/strings/util.jl index 17d1f9355006c..20de96ee9ce87 100644 --- a/test/strings/util.jl +++ b/test/strings/util.jl @@ -301,6 +301,7 @@ end @test chomp("foo\r\n") == "foo" @test chomp("fo∀\r\n") == "fo∀" @test chomp("fo∀") == "fo∀" + @test chop("") == "" @test chop("fooε") == "foo" @test chop("foεo") == "foε" @test chop("∃∃∃∃") == "∃∃∃"