Skip to content

Commit

Permalink
suggestions
Browse files Browse the repository at this point in the history
Co-Authored-By: Steven G. Johnson <[email protected]>
  • Loading branch information
IanButterworth and stevengj committed Aug 7, 2024
1 parent 1b187ae commit 6c222fe
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,7 @@ function rpad(
end

"""
rtrunc(str::AbstractString, maxwidth::Int, replace_str::AbstractString = "…")
rtrunc(str::AbstractString, maxwidth::Integer, replace_str::AbstractString = "…")
Truncate `str` to at most `maxwidth` columns (as estimated by [`textwidth`](@ref)), replacing the last characters
with `replacement` if necessary. The default replacement string is "…".
Expand All @@ -536,18 +536,18 @@ julia> rtrunc("foo", 3)
See also [`ltrunc`](@ref) and [`ctrunc`](@ref).
"""
function rtrunc(str::AbstractString, maxwidth::Int, replacement::Union{AbstractString,Char} = '')
ret = string_truncate_boundaries(str, maxwidth, replacement, Val(:right))
function rtrunc(str::AbstractString, maxwidth::Integer, replacement::Union{AbstractString,AbstractChar} = '')
ret = string_truncate_boundaries(str, Int(maxwidth), replacement, Val(:right))
if isnothing(ret)
return str
return string(str)
else
left, _ = ret::Tuple{Int,Int}
@views return str[begin:left] * replacement
end
end

"""
ltrunc(str::AbstractString, maxwidth::Int, replace_str::AbstractString = "…")
ltrunc(str::AbstractString, maxwidth::Integer, replace_str::AbstractString = "…")
Truncate `str` to at most `maxwidth` columns (as estimated by [`textwidth`](@ref)), replacing the first characters
with `replacement` if necessary. The default replacement string is "…".
Expand All @@ -569,18 +569,18 @@ julia> ltrunc("foo", 3)
See also [`rtrunc`](@ref) and [`ctrunc`](@ref).
"""
function ltrunc(str::AbstractString, maxwidth::Int, replacement::Union{AbstractString,Char} = '')
ret = string_truncate_boundaries(str, maxwidth, replacement, Val(:left))
function ltrunc(str::AbstractString, maxwidth::Integer, replacement::Union{AbstractString,AbstractChar} = '')
ret = string_truncate_boundaries(str, Int(maxwidth), replacement, Val(:left))
if isnothing(ret)
return str
return string(str)
else
_, right = ret::Tuple{Int,Int}
@views return replacement * str[right:end]
end
end

"""
ctrunc(str::AbstractString, maxwidth::Int, replacement::Union{AbstractString,Char} = '…'; prefer_left::Bool = true)
ctrunc(str::AbstractString, maxwidth::Integer, replacement::Union{AbstractString,AbstractChar} = '…'; prefer_left::Bool = true)
Truncate `str` to at most `maxwidth` columns (as estimated by [`textwidth`](@ref)), replacing the middle characters
with `replacement` if necessary. The default replacement string is "…". By default, the truncation
Expand All @@ -603,10 +603,10 @@ julia> ctrunc("foo", 3)
See also [`ltrunc`](@ref) and [`rtrunc`](@ref).
"""
function ctrunc(str::AbstractString, maxwidth::Int, replacement::Union{AbstractString,Char} = ''; prefer_left::Bool = true)
ret = string_truncate_boundaries(str, maxwidth, replacement, Val(:center), prefer_left)
function ctrunc(str::AbstractString, maxwidth::Integer, replacement::Union{AbstractString,AbstractChar} = ''; prefer_left::Bool = true)
ret = string_truncate_boundaries(str, Int(maxwidth), replacement, Val(:center), prefer_left)
if isnothing(ret)
return str
return string(str)
else
left, right = ret::Tuple{Int,Int}
@views return str[begin:left] * replacement * str[right:end]
Expand All @@ -615,8 +615,8 @@ end

function string_truncate_boundaries(
str::AbstractString,
maxwidth::Int,
replacement::Union{AbstractString,Char},
maxwidth::Integer,
replacement::Union{AbstractString,AbstractChar},
::Val{mode},
prefer_left::Bool = true) where {mode}

Expand All @@ -632,7 +632,7 @@ function string_truncate_boundaries(

l0, _ = left, right = firstindex(str), lastindex(str)
width = textwidth(replacement)
while true
@inbounds while true
if mode === :left || (mode === :center && (!prefer_left || left > l0))
(width += textwidth(str[right])) <= maxwidth || break
right = prevind(str, right)
Expand Down

0 comments on commit 6c222fe

Please sign in to comment.