Skip to content

Keywords for string, deprecate bin, oct, dec, hex, base #25717

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

Closed
wants to merge 11 commits into from
2 changes: 1 addition & 1 deletion base/char.jl
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,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
16 changes: 14 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,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 @@ -1665,6 +1665,18 @@ export readandwrite
@deprecate PipeBuffer(data, maxsize) PipeBuffer(data, maxsize = maxsize)
@deprecate unsafe_wrap(T, pointer, dims, own) unsafe_wrap(T, pointer, dims, own = own)

@deprecase base(b, n, pad = 1) string(n, base = b, pad = pad)
@deprecate bin(n, pad = 1) string(n, base = 2, pad = pad)
@deprecate oct(n, pad = 1) string(n, base = 8, pad = pad)
@deprecate dec(n, pad = 1) string(n, pad = pad)
@deprecate hex(n, pad = 1) string(n, base = 16, pad = pad)
@deprecate hex(id::LibGit2.GitHash) string(id)
@deprecate hex(id::LibGit2.GitShortHash) string(id)
@deprecate bin(n::Char, pad = 1) string(UInt32(n), base = 2, pad = pad)
@deprecate oct(n::Char, pad = 1) string(UInt32(n), base = 8, pad = pad)
@deprecate dec(n::Char, pad = 1) string(UInt32(n), pad = pad)
@deprecate hex(n::Char, pad = 1) string(UInt32(n), base = 16, pad = pad)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
5 changes: 0 additions & 5 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -573,20 +573,16 @@ export

# strings and text output
ascii,
base,
bin,
bitstring,
bytes2hex,
chomp,
chop,
codeunit,
codeunits,
dec,
digits,
digits!,
dump,
escape_string,
hex,
hex2bytes,
hex2bytes!,
info,
Expand All @@ -611,7 +607,6 @@ export
ncodeunits,
ndigits,
nextind,
oct,
prevind,
print,
print_shortest,
Expand Down
21 changes: 5 additions & 16 deletions base/gmp.jl
Original file line number Diff line number Diff line change
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(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
4 changes: 2 additions & 2 deletions base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,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
2 changes: 1 addition & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -708,7 +708,7 @@ function runtests(tests = ["all"], numcores = ceil(Int, Sys.CPU_CORES / 2);
tests = split(tests)
end
exit_on_error && push!(tests, "--exit-on-error")
seed != nothing && push!(tests, "--seed=0x$(hex(seed % UInt128))") # cast to UInt128 to avoid a minus sign
seed != nothing && push!(tests, "--seed=0x$(string(seed % UInt128, base = 16))") # cast to UInt128 to avoid a minus sign
ENV2 = copy(ENV)
ENV2["JULIA_CPU_CORES"] = "$numcores"
try
Expand Down
115 changes: 27 additions & 88 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -549,16 +549,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 @@ -611,7 +609,6 @@ 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)
(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"))
Expand All @@ -632,97 +629,39 @@ 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)
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

"""
bitstring(n)
Expand All @@ -740,11 +679,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/libgit2/blob.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Return the [`GitHash`](@ref) of the resulting blob.

# Examples
```julia
hash_str = hex(commit_oid)
hash_str = string(commit_oid)
blob_file = joinpath(repo_path, ".git", "objects", hash_str[1:2], hash_str[3:end])
id = LibGit2.addblob!(repo, blob_file)
```
Expand Down
10 changes: 6 additions & 4 deletions base/libgit2/oid.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,19 @@ function GitShortHash(obj::GitObject)
return sid
end

Base.hex(id::GitHash) = join([hex(i,2) for i in id.val])
Base.hex(id::GitShortHash) = hex(id.hash)[1:id.len]

"""
raw(id::GitHash) -> Vector{UInt8}

Obtain the raw bytes of the [`GitHash`](@ref) as a vector of length $OID_RAWSZ.
"""
raw(id::GitHash) = collect(id.val)

Base.string(id::AbstractGitHash) = hex(id)
function Base.print(io::IO, id::GitHash)
for i in id.val
print(io, string(i, base = 16, pad = 2))
end
end
Base.string(id::GitShortHash) = string(id.hash)[1:id.len]

Base.show(io::IO, id::GitHash) = print(io, "GitHash(\"$(string(id))\")")
Base.show(io::IO, id::GitShortHash) = print(io, "GitShortHash(\"$(string(id))\")")
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
2 changes: 1 addition & 1 deletion base/printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,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 @@ -553,11 +553,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
Loading