Skip to content

Commit

Permalink
deprecate bin, oct, dec, hex, and base in favor of string and keywo…
Browse files Browse the repository at this point in the history
…rd args (JuliaLang#25804)

* deprecate bin, oct, dec, hex, and base in favor of `string` and keyword args

* Work around inference problem in `div(::UInt128, ::UInt128)`
  • Loading branch information
JeffBezanson committed Mar 2, 2018
1 parent ea62c00 commit d480d1b
Show file tree
Hide file tree
Showing 38 changed files with 303 additions and 361 deletions.
2 changes: 1 addition & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ function show(io::IO, ::MIME"text/plain", c::Char)
else
u = UInt32(c)
end
h = hex(u, u 0xffff ? 4 : 6)
h = string(u, base = 16, pad = u 0xffff ? 4 : 6)
print(io, (isascii(c) ? "ASCII/" : ""), "Unicode U+", h)
else
print(io, ": Malformed UTF-8")
Expand Down
23 changes: 21 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ function hex2num(s::AbstractString)
end
export hex2num

@deprecate num2hex(x::Union{Float16,Float32,Float64}) hex(reinterpret(Unsigned, x), sizeof(x)*2)
@deprecate num2hex(n::Integer) hex(n, sizeof(n)*2)
@deprecate num2hex(x::Union{Float16,Float32,Float64}) string(reinterpret(Unsigned, x), base = 16, pad = sizeof(x)*2)
@deprecate num2hex(n::Integer) string(n, base = 16, pad = sizeof(n)*2)

# PR #22742: change in isapprox semantics
@deprecate rtoldefault(x,y) rtoldefault(x,y,0) false
Expand Down Expand Up @@ -1394,6 +1394,25 @@ end

@deprecate print_with_color(color, args...; kwargs...) printstyled(args...; kwargs..., color=color)

@deprecate base(b, n) string(n, base = b)
@deprecate base(b, n, pad) string(n, base = b, pad = pad)
@deprecate bin(n) string(n, base = 2)
@deprecate bin(n, pad) string(n, base = 2, pad = pad)
@deprecate oct(n) string(n, base = 8)
@deprecate oct(n, pad) string(n, base = 8, pad = pad)
@deprecate dec(n) string(n)
@deprecate dec(n, pad) string(n, pad = pad)
@deprecate hex(n) string(n, base = 16)
@deprecate hex(n, pad) string(n, base = 16, pad = pad)
@deprecate bin(n::Char) string(UInt32(n), base = 2)
@deprecate bin(n::Char, pad) string(UInt32(n), base = 2, pad = pad)
@deprecate oct(n::Char) string(UInt32(n), base = 8)
@deprecate oct(n::Char, pad) string(UInt32(n), base = 8, pad = pad)
@deprecate dec(n::Char) string(UInt32(n))
@deprecate dec(n::Char, pad) string(UInt32(n), pad = pad)
@deprecate hex(n::Char) string(UInt32(n), base = 16)
@deprecate hex(n::Char, pad) string(UInt32(n), base = 16, pad = pad)

@deprecate which(s::Symbol) which(Main, s)

@deprecate IOBuffer(data::AbstractVector{UInt8}, read::Bool, write::Bool=false, maxsize::Integer=typemax(Int)) IOBuffer(data, read=read, write=write, maxsize=maxsize)
Expand Down
5 changes: 0 additions & 5 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,15 @@ export

# strings
ascii,
base,
bin,
bitstring,
bytes2hex,
chomp,
chop,
codeunit,
codeunits,
dec,
digits,
digits!,
escape_string,
hex,
hex2bytes,
hex2bytes!,
isalpha,
Expand All @@ -594,7 +590,6 @@ export
ncodeunits,
ndigits,
nextind,
oct,
prevind,
repeat,
replace,
Expand Down
23 changes: 6 additions & 17 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export BigInt
import .Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), xor,
binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod,
ndigits, promote_rule, rem, show, isqrt, string, powermod,
sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal,
sum, trailing_zeros, trailing_ones, count_ones, tryparse_internal,
bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0zpb,
widen, signed, unsafe_trunc, trunc, iszero, isone, big, flipsign, signbit,
hastypemax
Expand Down Expand Up @@ -593,27 +593,16 @@ flipsign( x::BigInt, y::Integer) = signbit(y) ? -x : x
flipsign( x::BigInt, y::BigInt) = signbit(y) ? -x : x
# above method to resolving ambiguities with flipsign(::T, ::T) where T<:Signed

string(x::BigInt) = dec(x)
show(io::IO, x::BigInt) = print(io, string(x))

bin(n::BigInt) = base( 2, n)
oct(n::BigInt) = base( 8, n)
dec(n::BigInt) = base(10, n)
hex(n::BigInt) = base(16, n)

bin(n::BigInt, pad::Int) = base( 2, n, pad)
oct(n::BigInt, pad::Int) = base( 8, n, pad)
dec(n::BigInt, pad::Int) = base(10, n, pad)
hex(n::BigInt, pad::Int) = base(16, n, pad)

function base(b::Integer, n::BigInt, pad::Integer=1)
b < 0 && return base(Int(b), n, pad, (b>0) & (n.size<0))
2 <= b <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $b"))
function string(n::BigInt; base::Integer = 10, pad::Integer = 1)
base < 0 && return Base._base(Int(base), n, pad, (base>0) & (n.size<0))
2 <= base <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $base"))
iszero(n) && pad < 1 && return ""
nd1 = ndigits(n, b)
nd1 = ndigits(n, base)
nd = max(nd1, pad)
sv = Base.StringVector(nd + isneg(n))
GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, b, n)
GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, base, n)
@inbounds for i = (1:nd-nd1) .+ isneg(n)
sv[i] = '0' % UInt8
end
Expand Down
4 changes: 2 additions & 2 deletions base/grisu/grisu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function _show(io::IO, x::AbstractFloat, mode, n::Int, typed, compact)
write(io, '0')
end
write(io, (typed && isa(x,Float32)) ? 'f' : 'e')
write(io, dec(pt-1))
write(io, string(pt - 1))
typed && isa(x,Float16) && write(io, ")")
return
elseif pt <= 0
Expand Down Expand Up @@ -160,7 +160,7 @@ function _print_shortest(io::IO, x::AbstractFloat, dot::Bool, mode, n::Int)
# => ########e###
unsafe_write(io, pdigits+0, len)
write(io, 'e')
write(io, dec(e))
write(io, string(e))
return
elseif pt <= 0
# => 0.000########
Expand Down
6 changes: 3 additions & 3 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ julia> a = bswap(4)
julia> bswap(a)
4
julia> bin(1)
julia> string(1, base = 2)
"1"
julia> bin(bswap(1))
julia> string(bswap(1), base = 2)
"100000000000000000000000000000000000000000000000000000000"
```
"""
Expand Down Expand Up @@ -717,7 +717,7 @@ if Core.sizeof(Int) == 4
return Int128(div(BigInt(x), BigInt(y)))
end
function div(x::UInt128, y::UInt128)
return UInt128(div(BigInt(x), BigInt(y)))
return UInt128(div(BigInt(x), BigInt(y)))::UInt128
end

function rem(x::Int128, y::Int128)
Expand Down
116 changes: 29 additions & 87 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,14 @@ julia> ndigits(12345)
julia> ndigits(1022, 16)
3
julia> base(16, 1022)
julia> string(1022, base = 16)
"3fe"
```
"""
ndigits(x::Integer, b::Integer, pad::Int=1) = max(pad, ndigits0z(x, b))

## integer to string functions ##

string(x::Union{Int8,Int16,Int32,Int64,Int128}) = dec(x)

function bin(x::Unsigned, pad::Int, neg::Bool)
i = neg + max(pad,sizeof(x)<<3-leading_zeros(x))
a = StringVector(i)
Expand Down Expand Up @@ -620,8 +618,7 @@ end
const base36digits = ['0':'9';'a':'z']
const base62digits = ['0':'9';'A':'Z';'a':'z']


function base(b::Int, x::Integer, pad::Int, neg::Bool)
function _base(b::Int, x::Integer, pad::Int, neg::Bool)
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative."))
2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b"))
digits = abs(b) <= 36 ? base36digits : base62digits
Expand All @@ -641,97 +638,42 @@ function base(b::Int, x::Integer, pad::Int, neg::Bool)
String(a)
end

split_sign(n::Integer) = unsigned(abs(n)), n < 0
split_sign(n::Unsigned) = n, false

"""
base(base::Integer, n::Integer, pad::Integer=1)
string(n::Integer; base::Integer = 10, pad::Integer = 1)
Convert an integer `n` to a string in the given `base`,
optionally specifying a number of digits to pad to.
```jldoctest
julia> base(13,5,4)
julia> string(5, base = 13, pad = 4)
"0005"
julia> base(5,13,4)
julia> string(13, base = 5, pad = 4)
"0023"
```
"""
base(b::Integer, n::Integer, pad::Integer=1) =
base(Int(b), b > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (b>0) & (n<0))

for sym in (:bin, :oct, :dec, :hex)
@eval begin
($sym)(x::Unsigned, p::Int) = ($sym)(x,p,false)
($sym)(x::Unsigned) = ($sym)(x,1,false)
($sym)(x::Char, p::Int) = ($sym)(UInt32(x),p,false)
($sym)(x::Char) = ($sym)(UInt32(x),1,false)
($sym)(x::Integer, p::Int) = ($sym)(unsigned(abs(x)),p,x<0)
($sym)(x::Integer) = ($sym)(unsigned(abs(x)),1,x<0)
function string(n::Integer; base::Integer = 10, pad::Integer = 1)
if base == 2
(n_positive, neg) = split_sign(n)
bin(n_positive, pad, neg)
elseif base == 8
(n_positive, neg) = split_sign(n)
oct(n_positive, pad, neg)
elseif base == 10
(n_positive, neg) = split_sign(n)
dec(n_positive, pad, neg)
elseif base == 16
(n_positive, neg) = split_sign(n)
hex(n_positive, pad, neg)
else
_base(Int(base), base > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (base>0) & (n<0))
end
end

"""
bin(n, pad::Int=1)
Convert an integer to a binary string, optionally specifying a number of digits to pad to.
```jldoctest
julia> bin(10,2)
"1010"
julia> bin(10,8)
"00001010"
```
"""
bin

"""
hex(n, pad::Int=1)
Convert an integer to a hexadecimal string, optionally specifying a number of
digits to pad to.
```jldoctest
julia> hex(20)
"14"
julia> hex(20, 3)
"014"
```
"""
hex

"""
oct(n, pad::Int=1)
Convert an integer to an octal string, optionally specifying a number of digits
to pad to.
```jldoctest
julia> oct(20)
"24"
julia> oct(20, 3)
"024"
```
"""
oct

"""
dec(n, pad::Int=1)
Convert an integer to a decimal string, optionally specifying a number of digits
to pad to.
# Examples
```jldoctest
julia> dec(20)
"20"
julia> dec(20, 3)
"020"
```
"""
dec
string(b::Bool) = b ? "true" : "false"

"""
bitstring(n)
Expand All @@ -749,11 +691,11 @@ julia> bitstring(2.2)
"""
function bitstring end

bitstring(x::Union{Bool,Int8,UInt8}) = bin(reinterpret(UInt8,x),8)
bitstring(x::Union{Int16,UInt16,Float16}) = bin(reinterpret(UInt16,x),16)
bitstring(x::Union{Char,Int32,UInt32,Float32}) = bin(reinterpret(UInt32,x),32)
bitstring(x::Union{Int64,UInt64,Float64}) = bin(reinterpret(UInt64,x),64)
bitstring(x::Union{Int128,UInt128}) = bin(reinterpret(UInt128,x),128)
bitstring(x::Union{Bool,Int8,UInt8}) = string(reinterpret(UInt8,x), pad = 8, base = 2)
bitstring(x::Union{Int16,UInt16,Float16}) = string(reinterpret(UInt16,x), pad = 16, base = 2)
bitstring(x::Union{Char,Int32,UInt32,Float32}) = string(reinterpret(UInt32,x), pad = 32, base = 2)
bitstring(x::Union{Int64,UInt64,Float64}) = string(reinterpret(UInt64,x), pad = 64, base = 2)
bitstring(x::Union{Int128,UInt128}) = string(reinterpret(UInt128,x), pad = 128, base = 2)

"""
digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1)
Expand Down
2 changes: 1 addition & 1 deletion base/logging.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ function log_record_id(_module, level, message_ex)
# as we increment h to resolve any collisions.
h = hash(string(modname, level, message_ex)) % (1<<31)
while true
id = Symbol(modname, '_', hex(h, 8))
id = Symbol(modname, '_', string(h, base = 16, pad = 8))
# _log_record_ids is a registry of log record ids for use during
# compilation, to ensure uniqueness of ids. Note that this state will
# only persist during module compilation so it will be empty when a
Expand Down
15 changes: 6 additions & 9 deletions base/operators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -782,14 +782,11 @@ entered in the Julia REPL (and most editors, appropriately configured) by typing
# Examples
```jldoctest
julia> map(uppercase∘hex, 250:255)
6-element Array{String,1}:
"FA"
"FB"
"FC"
"FD"
"FE"
"FF"
julia> map(uppercase∘first, ["apple", "banana", "carrot"])
3-element Array{Char,1}:
'A'
'B'
'C'
```
"""
(f, g) = (x...)->f(g(x...))
Expand Down Expand Up @@ -874,4 +871,4 @@ julia> map(splat(+), zip(1:3,4:6))
9
```
"""
splat(f) = args->f(args...)
splat(f) = args->f(args...)
2 changes: 1 addition & 1 deletion base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ function gen_p(flags::String, width::Int, precision::Int, c::Char)
end
push!(blk.args, :(write(out, '0')))
push!(blk.args, :(write(out, 'x')))
push!(blk.args, :(write(out, String(hex(unsigned($x), $ptrwidth)))))
push!(blk.args, :(write(out, String(string(unsigned($x), pad = $ptrwidth, base = 16)))))
if width > 0 && '-' in flags
push!(blk.args, pad(width, width, ' '))
end
Expand Down
10 changes: 5 additions & 5 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ function show_default(io::IO, @nospecialize(x))
GC.@preserve r begin
p = unsafe_convert(Ptr{Cvoid}, r)
for i in (nb - 1):-1:0
print(io, hex(unsafe_load(convert(Ptr{UInt8}, p + i)), 2))
print(io, string(unsafe_load(convert(Ptr{UInt8}, p + i)), base = 16, pad = 2))
end
end
end
Expand Down Expand Up @@ -552,11 +552,11 @@ end

show(io::IO, ::Nothing) = print(io, "nothing")
show(io::IO, b::Bool) = print(io, b ? "true" : "false")
show(io::IO, n::Signed) = (write(io, dec(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", hex(n,sizeof(n)<<1))
print(io::IO, n::Unsigned) = print(io, dec(n))
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
print(io::IO, n::Unsigned) = print(io, string(n))

show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(hex(UInt(p), Sys.WORD_SIZE>>2))")
show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(string(UInt(p), base = 16, pad = Sys.WORD_SIZE>>2))")

has_tight_type(p::Pair) =
typeof(p.first) == typeof(p).parameters[1] &&
Expand Down
2 changes: 1 addition & 1 deletion base/stat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ StatStruct(buf::Union{Vector{UInt8},Ptr{UInt8}}) = StatStruct(
ccall(:jl_stat_ctime, Float64, (Ptr{UInt8},), buf),
)

show(io::IO, st::StatStruct) = print(io, "StatStruct(mode=0o$(oct(filemode(st),6)), size=$(filesize(st)))")
show(io::IO, st::StatStruct) = print(io, "StatStruct(mode=0o$(string(filemode(st), base = 8, pad = 6)), size=$(filesize(st)))")

# stat & lstat functions

Expand Down
Loading

0 comments on commit d480d1b

Please sign in to comment.