Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge strwidth and charwidth to textwidth #23667

Merged
merged 1 commit into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ Library improvements

* The functions `strip`, `lstrip` and `rstrip` now return `SubString` ([#22496]).

* The functions `strwidth` and `charwidth` have been merged into `textwidth`([#20816]).

* The functions `base` and `digits` digits now accept a negative
base (like `ndigits` did) ([#21692]).

Expand Down
6 changes: 5 additions & 1 deletion base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1651,7 +1651,7 @@ end
end

# PR #23187
@deprecate cpad(s, n::Integer, p=" ") rpad(lpad(s, div(n+strwidth(s), 2), p), n, p) false
@deprecate cpad(s, n::Integer, p=" ") rpad(lpad(s, div(n+textwidth(s), 2), p), n, p) false

# PR #22088
function hex2num(s::AbstractString)
Expand Down Expand Up @@ -1877,6 +1877,10 @@ end
nothing
end

# issue #20816
@deprecate strwidth textwidth
@deprecate charwidth textwidth

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
4 changes: 2 additions & 2 deletions base/dict.jl
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
truncwidth = strwidth(truncmark)
truncwidth = textwidth(truncmark)
(width <= 0 || width < truncwidth) && return ""

wid = truncidx = lastidx = 0
idx = start(str)
while !done(str, idx)
lastidx = idx
c, idx = next(str, idx)
wid += charwidth(c)
wid += textwidth(c)
wid >= width - truncwidth && truncidx == 0 && (truncidx = lastidx)
(wid >= width || c in chars) && break
end
Expand Down
3 changes: 1 addition & 2 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ export
bin,
bits,
bytes2hex,
charwidth,
chomp,
chop,
chr2ind,
Expand Down Expand Up @@ -800,8 +799,8 @@ export
sprint,
string,
strip,
strwidth,
summary,
textwidth,
titlecase,
transcode,
ucfirst,
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,7 @@ end

function warnbanner(msg...; label="[ WARNING ]", prefix="")
cols = Base.displaysize(STDERR)[2]
str = rpad(lpad(label, div(cols+strwidth(label), 2), "="), cols, "=")
str = rpad(lpad(label, div(cols+textwidth(label), 2), "="), cols, "=")
warn(prefix="", str)
println(STDERR)
warn(prefix=prefix, msg...)
Expand Down
8 changes: 4 additions & 4 deletions base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,11 +617,11 @@ function gen_c(flags::String, width::Int, precision::Int, c::Char)
blk = Expr(:block, :($x = Char($x)))
if width > 1 && !('-' in flags)
p = '0' in flags ? '0' : ' '
push!(blk.args, pad(width-1, :($width-charwidth($x)), p))
push!(blk.args, pad(width-1, :($width-textwidth($x)), p))
end
push!(blk.args, :(write(out, $x)))
if width > 1 && '-' in flags
push!(blk.args, pad(width-1, :($width-charwidth($x)), ' '))
push!(blk.args, pad(width-1, :($width-textwidth($x)), ' '))
end
:(($x)::Integer), blk
end
Expand Down Expand Up @@ -652,11 +652,11 @@ function gen_s(flags::String, width::Int, precision::Int, c::Char)
push!(blk.args, :($x = _limit($x, $precision)))
end
if !('-' in flags)
push!(blk.args, pad(width, :($width-strwidth($x)), ' '))
push!(blk.args, pad(width, :($width-textwidth($x)), ' '))
end
push!(blk.args, :(write(out, $x)))
if '-' in flags
push!(blk.args, pad(width, :($width-strwidth($x)), ' '))
push!(blk.args, pad(width, :($width-textwidth($x)), ' '))
end
else
if precision!=-1
Expand Down
16 changes: 8 additions & 8 deletions base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,8 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal, buf
while moreinput
l = readline(buf, chomp=false)
moreinput = endswith(l, "\n")
# We need to deal with on-screen characters, so use strwidth to compute occupied columns
llength = strwidth(l)
# We need to deal with on-screen characters, so use textwidth to compute occupied columns
llength = textwidth(l)
slength = sizeof(l)
cur_row += 1
cmove_col(termbuf, lindent + 1)
Expand All @@ -319,7 +319,7 @@ function refresh_multi_line(termbuf::TerminalBuffer, terminal::UnixTerminal, buf
# in this case, we haven't yet written the cursor position
line_pos -= slength # '\n' gets an extra pos
if line_pos < 0 || !moreinput
num_chars = (line_pos >= 0 ? llength : strwidth(l[1:prevind(l, line_pos + slength + 1)]))
num_chars = (line_pos >= 0 ? llength : textwidth(l[1:prevind(l, line_pos + slength + 1)]))
curs_row, curs_pos = divrem(lindent + num_chars - 1, cols)
curs_row += cur_row
curs_pos += 1
Expand Down Expand Up @@ -408,7 +408,7 @@ function edit_move_left(buf::IOBuffer)
#move to the next base UTF8 character to the left
while true
c = char_move_left(buf)
if charwidth(c) != 0 || c == '\n' || position(buf) == 0
if textwidth(c) != 0 || c == '\n' || position(buf) == 0
break
end
end
Expand Down Expand Up @@ -466,7 +466,7 @@ function edit_move_right(buf::IOBuffer)
pos = position(buf)
nextc = read(buf,Char)
seek(buf,pos)
(charwidth(nextc) != 0 || nextc == '\n') && break
(textwidth(nextc) != 0 || nextc == '\n') && break
end
return true
end
Expand Down Expand Up @@ -637,7 +637,7 @@ function edit_backspace(buf::IOBuffer, align::Bool=false, adjust::Bool=false)
newpos = position(buf)
if align && c == ' ' # maybe delete multiple spaces
beg = beginofline(buf, newpos)
align = strwidth(String(buf.data[1+beg:newpos])) % 4
align = textwidth(String(buf.data[1+beg:newpos])) % 4
nonspace = findprev(_notspace, buf.data, newpos)
if newpos - align >= nonspace
newpos -= align
Expand Down Expand Up @@ -950,7 +950,7 @@ end
function write_prompt(terminal, s::Union{AbstractString,Function})
promptstr = prompt_string(s)
write(terminal, promptstr)
strwidth(promptstr)
textwidth(promptstr)
end

### Keymap Support
Expand Down Expand Up @@ -1697,7 +1697,7 @@ function edit_insert_tab(buf::IOBuffer, jump_spaces=false, delete_trailing=jump_
end
end
# align to multiples of 4:
align = 4 - strwidth(String(buf.data[1+beginofline(buf, i):i])) % 4
align = 4 - textwidth(String(buf.data[1+beginofline(buf, i):i])) % 4
edit_insert(buf, ' '^align)
return true
end
Expand Down
2 changes: 1 addition & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1682,7 +1682,7 @@ function print_matrix(io::IO, X::AbstractVecOrMat,
screenwidth -= length(pre) + length(post)
presp = repeat(" ", length(pre)) # indent each row to match pre string
postsp = ""
@assert strwidth(hdots) == strwidth(ddots)
@assert textwidth(hdots) == textwidth(ddots)
sepsize = length(sep)
rowsA, colsA = indices(X,1), indices(X,2)
m, n = length(rowsA), length(colsA)
Expand Down
15 changes: 0 additions & 15 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,21 +389,6 @@ next(e::EachStringIndex, state) = (state, nextind(e.s, state))
done(e::EachStringIndex, state) = done(e.s, state)
eltype(::Type{EachStringIndex}) = Int

## character column width function ##

"""
strwidth(s::AbstractString)

Give the number of columns needed to print a string.

# Examples
```jldoctest
julia> strwidth("March")
5
```
"""
strwidth(s::AbstractString) = (w=0; for c in s; w += charwidth(c); end; w)

"""
isascii(c::Union{Char,AbstractString}) -> Bool

Expand Down
26 changes: 20 additions & 6 deletions base/strings/utf8proc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Base: show, ==, hash, string, Symbol, isless, length, eltype, start, next
export isgraphemebreak, category_code, category_abbrev, category_string

# also exported by Base:
export normalize_string, graphemes, is_assigned_char, charwidth, isvalid,
export normalize_string, graphemes, is_assigned_char, textwidth, isvalid,
islower, isupper, isalpha, isdigit, isnumber, isalnum,
iscntrl, ispunct, isspace, isprint, isgraph

Expand Down Expand Up @@ -208,21 +208,35 @@ end

############################################################################

## character column width function ##
"""
charwidth(c)
textwidth(c)

Gives the number of columns needed to print a character.
Give the number of columns needed to print a character.

# Examples
```jldoctest
julia> charwidth('α')
julia> textwidth('α')
1

julia> charwidth('❤')
julia> textwidth('❤')
2
```
"""
charwidth(c::Char) = Int(ccall(:utf8proc_charwidth, Cint, (UInt32,), c))
textwidth(c::Char) = Int(ccall(:utf8proc_charwidth, Cint, (UInt32,), c))

"""
textwidth(s::AbstractString)

Give the number of columns needed to print a string.

# Examples
```jldoctest
julia> textwidth("March")
5
```
"""
textwidth(s::AbstractString) = mapreduce(textwidth, +, 0, s)

lowercase(c::Char) = isascii(c) ? ('A' <= c <= 'Z' ? c + 0x20 : c) : Char(ccall(:utf8proc_tolower, UInt32, (UInt32,), c))
uppercase(c::Char) = isascii(c) ? ('a' <= c <= 'z' ? c - 0x20 : c) : Char(ccall(:utf8proc_toupper, UInt32, (UInt32,), c))
Expand Down
8 changes: 4 additions & 4 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars)
## string padding functions ##

function lpad(s::AbstractString, n::Integer, p::AbstractString=" ")
m = n - strwidth(s)
m = n - textwidth(s)
(m <= 0) && (return s)
l = strwidth(p)
l = textwidth(p)
if l==1
return string(p^m, s)
end
Expand All @@ -217,9 +217,9 @@ function lpad(s::AbstractString, n::Integer, p::AbstractString=" ")
end

function rpad(s::AbstractString, n::Integer, p::AbstractString=" ")
m = n - strwidth(s)
m = n - textwidth(s)
(m <= 0) && (return s)
l = strwidth(p)
l = textwidth(p)
if l==1
return string(s, p^m)
end
Expand Down
3 changes: 1 addition & 2 deletions doc/src/stdlib/strings.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ Base.chr2ind
Base.nextind
Base.prevind
Base.Random.randstring
Base.UTF8proc.charwidth
Base.strwidth
Base.UTF8proc.textwidth
Base.UTF8proc.isalnum
Base.UTF8proc.isalpha
Base.isascii
Expand Down
4 changes: 2 additions & 2 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ end
Base.show(io, MIME("text/plain"), d)
out = split(String(take!(s)),'\n')
for line in out[2:end]
@test strwidth(line) <= cols
@test textwidth(line) <= cols
end
@test length(out) <= rows

Expand All @@ -284,7 +284,7 @@ end
Base.show(io, MIME("text/plain"), f(d))
out = split(String(take!(s)),'\n')
for line in out[2:end]
@test strwidth(line) <= cols
@test textwidth(line) <= cols
end
@test length(out) <= rows
end
Expand Down
13 changes: 7 additions & 6 deletions test/unicode/utf8proc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,16 +266,17 @@ end
end

@testset "#3721, #6939 up-to-date character widths" begin
@test charwidth('\U1f355') == 2
@test strwidth("\U1f355") == 2
@test strwidth(GenericString("\U1f355")) == 2
@test strwidth("\U1f355\u0302") == 2
@test strwidth(GenericString("\U1f355\u0302")) == 2
@test textwidth("") == 0
@test textwidth('\U1f355') == 2
@test textwidth("\U1f355") == 2
@test textwidth(GenericString("\U1f355")) == 2
@test textwidth("\U1f355\u0302") == 2
@test textwidth(GenericString("\U1f355\u0302")) == 2
end

@testset "#10958 handling of embedded NUL chars" begin
@test length("\0w") == length("\0α") == 2
@test strwidth("\0w") == strwidth("\0α") == 1
@test textwidth("\0w") == textwidth("\0α") == 1
@test normalize_string("\0W", casefold=true) == "\0w"
end

Expand Down