Skip to content

Commit 99e0f78

Browse files
authored
Allow removing an AbstractChar with chopprefix/chopsuffix (JuliaLang#59425)
1 parent d807b70 commit 99e0f78

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ New library features
6363
* `sort(keys(::Dict))` and `sort(values(::Dict))` now automatically collect, they previously threw ([#56978]).
6464
* `Base.AbstractOneTo` is added as a supertype of one-based axes, with `Base.OneTo` as its subtype ([#56902]).
6565
* `takestring!(::IOBuffer)` removes the content from the buffer, returning the content as a `String`.
66+
* `chopprefix` and `chopsuffix` can now also accept an `AbstractChar` as the prefix/suffix to remove.
6667
* The `macroexpand` (with default true) and the new `macroexpand!` (with default false)
6768
functions now support a `legacyscope` boolean keyword argument to control whether to run
6869
the legacy scope resolution pass over the result. The legacy scope resolution code has

base/strings/util.jl

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ end
232232
# chop(s::AbstractString) = SubString(s, firstindex(s), prevind(s, lastindex(s)))
233233

234234
"""
235-
chopprefix(s::AbstractString, prefix::Union{AbstractString,Regex})::SubString
235+
chopprefix(s::AbstractString, prefix::Union{AbstractString,Regex,AbstractChar})::SubString
236236
237237
Remove the prefix `prefix` from `s`. If `s` does not start with `prefix`, a string equal to `s` is returned.
238238
@@ -241,6 +241,9 @@ See also [`chopsuffix`](@ref).
241241
!!! compat "Julia 1.8"
242242
This function is available as of Julia 1.8.
243243
244+
!!! compat "Julia 1.13"
245+
The method which accepts an `AbstractChar` prefix is available as of Julia 1.13.
246+
244247
# Examples
245248
```jldoctest
246249
julia> chopprefix("Hamburger", "Ham")
@@ -272,8 +275,16 @@ function chopprefix(s::Union{String, SubString{String}},
272275
end
273276
end
274277

278+
function chopprefix(s::AbstractString, prefix::AbstractChar)
279+
if !isempty(s) && first(s) == prefix
280+
return SubString(s, nextind(s, firstindex(s)))
281+
else
282+
return SubString(s)
283+
end
284+
end
285+
275286
"""
276-
chopsuffix(s::AbstractString, suffix::Union{AbstractString,Regex})::SubString
287+
chopsuffix(s::AbstractString, suffix::Union{AbstractString,Regex,AbstractChar})::SubString
277288
278289
Remove the suffix `suffix` from `s`. If `s` does not end with `suffix`, a string equal to `s` is returned.
279290
@@ -282,6 +293,9 @@ See also [`chopprefix`](@ref).
282293
!!! compat "Julia 1.8"
283294
This function is available as of Julia 1.8.
284295
296+
!!! compat "Julia 1.13"
297+
The method which accepts an `AbstractChar` suffix is available as of Julia 1.13.
298+
285299
# Examples
286300
```jldoctest
287301
julia> chopsuffix("Hamburger", "er")
@@ -315,6 +329,13 @@ function chopsuffix(s::Union{String, SubString{String}},
315329
end
316330
end
317331

332+
function chopsuffix(s::AbstractString, suffix::AbstractChar)
333+
if !isempty(s) && last(s) == suffix
334+
return SubString(s, firstindex(s), prevind(s, lastindex(s)))
335+
else
336+
return SubString(s)
337+
end
338+
end
318339

319340
"""
320341
chomp(s::AbstractString)::SubString

test/strings/util.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -709,6 +709,11 @@ end
709709

710710
@test isa(chopprefix(S("foo"), "fo"), SubString)
711711
@test isa(chopsuffix(S("foo"), "oo"), SubString)
712+
713+
@test chopprefix(S(""), 'z') == chopsuffix(S(""), 'z') == ""
714+
@test chopprefix(S("吃齋"), '🍖') == chopsuffix(S("吃齋"), '🍖') == "吃齋"
715+
@test chopprefix(S("äwesome"), 'ä') == "wesome"
716+
@test chopsuffix(S("äwesome"), 'e') == "äwesom"
712717
end
713718
end
714719

0 commit comments

Comments
 (0)