@@ -514,10 +514,10 @@ function rpad(
514514end
515515
516516"""
517- rtrunc(str::AbstractString, w ::Int, replace_str::AbstractString = "…")
517+ rtrunc(str::AbstractString, maxwidth ::Int, replace_str::AbstractString = "…")
518518
519- Truncate `str` to at most `w` characters (see [`textwidth`](@ref)), replacing the last characters
520- with `replace_str ` if necessary. The default replacement string is "…".
519+ Truncate `str` to at most `maxwidth` columns (as estimated by [`textwidth`](@ref)), replacing the last characters
520+ with `replacement ` if necessary. The default replacement string is "…".
521521
522522# Examples
523523```jldoctest
@@ -536,26 +536,13 @@ julia> rtrunc("foo", 3)
536536
537537See also [`ltrunc`](@ref) and [`ctrunc`](@ref).
538538"""
539- function rtrunc (str:: AbstractString , w:: Int , replace_str:: AbstractString = " …" )
540- if textwidth (str) <= w
541- return str
542- else
543- i, accwidth = firstindex (str), textwidth (replace_str)
544- while true
545- tw = textwidth (str[i])
546- accwidth + tw <= w || break # no need to check if we go out of bounds because of the first if branch
547- accwidth += tw
548- i = nextind (str, i)
549- end
550- return str[firstindex (str): prevind (str, i)] * replace_str
551- end
552- end
539+ rtrunc (str:: AbstractString , maxwidth:: Int , replacement:: Union{AbstractString,Char} = ' …' ) = struncate (str, maxwidth, replacement, :right )
553540
554541"""
555- ltrunc(str::AbstractString, w ::Int, replace_str::AbstractString = "…")
542+ ltrunc(str::AbstractString, maxwidth ::Int, replace_str::AbstractString = "…")
556543
557- Truncate `str` to at most `w` characters (see [`textwidth`](@ref)), replacing the first characters
558- with `replace_str ` if necessary. The default replacement string is "…".
544+ Truncate `str` to at most `maxwidth` columns (as estimated by [`textwidth`](@ref)), replacing the first characters
545+ with `replacement ` if necessary. The default replacement string is "…".
559546
560547# Examples
561548```jldoctest
@@ -574,26 +561,13 @@ julia> ltrunc("foo", 3)
574561
575562See also [`rtrunc`](@ref) and [`ctrunc`](@ref).
576563"""
577- function ltrunc (str:: AbstractString , w:: Int , replace_str:: AbstractString = " …" )
578- if textwidth (str) <= w
579- return str
580- else
581- i, accwidth = lastindex (str), textwidth (replace_str)
582- while true
583- tw = textwidth (str[i])
584- accwidth + tw <= w || break # no need to check if we go out of bounds because of the first if branch
585- accwidth += tw
586- i = prevind (str, i)
587- end
588- return replace_str * str[nextind (str, i): lastindex (str)]
589- end
590- end
564+ ltrunc (str:: AbstractString , maxwidth:: Int , replacement:: Union{AbstractString,Char} = ' …' ) = struncate (str, maxwidth, replacement, :left )
591565
592566"""
593- ctrunc(str::AbstractString, w ::Int, replace_str:: AbstractString = "…" ; prefer_left::Bool = true)
567+ ctrunc(str::AbstractString, maxwidth ::Int, replacement::Union{ AbstractString,Char} = '…' ; prefer_left::Bool = true)
594568
595- Truncate `str` to at most `w` characters (see [`textwidth`](@ref)), replacing the middle characters
596- with `replace_str ` if necessary. The default replacement string is "…". By default, the truncation
569+ Truncate `str` to at most `maxwidth` columns (as estimated by [`textwidth`](@ref)), replacing the middle characters
570+ with `replacement ` if necessary. The default replacement string is "…". By default, the truncation
597571prefers keeping chars on the left, but this can be changed by setting `prefer_left` to `false`.
598572
599573# Examples
@@ -613,24 +587,25 @@ julia> ctrunc("foo", 3)
613587
614588See also [`ltrunc`](@ref) and [`rtrunc`](@ref).
615589"""
616- function ctrunc (str:: AbstractString , w:: Int , replace_str:: AbstractString = " …" ; prefer_left:: Bool = true )
617- if textwidth (str) <= w
618- return str
619- else
620- l, r, accwidth = firstindex (str), lastindex (str), textwidth (replace_str)
621- isleft = prefer_left
622- while true
623- tw = if isleft textwidth (str[l]) else textwidth (str[r]) end
624- (accwidth += tw) <= w || break
625- if isleft
626- l = nextind (str, l)
627- else
628- r = prevind (str, r)
629- end
630- isleft = ! isleft
590+ ctrunc (str:: AbstractString , maxwidth:: Int , replacement:: Union{AbstractString,Char} = ' …' ; prefer_left:: Bool = true ) =
591+ struncate (str, maxwidth, replacement, :center , prefer_left)
592+
593+ function struncate (str:: AbstractString , maxwidth:: Int , replacement:: Union{AbstractString,Char} , mode:: Symbol = :center , prefer_left:: Bool = true )
594+ maxwidth >= 0 || throw (ArgumentError (" maxwidth $maxwidth should be non-negative" ))
595+ textwidth (str) <= maxwidth && return str
596+ l0, _ = left, right = firstindex (str), lastindex (str)
597+ width = textwidth (replacement)
598+ while true
599+ if mode === :left || (mode === :center && (! prefer_left || left > l0))
600+ (width += textwidth (str[right])) <= maxwidth || break
601+ right = prevind (str, right)
602+ end
603+ if mode ∈ (:right , :center )
604+ (width += textwidth (str[left])) <= maxwidth || break
605+ left = nextind (str, left)
631606 end
632- return str[firstindex (str): prevind (str, l)] * replace_str * str[nextind (str, r): lastindex (str)]
633607 end
608+ @views return str[begin : prevind (str, left)] * replacement * str[nextind (str, right): end ]
634609end
635610
636611"""
0 commit comments