From 6246e1e417fe6dadd4056e69531052f4277fbedb Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Tue, 23 Jan 2018 17:39:35 -0500 Subject: [PATCH 01/11] pad keyword for base --- base/deprecated.jl | 2 ++ base/gmp.jl | 2 +- base/intfuncs.jl | 4 ++-- test/bigint.jl | 2 +- test/intfuncs.jl | 2 +- 5 files changed, 7 insertions(+), 5 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 388f8f6af40d2..eb359c7959221 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1665,6 +1665,8 @@ 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) base(b, n, pad = pad) + # END 0.7 deprecations # BEGIN 1.0 deprecations diff --git a/base/gmp.jl b/base/gmp.jl index aefe62110d65f..4635bc4c4b224 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -606,7 +606,7 @@ 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) +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")) iszero(n) && pad < 1 && return "" diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 906f9012cb07a..26931b3b89cbf 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -633,7 +633,7 @@ function base(b::Int, x::Integer, pad::Int, neg::Bool) end """ - base(base::Integer, n::Integer, pad::Integer=1) + base(base::Integer, n::Integer; pad::Integer=1) Convert an integer `n` to a string in the given `base`, optionally specifying a number of digits to pad to. @@ -646,7 +646,7 @@ julia> base(5,13,4) "0023" ``` """ -base(b::Integer, n::Integer, pad::Integer=1) = +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) diff --git a/test/bigint.jl b/test/bigint.jl index b4886df8220c2..40ea9e5b0cd7c 100644 --- a/test/bigint.jl +++ b/test/bigint.jl @@ -373,7 +373,7 @@ for f in (bin, oct, dec, hex) local f @test f(big(0), 0) == "" end -@test base(rand(2:62), big(0), 0) == "" +@test base(rand(2:62), big(0), pad = 0) == "" @test isqrt(big(4)) == 2 @test isqrt(big(5)) == 2 diff --git a/test/intfuncs.jl b/test/intfuncs.jl index f3db1ebc52437..54492d417fbdc 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -133,7 +133,7 @@ end @test hex(12) == "c" @test hex(-12, 3) == "-00c" - @test base(2, 5, 7) == "0000101" + @test base(2, 5, pad = 7) == "0000101" @test bitstring(Int16(3)) == "0000000000000011" @test bitstring('3') == "00110011000000000000000000000000" From eb54a4ec05c291a94ef26c18c8ff55f87d8830f7 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Tue, 23 Jan 2018 20:14:25 -0500 Subject: [PATCH 02/11] deprecate bin, oct, dec, hex --- HISTORY.md | 2 +- base/char.jl | 2 +- base/deprecated.jl | 8 +- base/gmp.jl | 12 +-- base/grisu/grisu.jl | 4 +- base/int.jl | 4 +- base/interactiveutil.jl | 2 +- base/intfuncs.jl | 108 +++++--------------- base/libgit2/blob.jl | 2 +- base/libgit2/oid.jl | 19 +++- base/logging.jl | 2 +- base/printf.jl | 2 +- base/show.jl | 10 +- base/socket.jl | 10 +- base/stat.jl | 2 +- base/strings/io.jl | 10 +- base/strings/util.jl | 4 +- base/task.jl | 2 +- doc/src/manual/unicode-input.md | 2 +- stdlib/Dates/src/io.jl | 10 +- stdlib/Distributed/src/cluster.jl | 2 +- stdlib/Profile/src/Profile.jl | 2 +- test/bigint.jl | 46 ++++----- test/intfuncs.jl | 24 ++--- test/libgit2.jl | 6 +- test/numbers.jl | 162 +++++++++++++++--------------- test/perf/micro/perf.jl | 2 +- test/perf/micro/perf.py | 4 +- test/runtests.jl | 2 +- test/strings/io.jl | 18 ++-- 30 files changed, 218 insertions(+), 267 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 9edaa38af09a2..c9bd1e96ae2b3 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -417,7 +417,7 @@ Library improvements respectively perform predicate function negation and function composition. For example, `map(!iszero, (0, 1))` is now equivalent to `map(x -> !iszero(x), (0, 1))` and `map(uppercase ∘ hex, 250:255)` is now equivalent to - `map(x -> uppercase(hex(x)), 250:255)` ([#17155]). + `map(x -> uppercase(base(16, x)), 250:255)` ([#17155]). * `enumerate` now supports the two-argument form `enumerate(::IndexStyle, iterable)`. This form allows specification of the returned indices' style. For example, diff --git a/base/char.jl b/base/char.jl index 6e7b051008258..7fa9e33583900 100644 --- a/base/char.jl +++ b/base/char.jl @@ -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 = base(16, u, pad = u ≤ 0xffff ? 4 : 6) print(io, (isascii(c) ? "ASCII/" : ""), "Unicode U+", h) else print(io, ": Malformed UTF-8") diff --git a/base/deprecated.jl b/base/deprecated.jl index eb359c7959221..f7c2404b2b98c 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -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}) base(16, reinterpret(Unsigned, x), pad = sizeof(x)*2) +@deprecate num2hex(n::Integer) base(16, n, pad = sizeof(n)*2) # PR #22742: change in isapprox semantics @deprecate rtoldefault(x,y) rtoldefault(x,y,0) false @@ -1666,6 +1666,10 @@ export readandwrite @deprecate unsafe_wrap(T, pointer, dims, own) unsafe_wrap(T, pointer, dims, own = own) @deprecase base(b, n, pad) base(b, n, pad = pad) +@deprecate bin(n, pad) base(2, n, pad = pad) +@deprecate oct(n, pad) base(8, n, pad = pad) +@deprecate dec(n, pad) base(10, n, pad = pad) +@deprecate hex(n, pad) base(16, n, pad = pad) # END 0.7 deprecations diff --git a/base/gmp.jl b/base/gmp.jl index 4635bc4c4b224..a5bae3fa14939 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -593,19 +593,9 @@ 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) +string(x::BigInt) = base(10, 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")) diff --git a/base/grisu/grisu.jl b/base/grisu/grisu.jl index 953b386514106..d0cbe1324558e 100644 --- a/base/grisu/grisu.jl +++ b/base/grisu/grisu.jl @@ -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, base(10, pt-1)) typed && isa(x,Float16) && write(io, ")") return elseif pt <= 0 @@ -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, base(10, e)) return elseif pt <= 0 # => 0.000######## diff --git a/base/int.jl b/base/int.jl index a046a2c344ef2..1a2de7fd3c3f9 100644 --- a/base/int.jl +++ b/base/int.jl @@ -316,10 +316,10 @@ julia> a = bswap(4) julia> bswap(a) 4 -julia> bin(1) +julia> base(2, 1) "1" -julia> bin(bswap(1)) +julia> base(2, bswap(1)) "100000000000000000000000000000000000000000000000000000000" ``` """ diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index fc25bc0c7b019..3f482199d4c6f 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -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$(base(16, seed % UInt128))") # cast to UInt128 to avoid a minus sign ENV2 = copy(ENV) ENV2["JULIA_CPU_CORES"] = "$numcores" try diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 26931b3b89cbf..17e1b59c51419 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -557,7 +557,7 @@ 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) +string(x::Union{Int8,Int16,Int32,Int64,Int128}) = base(10, x) function bin(x::Unsigned, pad::Int, neg::Bool) i = neg + max(pad,sizeof(x)<<3-leading_zeros(x)) @@ -632,6 +632,9 @@ 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) @@ -639,90 +642,31 @@ 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> base(13, 5, pad = 4) "0005" -julia> base(5,13,4) +julia> base(5, 13, 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) + if b == 2 + (n_positive, neg) = split_sign(n) + bin(n_positive, pad, neg) + elseif b == 8 + (n_positive, neg) = split_sign(n) + oct(n_positive, pad, neg) + elseif b == 10 + (n_positive, neg) = split_sign(n) + dec(n_positive, pad, neg) + elseif b == 16 + (n_positive, neg) = split_sign(n) + hex(n_positive, pad, neg) + else + base(Int(b), b > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (b>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 +base(b::Integer, n::Char; pad::Integer = 1) = base(b, UInt32(n); pad = pad) """ bitstring(n) @@ -740,11 +684,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}) = base(2, reinterpret(UInt8,x), pad = 8) +bitstring(x::Union{Int16,UInt16,Float16}) = base(2, reinterpret(UInt16,x), pad = 16) +bitstring(x::Union{Char,Int32,UInt32,Float32}) = base(2, reinterpret(UInt32,x), pad = 32) +bitstring(x::Union{Int64,UInt64,Float64}) = base(2, reinterpret(UInt64,x), pad = 64) +bitstring(x::Union{Int128,UInt128}) = base(2, reinterpret(UInt128,x), pad = 128) """ digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1) diff --git a/base/libgit2/blob.jl b/base/libgit2/blob.jl index d306ab96924bf..83c5d9e757efa 100644 --- a/base/libgit2/blob.jl +++ b/base/libgit2/blob.jl @@ -56,7 +56,7 @@ Return the [`GitHash`](@ref) of the resulting blob. # Examples ```julia -hash_str = hex(commit_oid) +hash_str = base(16, commit_oid) blob_file = joinpath(repo_path, ".git", "objects", hash_str[1:2], hash_str[3:end]) id = LibGit2.addblob!(repo, blob_file) ``` diff --git a/base/libgit2/oid.jl b/base/libgit2/oid.jl index daf1ad603011d..e2b9566a45cb2 100644 --- a/base/libgit2/oid.jl +++ b/base/libgit2/oid.jl @@ -155,8 +155,21 @@ 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] +assert_sixteen(x) = + if x == 16 + x + else + error("Must be base 16") + end + +function Base.base(b, id::GitHash) + assert_sixteen(b) + join([base(b,i,2) for i in id.val]) +end +function Base.base(b, id::GitShortHash) + assert_sixteen(b) + base(b, id.hash)[1:id.len] +end """ raw(id::GitHash) -> Vector{UInt8} @@ -165,7 +178,7 @@ 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) +Base.string(id::AbstractGitHash) = base(16, id) Base.show(io::IO, id::GitHash) = print(io, "GitHash(\"$(string(id))\")") Base.show(io::IO, id::GitShortHash) = print(io, "GitShortHash(\"$(string(id))\")") diff --git a/base/logging.jl b/base/logging.jl index cc4a21f34f3e3..5c24f5d431297 100644 --- a/base/logging.jl +++ b/base/logging.jl @@ -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, '_', base(16, h, 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 diff --git a/base/printf.jl b/base/printf.jl index d2f1f00ecc025..fe78a00fe8b26 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -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(base(16, unsigned($x), pad = $ptrwidth))))) if width > 0 && '-' in flags push!(blk.args, pad(width, width, ' ')) end diff --git a/base/show.jl b/base/show.jl index f4178f3242756..0ca640753eae6 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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, base(16, unsafe_load(convert(Ptr{UInt8}, p + i)), pad = 2)) end end end @@ -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, base(10, n)); nothing) +show(io::IO, n::Unsigned) = print(io, "0x", base(16, n, pad = sizeof(n)<<1)) +print(io::IO, n::Unsigned) = print(io, base(10, 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$(base(16, UInt(p), pad = Sys.WORD_SIZE>>2))") has_tight_type(p::Pair) = typeof(p.first) == typeof(p).parameters[1] && diff --git a/base/socket.jl b/base/socket.jl index 46e423470353c..e3e33f5c8153f 100644 --- a/base/socket.jl +++ b/base/socket.jl @@ -45,10 +45,10 @@ end IPv4(str::AbstractString) = parse(IPv4, str) show(io::IO,ip::IPv4) = print(io,"ip\"",ip,"\"") -print(io::IO,ip::IPv4) = print(io,dec((ip.host&(0xFF000000))>>24),".", - dec((ip.host&(0xFF0000))>>16),".", - dec((ip.host&(0xFF00))>>8),".", - dec(ip.host&0xFF)) +print(io::IO,ip::IPv4) = print(io,base(10, (ip.host&(0xFF000000))>>24),".", + base(10, (ip.host&(0xFF0000))>>16),".", + base(10, (ip.host&(0xFF00))>>8),".", + base(10, ip.host&0xFF)) struct IPv6 <: IPAddr host::UInt128 @@ -98,7 +98,7 @@ end IPv6(str::AbstractString) = parse(IPv6, str) # Suppress leading '0's and "0x" -print_ipv6_field(io,field::UInt16) = print(io,hex(field)) +print_ipv6_field(io,field::UInt16) = print(io,base(16,field)) print_ipv6_field(io,ip,i) = print_ipv6_field(io,ipv6_field(ip,i)) function ipv6_field(ip::IPv6,i) diff --git a/base/stat.jl b/base/stat.jl index 4e475d9d0f2c8..67e5b3802d671 100644 --- a/base/stat.jl +++ b/base/stat.jl @@ -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$(base(8, filemode(st),6)), size=$(filesize(st)))") # stat & lstat functions diff --git a/base/strings/io.jl b/base/strings/io.jl index 399eae5748795..c386d84d0530d 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -287,16 +287,16 @@ function escape_string(io, s::AbstractString, esc::AbstractString="") c in esc ? print(io, '\\', c) : '\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) : isprint(c) ? print(io, c) : - print(io, "\\x", hex(c, 2)) + print(io, "\\x", base(16, c, pad = 2)) elseif !isoverlong(c) && !ismalformed(c) isprint(c) ? print(io, c) : - c <= '\x7f' ? print(io, "\\x", hex(c, 2)) : - c <= '\uffff' ? print(io, "\\u", hex(c, need_full_hex(s, j) ? 4 : 2)) : - print(io, "\\U", hex(c, need_full_hex(s, j) ? 8 : 4)) + c <= '\x7f' ? print(io, "\\x", base(16, c, pad = 2)) : + c <= '\uffff' ? print(io, "\\u", base(16, c, pad = need_full_hex(s, j) ? 4 : 2)) : + print(io, "\\U", base(16, c, pad = need_full_hex(s, j) ? 8 : 4)) else # malformed or overlong u = bswap(reinterpret(UInt32, c)) while true - print(io, "\\x", hex(u % UInt8, 2)) + print(io, "\\x", base(16, u % UInt8, 2)) (u >>= 8) == 0 && break end end diff --git a/base/strings/util.jl b/base/strings/util.jl index 91afc96de9997..85ed4e16d6edc 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -471,7 +471,7 @@ See also [`hex2bytes!`](@ref) for an in-place version, and [`bytes2hex`](@ref) f # Examples ```jldoctest -julia> s = hex(12345) +julia> s = base(16, 12345) "3039" julia> hex2bytes(s) @@ -537,7 +537,7 @@ Convert an array of bytes to its hexadecimal representation. All characters are in lower-case. # Examples ```jldoctest -julia> a = hex(12345) +julia> a = base(16, 12345) "3039" julia> b = hex2bytes(a) diff --git a/base/task.jl b/base/task.jl index 41da005e17526..7719716eb79a0 100644 --- a/base/task.jl +++ b/base/task.jl @@ -51,7 +51,7 @@ function showerror(io::IO, ex::CompositeException) end function show(io::IO, t::Task) - print(io, "Task ($(t.state)) @0x$(hex(convert(UInt, pointer_from_objref(t)), Sys.WORD_SIZE>>2))") + print(io, "Task ($(t.state)) @0x$(base(16, convert(UInt, pointer_from_objref(t)), pad = Sys.WORD_SIZE>>2))") end """ diff --git a/doc/src/manual/unicode-input.md b/doc/src/manual/unicode-input.md index b95130160478c..11014fc05ae66 100644 --- a/doc/src/manual/unicode-input.md +++ b/doc/src/manual/unicode-input.md @@ -61,7 +61,7 @@ function table_entries(completions, unicode_dict) for (chars, inputs) in sort!(collect(completions), by = first) code_points, unicode_names, characters = String[], String[], String[] for char in chars - push!(code_points, "U+$(uppercase(hex(char, 5)))") + push!(code_points, "U+$(uppercase(base(16, char, pad = 5)))") push!(unicode_names, get(unicode_dict, UInt32(char), "(No Unicode name)")) push!(characters, isempty(characters) ? fix_combining_chars(char) : "$char") end diff --git a/stdlib/Dates/src/io.jl b/stdlib/Dates/src/io.jl index 6d77e845d36bc..4b1bede92a722 100644 --- a/stdlib/Dates/src/io.jl +++ b/stdlib/Dates/src/io.jl @@ -131,7 +131,7 @@ end for (c, fn) in zip("YmdHMS", [year, month, day, hour, minute, second]) @eval function format(io, d::DatePart{$c}, dt) - write(io, dec($fn(dt), d.width)) + write(io, base(10, $fn(dt), pad = d.width)) end end @@ -153,7 +153,7 @@ end # the last n digits of y # will be 0 padded if y has less than n digits - str = dec(y, n) + str = base(10, y, pad = n) l = endof(str) if l == n # fast path @@ -166,11 +166,11 @@ end function format(io, d::DatePart{'s'}, dt) ms = millisecond(dt) if ms % 100 == 0 - str = dec(div(ms, 100), 1) + str = base(10, div(ms, 100), 1) elseif ms % 10 == 0 - str = dec(div(ms, 10), 2) + str = base(10, div(ms, 10), 2) else - str = dec(ms, 3) + str = base(10, ms, 3) end write(io, rpad(str, d.width, '0')) diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index 9864a26dd5b4b..3754db8ace179 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -206,7 +206,7 @@ function start_worker(out::IO, cookie::AbstractString=readline(STDIN)) process_messages(client, client, true) end print(out, "julia_worker:") # print header - print(out, "$(dec(LPROC.bind_port))#") # print port + print(out, "$(base(10, LPROC.bind_port))#") # print port print(out, LPROC.bind_addr) print(out, '\n') flush(out) diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 8ac2660eca222..3df95d3842863 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -489,7 +489,7 @@ function tree_format(lilist::Vector{StackFrame}, counts::Vector{Int}, level::Int rpad(string(counts[i]), ndigcounts, " "), " ", "unknown function (pointer: 0x", - hex(li.pointer,2*sizeof(Ptr{Cvoid})), + base(16, li.pointer, pad = 2*sizeof(Ptr{Cvoid})), ")") else fname = string(li.func) diff --git a/test/bigint.jl b/test/bigint.jl index 40ea9e5b0cd7c..58996a7f18415 100644 --- a/test/bigint.jl +++ b/test/bigint.jl @@ -336,10 +336,10 @@ end @test_throws InexactError floor(BigInt,Inf) @test_throws InexactError ceil(BigInt,Inf) - @test bin(big(3)) == "11" - @test oct(big(9)) == "11" - @test oct(-big(9)) == "-11" - @test hex(big(12)) == "c" + @test base(2, big(3)) == "11" + @test base(8, big(9)) == "11" + @test base(8, -big(9)) == "-11" + @test base(16, big(12)) == "c" end @testset "Issue #18849" begin # bin, oct, dec, hex should not call sizeof on BigInts @@ -347,25 +347,25 @@ end padding = 4 low = big(4) high = big(2^20) - @test bin(low, padding) == "0100" - @test oct(low, padding) == "0004" - @test dec(low, padding) == "0004" - @test hex(low, padding) == "0004" - - @test bin(high, padding) == "100000000000000000000" - @test oct(high, padding) == "4000000" - @test dec(high, padding) == "1048576" - @test hex(high, padding) == "100000" - - @test bin(-low, padding) == "-0100" # handle negative numbers correctly - @test oct(-low, padding) == "-0004" - @test dec(-low, padding) == "-0004" - @test hex(-low, padding) == "-0004" - - @test bin(-high, padding) == "-100000000000000000000" - @test oct(-high, padding) == "-4000000" - @test dec(-high, padding) == "-1048576" - @test hex(-high, padding) == "-100000" + @test base(2, low, pad = padding) == "0100" + @test base(8, low, pad = padding) == "0004" + @test base(10, low, pad = padding) == "0004" + @test base(16, low, pad = padding) == "0004" + + @test base(2, high, pad = padding) == "100000000000000000000" + @test base(8, high, pad = padding) == "4000000" + @test base(10, high, pad = padding) == "1048576" + @test base(16, high, pad = padding) == "100000" + + @test base(2, -low, pad = padding) == "-0100" # handle negative numbers correctly + @test base(8, -low, pad = padding) == "-0004" + @test base(10, -low, pad = padding) == "-0004" + @test base(16, -low, pad = padding) == "-0004" + + @test base(2, -high, pad = padding) == "-100000000000000000000" + @test base(8, -high, pad = padding) == "-4000000" + @test base(10, -high, pad = padding) == "-1048576" + @test base(16, -high, pad = padding) == "-100000" end # respect 0-padding on big(0) diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 54492d417fbdc..5bdd22c1e063e 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -117,21 +117,21 @@ end @test all(n -> n == 1, ndigits(x, b) for b in [-20:-2;2:20] for x in [true, false]) end @testset "bin/oct/dec/hex/bits" begin - @test bin('3') == "110011" - @test bin('3',7) == "0110011" - @test bin(3) == "11" - @test bin(3, 2) == "11" - @test bin(3, 3) == "011" - @test bin(-3) == "-11" - @test bin(-3, 3) == "-011" + @test base(2, '3') == "110011" + @test base(2, '3', pad = 7) == "0110011" + @test base(2, 3) == "11" + @test base(2, 3, pad = 2) == "11" + @test base(2, 3, pad = 3) == "011" + @test base(2, -3) == "-11" + @test base(2, -3, pad = 3) == "-011" - @test oct(9) == "11" - @test oct(-9) == "-11" + @test base(8, 9) == "11" + @test base(8, -9) == "-11" - @test dec(121) == "121" + @test base(10, 121) == "121" - @test hex(12) == "c" - @test hex(-12, 3) == "-00c" + @test base(16, 12) == "c" + @test base(16, -12, pad = 3) == "-00c" @test base(2, 5, pad = 7) == "0000101" diff --git a/test/libgit2.jl b/test/libgit2.jl index ae805a2365111..cdb9abf3b0c40 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -680,7 +680,7 @@ mktempdir() do dir @test LibGit2.Consts.OBJECT(typeof(cmt)) == LibGit2.Consts.OBJ_COMMIT @test commit_oid1 == LibGit2.GitHash(cmt) short_oid1 = LibGit2.GitShortHash(string(commit_oid1)) - @test hex(commit_oid1) == hex(short_oid1) + @test base(16, commit_oid1) == base(16, short_oid1) @test cmp(commit_oid1, short_oid1) == 0 @test cmp(short_oid1, commit_oid1) == 0 @test !(short_oid1 < commit_oid1) @@ -689,7 +689,7 @@ mktempdir() do dir short_str = sprint(show, short_oid1) @test short_str == "GitShortHash(\"$(string(short_oid1))\")" short_oid2 = LibGit2.GitShortHash(cmt) - @test startswith(hex(commit_oid1), hex(short_oid2)) + @test startswith(base(16, commit_oid1), base(16, short_oid2)) LibGit2.with(LibGit2.GitCommit(repo, short_oid2)) do cmt2 @test commit_oid1 == LibGit2.GitHash(cmt2) @@ -887,7 +887,7 @@ mktempdir() do dir LibGit2.with(LibGit2.GitRepo(cache_repo)) do repo # this is slightly dubious, as it assumes the object has not been packed # could be replaced by another binary format - hash_string = hex(commit_oid1) + hash_string = base(16, commit_oid1) blob_file = joinpath(cache_repo,".git/objects", hash_string[1:2], hash_string[3:end]) id = LibGit2.addblob!(repo, blob_file) diff --git a/test/numbers.jl b/test/numbers.jl index 0d609e66f2c4b..49ae7de56e3a4 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -230,14 +230,14 @@ end @testset "bin/oct/dec/hex/base for extreme integers" begin # definition and printing of extreme integers - @test bin(typemin(UInt8)) == "0" - @test bin(typemax(UInt8)) == "1"^8 - @test oct(typemin(UInt8)) == "0" - @test oct(typemax(UInt8)) == "377" - @test dec(typemin(UInt8)) == "0" - @test dec(typemax(UInt8)) == "255" - @test hex(typemin(UInt8)) == "0" - @test hex(typemax(UInt8)) == "ff" + @test base(2, typemin(UInt8)) == "0" + @test base(2, typemax(UInt8)) == "1"^8 + @test base(8, typemin(UInt8)) == "0" + @test base(8, typemax(UInt8)) == "377" + @test base(10, typemin(UInt8)) == "0" + @test base(10, typemax(UInt8)) == "255" + @test base(16, typemin(UInt8)) == "0" + @test base(16, typemax(UInt8)) == "ff" @test repr(typemin(UInt8)) == "0x00" @test string(typemin(UInt8)) == "0" @test repr(typemax(UInt8)) == "0xff" @@ -247,14 +247,14 @@ end @test base(12,typemin(UInt8)) == "0" @test base(12,typemax(UInt8)) == "193" - @test bin(typemin(UInt16)) == "0" - @test bin(typemax(UInt16)) == "1"^16 - @test oct(typemin(UInt16)) == "0" - @test oct(typemax(UInt16)) == "177777" - @test dec(typemin(UInt16)) == "0" - @test dec(typemax(UInt16)) == "65535" - @test hex(typemin(UInt16)) == "0" - @test hex(typemax(UInt16)) == "ffff" + @test base(2, typemin(UInt16)) == "0" + @test base(2, typemax(UInt16)) == "1"^16 + @test base(8, typemin(UInt16)) == "0" + @test base(8, typemax(UInt16)) == "177777" + @test base(10, typemin(UInt16)) == "0" + @test base(10, typemax(UInt16)) == "65535" + @test base(16, typemin(UInt16)) == "0" + @test base(16, typemax(UInt16)) == "ffff" @test repr(typemin(UInt16)) == "0x0000" @test string(typemin(UInt16)) == "0" @test repr(typemax(UInt16)) == "0xffff" @@ -264,14 +264,14 @@ end @test base(12,typemin(UInt16)) == "0" @test base(12,typemax(UInt16)) == "31b13" - @test bin(typemin(UInt32)) == "0" - @test bin(typemax(UInt32)) == "1"^32 - @test oct(typemin(UInt32)) == "0" - @test oct(typemax(UInt32)) == "37777777777" - @test dec(typemin(UInt32)) == "0" - @test dec(typemax(UInt32)) == "4294967295" - @test hex(typemin(UInt32)) == "0" - @test hex(typemax(UInt32)) == "ffffffff" + @test base(2, typemin(UInt32)) == "0" + @test base(2, typemax(UInt32)) == "1"^32 + @test base(8, typemin(UInt32)) == "0" + @test base(8, typemax(UInt32)) == "37777777777" + @test base(10, typemin(UInt32)) == "0" + @test base(10, typemax(UInt32)) == "4294967295" + @test base(16, typemin(UInt32)) == "0" + @test base(16, typemax(UInt32)) == "ffffffff" @test repr(typemin(UInt32)) == "0x00000000" @test string(typemin(UInt32)) == "0" @test repr(typemax(UInt32)) == "0xffffffff" @@ -281,14 +281,14 @@ end @test base(12,typemin(UInt32)) == "0" @test base(12,typemax(UInt32)) == "9ba461593" - @test bin(typemin(UInt64)) == "0" - @test bin(typemax(UInt64)) == "1"^64 - @test oct(typemin(UInt64)) == "0" - @test oct(typemax(UInt64)) == "1777777777777777777777" - @test dec(typemin(UInt64)) == "0" - @test dec(typemax(UInt64)) == "18446744073709551615" - @test hex(typemin(UInt64)) == "0" - @test hex(typemax(UInt64)) == "ffffffffffffffff" + @test base(2, typemin(UInt64)) == "0" + @test base(2, typemax(UInt64)) == "1"^64 + @test base(8, typemin(UInt64)) == "0" + @test base(8, typemax(UInt64)) == "1777777777777777777777" + @test base(10, typemin(UInt64)) == "0" + @test base(10, typemax(UInt64)) == "18446744073709551615" + @test base(16, typemin(UInt64)) == "0" + @test base(16, typemax(UInt64)) == "ffffffffffffffff" @test repr(typemin(UInt64)) == "0x0000000000000000" @test string(typemin(UInt64)) == "0" @test repr(typemax(UInt64)) == "0xffffffffffffffff" @@ -298,33 +298,33 @@ end @test base(12,typemin(UInt64)) == "0" @test base(12,typemax(UInt64)) == "839365134a2a240713" - @test bin(typemin(UInt128)) == "0" - @test bin(typemax(UInt128)) == "1"^128 - @test oct(typemin(UInt128)) == "0" - @test oct(typemax(UInt128)) == "3777777777777777777777777777777777777777777" - @test hex(typemin(UInt128)) == "0" - @test hex(typemax(UInt128)) == "ffffffffffffffffffffffffffffffff" + @test base(2, typemin(UInt128)) == "0" + @test base(2, typemax(UInt128)) == "1"^128 + @test base(8, typemin(UInt128)) == "0" + @test base(8, typemax(UInt128)) == "3777777777777777777777777777777777777777777" + @test base(16, typemin(UInt128)) == "0" + @test base(16, typemax(UInt128)) == "ffffffffffffffffffffffffffffffff" @test repr(typemin(UInt128)) == "0x00000000000000000000000000000000" @test string(typemin(UInt128)) == "0" @test repr(typemax(UInt128)) == "0xffffffffffffffffffffffffffffffff" @test string(typemax(UInt128)) == "340282366920938463463374607431768211455" - @test dec(typemin(UInt128)) == "0" - @test dec(typemax(UInt128)) == "340282366920938463463374607431768211455" + @test base(10, typemin(UInt128)) == "0" + @test base(10, typemax(UInt128)) == "340282366920938463463374607431768211455" @test base(3,typemin(UInt128)) == "0" @test base(3,typemax(UInt128)) == "202201102121002021012000211012011021221022212021111001022110211020010021100121010" @test base(12,typemin(UInt128)) == "0" @test base(12,typemax(UInt128)) == "5916b64b41143526a777873841863a6a6993" - @test bin(typemin(Int8)) == "-1"*"0"^7 - @test bin(typemax(Int8)) == "1"^7 - @test oct(typemin(Int8)) == "-200" - @test oct(typemax(Int8)) == "177" - @test dec(typemin(Int8)) == "-128" - @test dec(typemax(Int8)) == "127" - @test hex(typemin(Int8)) == "-80" - @test hex(typemax(Int8)) == "7f" + @test base(2, typemin(Int8)) == "-1"*"0"^7 + @test base(2, typemax(Int8)) == "1"^7 + @test base(8, typemin(Int8)) == "-200" + @test base(8, typemax(Int8)) == "177" + @test base(10, typemin(Int8)) == "-128" + @test base(10, typemax(Int8)) == "127" + @test base(16, typemin(Int8)) == "-80" + @test base(16, typemax(Int8)) == "7f" @test string(typemin(Int8)) == "-128" @test string(typemax(Int8)) == "127" @test base(3,typemin(Int8)) == "-11202" @@ -332,14 +332,14 @@ end @test base(12,typemin(Int8)) == "-a8" @test base(12,typemax(Int8)) == "a7" - @test bin(typemin(Int16)) == "-1"*"0"^15 - @test bin(typemax(Int16)) == "1"^15 - @test oct(typemin(Int16)) == "-100000" - @test oct(typemax(Int16)) == "77777" - @test dec(typemin(Int16)) == "-32768" - @test dec(typemax(Int16)) == "32767" - @test hex(typemin(Int16)) == "-8000" - @test hex(typemax(Int16)) == "7fff" + @test base(2, typemin(Int16)) == "-1"*"0"^15 + @test base(2, typemax(Int16)) == "1"^15 + @test base(8, typemin(Int16)) == "-100000" + @test base(8, typemax(Int16)) == "77777" + @test base(10, typemin(Int16)) == "-32768" + @test base(10, typemax(Int16)) == "32767" + @test base(16, typemin(Int16)) == "-8000" + @test base(16, typemax(Int16)) == "7fff" @test string(typemin(Int16)) == "-32768" @test string(typemax(Int16)) == "32767" @test base(3,typemin(Int16)) == "-1122221122" @@ -347,14 +347,14 @@ end @test base(12,typemin(Int16)) == "-16b68" @test base(12,typemax(Int16)) == "16b67" - @test bin(typemin(Int32)) == "-1"*"0"^31 - @test bin(typemax(Int32)) == "1"^31 - @test oct(typemin(Int32)) == "-20000000000" - @test oct(typemax(Int32)) == "17777777777" - @test dec(typemin(Int32)) == "-2147483648" - @test dec(typemax(Int32)) == "2147483647" - @test hex(typemin(Int32)) == "-80000000" - @test hex(typemax(Int32)) == "7fffffff" + @test base(2, typemin(Int32)) == "-1"*"0"^31 + @test base(2, typemax(Int32)) == "1"^31 + @test base(8, typemin(Int32)) == "-20000000000" + @test base(8, typemax(Int32)) == "17777777777" + @test base(10, typemin(Int32)) == "-2147483648" + @test base(10, typemax(Int32)) == "2147483647" + @test base(16, typemin(Int32)) == "-80000000" + @test base(16, typemax(Int32)) == "7fffffff" @test string(typemin(Int32)) == "-2147483648" @test string(typemax(Int32)) == "2147483647" @test base(3,typemin(Int32)) == "-12112122212110202102" @@ -362,14 +362,14 @@ end @test base(12,typemin(Int32)) == "-4bb2308a8" @test base(12,typemax(Int32)) == "4bb2308a7" - @test bin(typemin(Int64)) == "-1"*"0"^63 - @test bin(typemax(Int64)) == "1"^63 - @test oct(typemin(Int64)) == "-1000000000000000000000" - @test oct(typemax(Int64)) == "777777777777777777777" - @test dec(typemin(Int64)) == "-9223372036854775808" - @test dec(typemax(Int64)) == "9223372036854775807" - @test hex(typemin(Int64)) == "-8000000000000000" - @test hex(typemax(Int64)) == "7fffffffffffffff" + @test base(2, typemin(Int64)) == "-1"*"0"^63 + @test base(2, typemax(Int64)) == "1"^63 + @test base(8, typemin(Int64)) == "-1000000000000000000000" + @test base(8, typemax(Int64)) == "777777777777777777777" + @test base(10, typemin(Int64)) == "-9223372036854775808" + @test base(10, typemax(Int64)) == "9223372036854775807" + @test base(16, typemin(Int64)) == "-8000000000000000" + @test base(16, typemax(Int64)) == "7fffffffffffffff" @test string(typemin(Int64)) == "-9223372036854775808" @test string(typemax(Int64)) == "9223372036854775807" @test base(3,typemin(Int64)) == "-2021110011022210012102010021220101220222" @@ -377,15 +377,15 @@ end @test base(12,typemin(Int64)) == "-41a792678515120368" @test base(12,typemax(Int64)) == "41a792678515120367" - @test bin(typemin(Int128)) == "-1"*"0"^127 - @test bin(typemax(Int128)) == "1"^127 - @test oct(typemin(Int128)) == "-2000000000000000000000000000000000000000000" - @test oct(typemax(Int128)) == "1777777777777777777777777777777777777777777" - @test hex(typemin(Int128)) == "-80000000000000000000000000000000" - @test hex(typemax(Int128)) == "7fffffffffffffffffffffffffffffff" + @test base(2, typemin(Int128)) == "-1"*"0"^127 + @test base(2, typemax(Int128)) == "1"^127 + @test base(8, typemin(Int128)) == "-2000000000000000000000000000000000000000000" + @test base(8, typemax(Int128)) == "1777777777777777777777777777777777777777777" + @test base(16, typemin(Int128)) == "-80000000000000000000000000000000" + @test base(16, typemax(Int128)) == "7fffffffffffffffffffffffffffffff" - @test dec(typemin(Int128)) == "-170141183460469231731687303715884105728" - @test dec(typemax(Int128)) == "170141183460469231731687303715884105727" + @test base(10, typemin(Int128)) == "-170141183460469231731687303715884105728" + @test base(10, typemax(Int128)) == "170141183460469231731687303715884105727" @test string(typemin(Int128)) == "-170141183460469231731687303715884105728" @test string(typemax(Int128)) == "170141183460469231731687303715884105727" @test base(3,typemin(Int128)) == @@ -2084,7 +2084,7 @@ end @test signif(Float16(1.1), 70) === Float16(1.1) end @testset "issue #1308" begin - @test hex(~UInt128(0)) == "f"^32 + @test base(16, ~UInt128(0)) == "f"^32 @test (~0)%UInt128 == ~UInt128(0) @test Int128(~0) == ~Int128(0) end diff --git a/test/perf/micro/perf.jl b/test/perf/micro/perf.jl index 93ef57e124a75..2aa7491883c82 100644 --- a/test/perf/micro/perf.jl +++ b/test/perf/micro/perf.jl @@ -20,7 +20,7 @@ function parseintperf(t) local n, m for i=1:t n = rand(UInt32) - s = hex(n) + s = base(16, n) m = UInt32(parse(Int64,s, base = 16)) end @test m == n diff --git a/test/perf/micro/perf.py b/test/perf/micro/perf.py index 1081cc1960dfa..00a29e4920a84 100644 --- a/test/perf/micro/perf.py +++ b/test/perf/micro/perf.py @@ -95,7 +95,7 @@ def pisum(): def parse_int(t): for i in range(1,t): n = random.randint(0,2**32-1) - s = hex(n) + s = base(16, n) if s[-1]=='L': s = s[0:-1] m = int(s,16) @@ -107,7 +107,7 @@ def printfd(t): for i in range(1,t): f.write("{:d} {:d}\n".format(i, i+1)) f.close() - + def print_perf(name, time): print("python," + name + "," + str(time*1000)) diff --git a/test/runtests.jl b/test/runtests.jl index e7b249dff2f7d..0b63389c91f4c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -277,7 +277,7 @@ cd(dirname(@__FILE__)) do println(" \033[31;1mFAILURE\033[0m\n") skipped > 0 && println("$skipped test", skipped > 1 ? "s were" : " was", " skipped due to failure.") - println("The global RNG seed was 0x$(hex(seed)).\n") + println("The global RNG seed was 0x$(base(16, seed)).\n") Test.print_test_errors(o_ts) throw(Test.FallbackTestSetException("Test run finished with errors")) end diff --git a/test/strings/io.jl b/test/strings/io.jl index 28f7d9bdf5e4b..7d580f76b4708 100644 --- a/test/strings/io.jl +++ b/test/strings/io.jl @@ -80,15 +80,15 @@ "\uFFFF","\U10000","\U10FFF","\U10FFFF"] c = Char(i) cp = string(c,p) - op = string(Char(div(i,8)), oct(i%8), p) - hp = string(Char(div(i,16)), hex(i%16), p) - @test string(unescape_string(string("\\",oct(i,1),p))) == cp - @test string(unescape_string(string("\\",oct(i,2),p))) == cp - @test string(unescape_string(string("\\",oct(i,3),p))) == cp - @test string(unescape_string(string("\\",oct(i,4),p))) == op - @test string(unescape_string(string("\\x",hex(i,1),p))) == cp - @test string(unescape_string(string("\\x",hex(i,2),p))) == cp - @test string(unescape_string(string("\\x",hex(i,3),p))) == hp + op = string(Char(div(i,8)), base(8, i%8), p) + hp = string(Char(div(i,16)), base(16, i%16), p) + @test string(unescape_string(string("\\",base(8,i,pad=1),p))) == cp + @test string(unescape_string(string("\\",base(8,i,pad=2),p))) == cp + @test string(unescape_string(string("\\",base(8,i,pad=3),p))) == cp + @test string(unescape_string(string("\\",base(8,i,pad=4),p))) == op + @test string(unescape_string(string("\\x",base(16,i,pad=1),p))) == cp + @test string(unescape_string(string("\\x",base(16,i,pad=2),p))) == cp + @test string(unescape_string(string("\\x",base(16,i,pad=3),p))) == hp end @testset "unescape_string" begin From a58e640e36e7e31f39ab94f508e7275bdc2cbf01 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Wed, 24 Jan 2018 12:03:38 -0500 Subject: [PATCH 03/11] restore history (phew) --- HISTORY.md | 2 +- base/libgit2/oid.jl | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index c9bd1e96ae2b3..9edaa38af09a2 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -417,7 +417,7 @@ Library improvements respectively perform predicate function negation and function composition. For example, `map(!iszero, (0, 1))` is now equivalent to `map(x -> !iszero(x), (0, 1))` and `map(uppercase ∘ hex, 250:255)` is now equivalent to - `map(x -> uppercase(base(16, x)), 250:255)` ([#17155]). + `map(x -> uppercase(hex(x)), 250:255)` ([#17155]). * `enumerate` now supports the two-argument form `enumerate(::IndexStyle, iterable)`. This form allows specification of the returned indices' style. For example, diff --git a/base/libgit2/oid.jl b/base/libgit2/oid.jl index e2b9566a45cb2..c1163bb49e2e2 100644 --- a/base/libgit2/oid.jl +++ b/base/libgit2/oid.jl @@ -164,7 +164,7 @@ assert_sixteen(x) = function Base.base(b, id::GitHash) assert_sixteen(b) - join([base(b,i,2) for i in id.val]) + join([base(b, i, pad=2) for i in id.val]) end function Base.base(b, id::GitShortHash) assert_sixteen(b) From 9b192702e42019f9190960d7a217470fffa43072 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Wed, 24 Jan 2018 12:08:17 -0500 Subject: [PATCH 04/11] deprecate hex methods for githash --- base/deprecated.jl | 2 ++ base/libgit2/blob.jl | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index f7c2404b2b98c..fb1bcfb28378c 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1670,6 +1670,8 @@ export readandwrite @deprecate oct(n, pad) base(8, n, pad = pad) @deprecate dec(n, pad) base(10, n, pad = pad) @deprecate hex(n, pad) base(16, n, pad = pad) +@deprecate hex(id::LibGit2.GitHash) string(id) +@deprecate hex(id::LibGit2.GitShortHash) string(id) # END 0.7 deprecations diff --git a/base/libgit2/blob.jl b/base/libgit2/blob.jl index 83c5d9e757efa..bbc69ef390bc9 100644 --- a/base/libgit2/blob.jl +++ b/base/libgit2/blob.jl @@ -56,7 +56,7 @@ Return the [`GitHash`](@ref) of the resulting blob. # Examples ```julia -hash_str = base(16, 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) ``` From 6da9e9dbe2f4fa8d23faa6b1ab5d1b89a98b20ba Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Wed, 24 Jan 2018 12:10:48 -0500 Subject: [PATCH 05/11] deprecate char method for base --- base/deprecated.jl | 1 + base/intfuncs.jl | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index fb1bcfb28378c..0b490849f86c2 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1672,6 +1672,7 @@ export readandwrite @deprecate hex(n, pad) base(16, n, pad = pad) @deprecate hex(id::LibGit2.GitHash) string(id) @deprecate hex(id::LibGit2.GitShortHash) string(id) +@deprecate base(b::Integer, n::Char; pad::Integer = pad) base(b, UInt32(n); pad = pad) # END 0.7 deprecations diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 17e1b59c51419..884b59f482876 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -666,8 +666,6 @@ 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)) end -base(b::Integer, n::Char; pad::Integer = 1) = base(b, UInt32(n); pad = pad) - """ bitstring(n) From 6721a4f4437afd415a18858873f5f1c375c5fd18 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Wed, 24 Jan 2018 12:12:52 -0500 Subject: [PATCH 06/11] removed bin, oct, dec, hex from exports --- base/exports.jl | 4 ---- 1 file changed, 4 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index d1aeb9be89694..2de098ca7cf92 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -574,19 +574,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, @@ -611,7 +608,6 @@ export ncodeunits, ndigits, nextind, - oct, prevind, print, print_shortest, From 5e260295ac5956ab6fed1ab4a363aebbb98063f8 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Wed, 24 Jan 2018 12:15:21 -0500 Subject: [PATCH 07/11] finish deprecating methods for git hash --- base/libgit2/oid.jl | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/base/libgit2/oid.jl b/base/libgit2/oid.jl index c1163bb49e2e2..2affa948aabbb 100644 --- a/base/libgit2/oid.jl +++ b/base/libgit2/oid.jl @@ -155,22 +155,6 @@ function GitShortHash(obj::GitObject) return sid end -assert_sixteen(x) = - if x == 16 - x - else - error("Must be base 16") - end - -function Base.base(b, id::GitHash) - assert_sixteen(b) - join([base(b, i, pad=2) for i in id.val]) -end -function Base.base(b, id::GitShortHash) - assert_sixteen(b) - base(b, id.hash)[1:id.len] -end - """ raw(id::GitHash) -> Vector{UInt8} From 76bb3e713f16518b5031bdad7fa8431f0426d9bf Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Wed, 24 Jan 2018 20:04:28 -0500 Subject: [PATCH 08/11] new methods for git string --- base/libgit2/oid.jl | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/base/libgit2/oid.jl b/base/libgit2/oid.jl index 2affa948aabbb..d4866ef7365fa 100644 --- a/base/libgit2/oid.jl +++ b/base/libgit2/oid.jl @@ -162,7 +162,8 @@ 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) = base(16, id) +Base.string(id::GitHash) = join([base(i,2,pad=2) for i in id.val]) +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))\")") From dd77ce2a04376ca0f47a37605f75d54531c4871b Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Fri, 26 Jan 2018 00:47:10 -0500 Subject: [PATCH 09/11] sooooo many tests --- base/char.jl | 2 +- base/deprecated.jl | 23 +- base/gmp.jl | 3 +- base/grisu/grisu.jl | 4 +- base/int.jl | 4 +- base/interactiveutil.jl | 2 +- base/intfuncs.jl | 23 +- base/libgit2/oid.jl | 2 +- base/logging.jl | 2 +- base/printf.jl | 2 +- base/show.jl | 10 +- base/socket.jl | 10 +- base/stat.jl | 2 +- base/strings/io.jl | 10 +- base/strings/util.jl | 4 +- base/task.jl | 2 +- doc/src/manual/unicode-input.md | 2 +- stdlib/Dates/src/io.jl | 10 +- stdlib/Distributed/src/cluster.jl | 2 +- stdlib/Profile/src/Profile.jl | 2 +- stdlib/Random/src/DSFMT.jl | 2 +- stdlib/Serialization/src/Serialization.jl | 2 +- test/bigint.jl | 48 ++-- test/intfuncs.jl | 28 +-- test/libgit2.jl | 6 +- test/numbers.jl | 262 +++++++++++----------- test/perf/micro/perf.jl | 2 +- test/perf/micro/perf.py | 2 +- test/runtests.jl | 2 +- test/strings/basic.jl | 2 +- test/strings/io.jl | 18 +- 31 files changed, 249 insertions(+), 246 deletions(-) diff --git a/base/char.jl b/base/char.jl index 7fa9e33583900..7ffeda282e2ba 100644 --- a/base/char.jl +++ b/base/char.jl @@ -164,7 +164,7 @@ function show(io::IO, ::MIME"text/plain", c::Char) else u = UInt32(c) end - h = base(16, u, pad = 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") diff --git a/base/deprecated.jl b/base/deprecated.jl index 0b490849f86c2..70ee61d36fbec 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -506,8 +506,8 @@ function hex2num(s::AbstractString) end export hex2num -@deprecate num2hex(x::Union{Float16,Float32,Float64}) base(16, reinterpret(Unsigned, x), pad = sizeof(x)*2) -@deprecate num2hex(n::Integer) base(16, n, pad = 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 @@ -1665,14 +1665,21 @@ 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) base(b, n, pad = pad) -@deprecate bin(n, pad) base(2, n, pad = pad) -@deprecate oct(n, pad) base(8, n, pad = pad) -@deprecate dec(n, pad) base(10, n, pad = pad) -@deprecate hex(n, pad) base(16, n, pad = pad) +@deprecase base(b, n, pad) string(n, base = b, pad = pad) +@deprecate bin(n, pad) string(n, base = 2, pad = pad) +@deprecate oct(n, pad) string(n, base = 8, pad = pad) +@deprecate dec(n, pad) string(n, pad = pad) +@deprecate hex(n, pad) string(n, base = 16, pad = pad) @deprecate hex(id::LibGit2.GitHash) string(id) @deprecate hex(id::LibGit2.GitShortHash) string(id) -@deprecate base(b::Integer, n::Char; pad::Integer = pad) base(b, UInt32(n); pad = pad) +@deprecate bin(n::Char, pad) string(UInt32(n), base = 2, pad = pad) +@deprecate oct(n::Char, pad) string(UInt32(n), base = 8, pad = pad) +@deprecate dec(n::Char, pad) string(UInt32(n), pad = pad) +@deprecate hex(n::Char, pad) string(UInt32(n), base = 16, pad = pad) +@deprecate bin(n::Char) string(UInt32(n), base = 2) +@deprecate oct(n::Char) string(UInt32(n), base = 8) +@deprecate dec(n::Char) string(UInt32(n)) +@deprecate hex(n::Char) string(UInt32(n), base = 16) # END 0.7 deprecations diff --git a/base/gmp.jl b/base/gmp.jl index a5bae3fa14939..c086f657aa41a 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -593,10 +593,9 @@ 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) = base(10, x) show(io::IO, x::BigInt) = print(io, string(x)) -function base(b::Integer, n::BigInt; pad::Integer=1) +function base(n::BigInt; b::Integer = 10, 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")) iszero(n) && pad < 1 && return "" diff --git a/base/grisu/grisu.jl b/base/grisu/grisu.jl index d0cbe1324558e..15c6a934cdc8e 100644 --- a/base/grisu/grisu.jl +++ b/base/grisu/grisu.jl @@ -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, base(10, pt-1)) + write(io, string(pt - 1, base = 10)) typed && isa(x,Float16) && write(io, ")") return elseif pt <= 0 @@ -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, base(10, e)) + write(io, string(e)) return elseif pt <= 0 # => 0.000######## diff --git a/base/int.jl b/base/int.jl index 1a2de7fd3c3f9..aea3ead74d18d 100644 --- a/base/int.jl +++ b/base/int.jl @@ -316,10 +316,10 @@ julia> a = bswap(4) julia> bswap(a) 4 -julia> base(2, 1) +julia> string(1, base = 2) "1" -julia> base(2, bswap(1)) +julia> string(bswap(1), base = 2) "100000000000000000000000000000000000000000000000000000000" ``` """ diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index 3f482199d4c6f..97b27d3a23e6a 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -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$(base(16, 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 diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 884b59f482876..cf2df8baca7ca 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -549,7 +549,7 @@ julia> ndigits(12345) julia> ndigits(1022, 16) 3 -julia> base(16, 1022) +julia> string(1022, bass = 16) "3fe" ``` """ @@ -557,8 +557,6 @@ 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}) = base(10, x) - function bin(x::Unsigned, pad::Int, neg::Bool) i = neg + max(pad,sizeof(x)<<3-leading_zeros(x)) a = StringVector(i) @@ -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")) @@ -636,20 +633,20 @@ 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, pad = 4) +julia> string(5, base = 13, pad = 4) "0005" -julia> base(5, 13, pad = 4) +julia> string(13, base = 5, pad = 4) "0023" ``` """ -base(b::Integer, n::Integer; pad::Integer=1) = +string(n::Integer; base::Integer = 10, pad::Integer = 1) = if b == 2 (n_positive, neg) = split_sign(n) bin(n_positive, pad, neg) @@ -682,11 +679,11 @@ julia> bitstring(2.2) """ function bitstring end -bitstring(x::Union{Bool,Int8,UInt8}) = base(2, reinterpret(UInt8,x), pad = 8) -bitstring(x::Union{Int16,UInt16,Float16}) = base(2, reinterpret(UInt16,x), pad = 16) -bitstring(x::Union{Char,Int32,UInt32,Float32}) = base(2, reinterpret(UInt32,x), pad = 32) -bitstring(x::Union{Int64,UInt64,Float64}) = base(2, reinterpret(UInt64,x), pad = 64) -bitstring(x::Union{Int128,UInt128}) = base(2, reinterpret(UInt128,x), pad = 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) diff --git a/base/libgit2/oid.jl b/base/libgit2/oid.jl index d4866ef7365fa..db5baa31085d7 100644 --- a/base/libgit2/oid.jl +++ b/base/libgit2/oid.jl @@ -162,7 +162,7 @@ Obtain the raw bytes of the [`GitHash`](@ref) as a vector of length $OID_RAWSZ. """ raw(id::GitHash) = collect(id.val) -Base.string(id::GitHash) = join([base(i,2,pad=2) for i in id.val]) +Base.string(id::GitHash) = join([string(i, base = 16, pad = 2) for i in id.val]) Base.string(id::GitShortHash) = string(id.hash)[1:id.len] Base.show(io::IO, id::GitHash) = print(io, "GitHash(\"$(string(id))\")") diff --git a/base/logging.jl b/base/logging.jl index 5c24f5d431297..b6e7b3478d270 100644 --- a/base/logging.jl +++ b/base/logging.jl @@ -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, '_', base(16, h, pad = 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 diff --git a/base/printf.jl b/base/printf.jl index fe78a00fe8b26..adc6c5313d264 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -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(base(16, unsigned($x), pad = $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 diff --git a/base/show.jl b/base/show.jl index 0ca640753eae6..57265a4abd67a 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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, base(16, unsafe_load(convert(Ptr{UInt8}, p + i)), pad = 2)) + print(io, string(unsafe_load(convert(Ptr{UInt8}, p + i)), base = 16, pad = 2)) end end end @@ -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, base(10, n)); nothing) -show(io::IO, n::Unsigned) = print(io, "0x", base(16, n, pad = sizeof(n)<<1)) -print(io::IO, n::Unsigned) = print(io, base(10, n)) +show(io::IO, n::Signed) = (write(io, string(n, base = 100)); 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$(base(16, UInt(p), pad = 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] && diff --git a/base/socket.jl b/base/socket.jl index e3e33f5c8153f..0fe0659c68779 100644 --- a/base/socket.jl +++ b/base/socket.jl @@ -45,10 +45,10 @@ end IPv4(str::AbstractString) = parse(IPv4, str) show(io::IO,ip::IPv4) = print(io,"ip\"",ip,"\"") -print(io::IO,ip::IPv4) = print(io,base(10, (ip.host&(0xFF000000))>>24),".", - base(10, (ip.host&(0xFF0000))>>16),".", - base(10, (ip.host&(0xFF00))>>8),".", - base(10, ip.host&0xFF)) +print(io::IO,ip::IPv4) = print(io,string((ip.host&(0xFF000000))>>24),".", + string((ip.host&(0xFF0000))>>16),".", + string((ip.host&(0xFF00))>>8),".", + string(ip.host&0xFF)) struct IPv6 <: IPAddr host::UInt128 @@ -98,7 +98,7 @@ end IPv6(str::AbstractString) = parse(IPv6, str) # Suppress leading '0's and "0x" -print_ipv6_field(io,field::UInt16) = print(io,base(16,field)) +print_ipv6_field(io,field::UInt16) = print(io, string(field, base = 16)) print_ipv6_field(io,ip,i) = print_ipv6_field(io,ipv6_field(ip,i)) function ipv6_field(ip::IPv6,i) diff --git a/base/stat.jl b/base/stat.jl index 67e5b3802d671..963df0827dd4a 100644 --- a/base/stat.jl +++ b/base/stat.jl @@ -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$(base(8, 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 diff --git a/base/strings/io.jl b/base/strings/io.jl index c386d84d0530d..72593d7bd7e24 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -287,16 +287,16 @@ function escape_string(io, s::AbstractString, esc::AbstractString="") c in esc ? print(io, '\\', c) : '\a' <= c <= '\r' ? print(io, '\\', "abtnvfr"[Int(c)-6]) : isprint(c) ? print(io, c) : - print(io, "\\x", base(16, c, pad = 2)) + print(io, "\\x", string(c, base = 16, pad = 2)) elseif !isoverlong(c) && !ismalformed(c) isprint(c) ? print(io, c) : - c <= '\x7f' ? print(io, "\\x", base(16, c, pad = 2)) : - c <= '\uffff' ? print(io, "\\u", base(16, c, pad = need_full_hex(s, j) ? 4 : 2)) : - print(io, "\\U", base(16, c, pad = need_full_hex(s, j) ? 8 : 4)) + c <= '\x7f' ? print(io, "\\x", string(c, base = 16, pad = 2)) : + c <= '\uffff' ? print(io, "\\u", string(c, base = 16, pad = need_full_hex(s, j) ? 4 : 2)) : + print(io, "\\U", string(c, base = 16, pad = need_full_hex(s, j) ? 8 : 4)) else # malformed or overlong u = bswap(reinterpret(UInt32, c)) while true - print(io, "\\x", base(16, u % UInt8, 2)) + print(io, "\\x", string(u % UInt8, base = 16, pad = 2)) (u >>= 8) == 0 && break end end diff --git a/base/strings/util.jl b/base/strings/util.jl index 85ed4e16d6edc..decac535f30c4 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -471,7 +471,7 @@ See also [`hex2bytes!`](@ref) for an in-place version, and [`bytes2hex`](@ref) f # Examples ```jldoctest -julia> s = base(16, 12345) +julia> s = string(12345, base = 16) "3039" julia> hex2bytes(s) @@ -537,7 +537,7 @@ Convert an array of bytes to its hexadecimal representation. All characters are in lower-case. # Examples ```jldoctest -julia> a = base(16, 12345) +julia> a = string(12345, base = 16) "3039" julia> b = hex2bytes(a) diff --git a/base/task.jl b/base/task.jl index 7719716eb79a0..07fbf36aaecdd 100644 --- a/base/task.jl +++ b/base/task.jl @@ -51,7 +51,7 @@ function showerror(io::IO, ex::CompositeException) end function show(io::IO, t::Task) - print(io, "Task ($(t.state)) @0x$(base(16, convert(UInt, pointer_from_objref(t)), pad = Sys.WORD_SIZE>>2))") + print(io, "Task ($(t.state)) @0x$(string(convert(UInt, pointer_from_objref(t)), base = 16, pad = Sys.WORD_SIZE>>2))") end """ diff --git a/doc/src/manual/unicode-input.md b/doc/src/manual/unicode-input.md index 11014fc05ae66..eeab4c2083464 100644 --- a/doc/src/manual/unicode-input.md +++ b/doc/src/manual/unicode-input.md @@ -61,7 +61,7 @@ function table_entries(completions, unicode_dict) for (chars, inputs) in sort!(collect(completions), by = first) code_points, unicode_names, characters = String[], String[], String[] for char in chars - push!(code_points, "U+$(uppercase(base(16, char, pad = 5)))") + push!(code_points, "U+$(uppercase(string(char, base = 16, pad = 5)))") push!(unicode_names, get(unicode_dict, UInt32(char), "(No Unicode name)")) push!(characters, isempty(characters) ? fix_combining_chars(char) : "$char") end diff --git a/stdlib/Dates/src/io.jl b/stdlib/Dates/src/io.jl index 4b1bede92a722..36b03cadabf4b 100644 --- a/stdlib/Dates/src/io.jl +++ b/stdlib/Dates/src/io.jl @@ -131,7 +131,7 @@ end for (c, fn) in zip("YmdHMS", [year, month, day, hour, minute, second]) @eval function format(io, d::DatePart{$c}, dt) - write(io, base(10, $fn(dt), pad = d.width)) + write(io, string($fn(dt), base = 10, pad = d.width)) end end @@ -153,7 +153,7 @@ end # the last n digits of y # will be 0 padded if y has less than n digits - str = base(10, y, pad = n) + str = string(y, base = 10, pad = n) l = endof(str) if l == n # fast path @@ -166,11 +166,11 @@ end function format(io, d::DatePart{'s'}, dt) ms = millisecond(dt) if ms % 100 == 0 - str = base(10, div(ms, 100), 1) + str = string(div(ms, 100)) elseif ms % 10 == 0 - str = base(10, div(ms, 10), 2) + str = string(div(ms, 10), pad = 2) else - str = base(10, ms, 3) + str = string(ms, pad = 3) end write(io, rpad(str, d.width, '0')) diff --git a/stdlib/Distributed/src/cluster.jl b/stdlib/Distributed/src/cluster.jl index 3754db8ace179..9c56f08c8cffb 100644 --- a/stdlib/Distributed/src/cluster.jl +++ b/stdlib/Distributed/src/cluster.jl @@ -206,7 +206,7 @@ function start_worker(out::IO, cookie::AbstractString=readline(STDIN)) process_messages(client, client, true) end print(out, "julia_worker:") # print header - print(out, "$(base(10, LPROC.bind_port))#") # print port + print(out, "$(string(LPROC.bind_port))#") # print port print(out, LPROC.bind_addr) print(out, '\n') flush(out) diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 3df95d3842863..08e8fea60ada1 100644 --- a/stdlib/Profile/src/Profile.jl +++ b/stdlib/Profile/src/Profile.jl @@ -489,7 +489,7 @@ function tree_format(lilist::Vector{StackFrame}, counts::Vector{Int}, level::Int rpad(string(counts[i]), ndigcounts, " "), " ", "unknown function (pointer: 0x", - base(16, li.pointer, pad = 2*sizeof(Ptr{Cvoid})), + string(li.pointer, base = 16, pad = 2*sizeof(Ptr{Cvoid})), ")") else fname = string(li.func) diff --git a/stdlib/Random/src/DSFMT.jl b/stdlib/Random/src/DSFMT.jl index 58e5ed23bb2ab..0d1c143ba0db2 100644 --- a/stdlib/Random/src/DSFMT.jl +++ b/stdlib/Random/src/DSFMT.jl @@ -106,7 +106,7 @@ struct GF2X end GF2X(s::AbstractString) = GF2X(parse(BigInt, reverse(s), base = 16)) -Base.string(f::GF2X) = reverse(base(16, f.z)) +Base.string(f::GF2X) = reverse(string(f.z, base = 16)) Base.:(==)(f::GF2X, g::GF2X) = f.z == g.z Base.copy(f::GF2X) = GF2X(MPZ.set(f.z)) diff --git a/stdlib/Serialization/src/Serialization.jl b/stdlib/Serialization/src/Serialization.jl index 181cf2e2baa28..c99c9d19ff797 100644 --- a/stdlib/Serialization/src/Serialization.jl +++ b/stdlib/Serialization/src/Serialization.jl @@ -321,7 +321,7 @@ end function serialize(s::AbstractSerializer, n::BigInt) serialize_type(s, BigInt) - serialize(s, base(62,n)) + serialize(s, string(n, base = 62)) end function serialize(s::AbstractSerializer, n::BigFloat) diff --git a/test/bigint.jl b/test/bigint.jl index 58996a7f18415..b95d865f9ce1b 100644 --- a/test/bigint.jl +++ b/test/bigint.jl @@ -336,10 +336,10 @@ end @test_throws InexactError floor(BigInt,Inf) @test_throws InexactError ceil(BigInt,Inf) - @test base(2, big(3)) == "11" - @test base(8, big(9)) == "11" - @test base(8, -big(9)) == "-11" - @test base(16, big(12)) == "c" + @test string(big(3), base = 2) == "11" + @test string(big(9), base = 8) == "11" + @test string(-big(9), base = 8) == "-11" + @test string(big(12), base = 16) == "c" end @testset "Issue #18849" begin # bin, oct, dec, hex should not call sizeof on BigInts @@ -347,25 +347,25 @@ end padding = 4 low = big(4) high = big(2^20) - @test base(2, low, pad = padding) == "0100" - @test base(8, low, pad = padding) == "0004" - @test base(10, low, pad = padding) == "0004" - @test base(16, low, pad = padding) == "0004" - - @test base(2, high, pad = padding) == "100000000000000000000" - @test base(8, high, pad = padding) == "4000000" - @test base(10, high, pad = padding) == "1048576" - @test base(16, high, pad = padding) == "100000" - - @test base(2, -low, pad = padding) == "-0100" # handle negative numbers correctly - @test base(8, -low, pad = padding) == "-0004" - @test base(10, -low, pad = padding) == "-0004" - @test base(16, -low, pad = padding) == "-0004" - - @test base(2, -high, pad = padding) == "-100000000000000000000" - @test base(8, -high, pad = padding) == "-4000000" - @test base(10, -high, pad = padding) == "-1048576" - @test base(16, -high, pad = padding) == "-100000" + @test string(low, pad = padding, base = 2) == "0100" + @test string(low, pad = padding, base = 8) == "0004" + @test string(low, pad = padding, base = 10) == "0004" + @test string(low, pad = padding, base = 16) == "0004" + + @test string(high, pad = padding, base = 2) == "100000000000000000000" + @test string(high, pad = padding, base = 8) == "4000000" + @test string(high, pad = padding, base = 10) == "1048576" + @test string(high, pad = padding, base = 16) == "100000" + + @test string(-low, pad = padding, base = 2) == "-0100" # handle negative numbers correctly + @test string(-low, pad = padding, base = 8) == "-0004" + @test string(-low, pad = padding, base = 10) == "-0004" + @test string(-low, pad = padding, base = 16) == "-0004" + + @test string(-high, pad = padding, base = 2) == "-100000000000000000000" + @test string(-high, pad = padding, base = 8) == "-4000000" + @test string(-high, pad = padding, base = 10) == "-1048576" + @test string(-high, pad = padding, base = 16) == "-100000" end # respect 0-padding on big(0) @@ -373,7 +373,7 @@ for f in (bin, oct, dec, hex) local f @test f(big(0), 0) == "" end -@test base(rand(2:62), big(0), pad = 0) == "" +@test string(big(0), base = rand(2:62), pad = 0) == "" @test isqrt(big(4)) == 2 @test isqrt(big(5)) == 2 diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 5bdd22c1e063e..ecbe7022ecea7 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -117,23 +117,23 @@ end @test all(n -> n == 1, ndigits(x, b) for b in [-20:-2;2:20] for x in [true, false]) end @testset "bin/oct/dec/hex/bits" begin - @test base(2, '3') == "110011" - @test base(2, '3', pad = 7) == "0110011" - @test base(2, 3) == "11" - @test base(2, 3, pad = 2) == "11" - @test base(2, 3, pad = 3) == "011" - @test base(2, -3) == "-11" - @test base(2, -3, pad = 3) == "-011" + @test string('3', base = 2) == "110011" + @test string('3', pad = 7, base = 2) == "0110011" + @test string(3, base = 2) == "11" + @test string(3, pad = 2, base = 2) == "11" + @test string(3, pad = 3, base = 2) == "011" + @test string(-3, base = 2) == "-11" + @test string(-3, pad = 3, base = 2) == "-011" - @test base(8, 9) == "11" - @test base(8, -9) == "-11" + @test string(9, base = 8) == "11" + @test string(-9, base = 8) == "-11" - @test base(10, 121) == "121" + @test string(121, base = 10) == "121" - @test base(16, 12) == "c" - @test base(16, -12, pad = 3) == "-00c" + @test string(12, base = 16) == "c" + @test string(-12, pad = 3, base = 16) == "-00c" - @test base(2, 5, pad = 7) == "0000101" + @test string(5, pad = 7, base = 2) == "0000101" @test bitstring(Int16(3)) == "0000000000000011" @test bitstring('3') == "00110011000000000000000000000000" @@ -152,7 +152,7 @@ end @test digits(T(-8163), base = -10) == [7, 7, 9, 9] end end - @test [base(b, n) + @test [string(n, base = b) for n = [-10^9, -10^5, -2^20, -2^10, -100, -83, -50, -34, -27, -16, -7, -3, -2, -1, 0, 1, 2, 3, 4, 7, 16, 27, 34, 50, 83, 100, 2^10, 2^20, 10^5, 10^9] for b = [-2, -3, -7, -10, -60]] == diff --git a/test/libgit2.jl b/test/libgit2.jl index cdb9abf3b0c40..2cd1d542707cc 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -680,7 +680,7 @@ mktempdir() do dir @test LibGit2.Consts.OBJECT(typeof(cmt)) == LibGit2.Consts.OBJ_COMMIT @test commit_oid1 == LibGit2.GitHash(cmt) short_oid1 = LibGit2.GitShortHash(string(commit_oid1)) - @test base(16, commit_oid1) == base(16, short_oid1) + @test string(commit_oid1) == string(short_oid1) @test cmp(commit_oid1, short_oid1) == 0 @test cmp(short_oid1, commit_oid1) == 0 @test !(short_oid1 < commit_oid1) @@ -689,7 +689,7 @@ mktempdir() do dir short_str = sprint(show, short_oid1) @test short_str == "GitShortHash(\"$(string(short_oid1))\")" short_oid2 = LibGit2.GitShortHash(cmt) - @test startswith(base(16, commit_oid1), base(16, short_oid2)) + @test startswith(string(commit_oid1), string(short_oid2)) LibGit2.with(LibGit2.GitCommit(repo, short_oid2)) do cmt2 @test commit_oid1 == LibGit2.GitHash(cmt2) @@ -887,7 +887,7 @@ mktempdir() do dir LibGit2.with(LibGit2.GitRepo(cache_repo)) do repo # this is slightly dubious, as it assumes the object has not been packed # could be replaced by another binary format - hash_string = base(16, commit_oid1) + hash_string = string(commit_oid1, base = 16) blob_file = joinpath(cache_repo,".git/objects", hash_string[1:2], hash_string[3:end]) id = LibGit2.addblob!(repo, blob_file) diff --git a/test/numbers.jl b/test/numbers.jl index 49ae7de56e3a4..782c9392136a7 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -230,170 +230,170 @@ end @testset "bin/oct/dec/hex/base for extreme integers" begin # definition and printing of extreme integers - @test base(2, typemin(UInt8)) == "0" - @test base(2, typemax(UInt8)) == "1"^8 - @test base(8, typemin(UInt8)) == "0" - @test base(8, typemax(UInt8)) == "377" - @test base(10, typemin(UInt8)) == "0" - @test base(10, typemax(UInt8)) == "255" - @test base(16, typemin(UInt8)) == "0" - @test base(16, typemax(UInt8)) == "ff" + @test string(typemin(UInt8), base = 2) == "0" + @test string(typemax(UInt8), base = 2) == "1"^8 + @test string(typemin(UInt8), base = 8) == "0" + @test string(typemax(UInt8), base = 8) == "377" + @test string(typemin(UInt8), base = 10) == "0" + @test string(typemax(UInt8), base = 10) == "255" + @test string(typemin(UInt8), base = 16) == "0" + @test string(typemax(UInt8), base = 16) == "ff" @test repr(typemin(UInt8)) == "0x00" @test string(typemin(UInt8)) == "0" @test repr(typemax(UInt8)) == "0xff" @test string(typemax(UInt8)) == "255" - @test base(3,typemin(UInt8)) == "0" - @test base(3,typemax(UInt8)) == "100110" - @test base(12,typemin(UInt8)) == "0" - @test base(12,typemax(UInt8)) == "193" - - @test base(2, typemin(UInt16)) == "0" - @test base(2, typemax(UInt16)) == "1"^16 - @test base(8, typemin(UInt16)) == "0" - @test base(8, typemax(UInt16)) == "177777" - @test base(10, typemin(UInt16)) == "0" - @test base(10, typemax(UInt16)) == "65535" - @test base(16, typemin(UInt16)) == "0" - @test base(16, typemax(UInt16)) == "ffff" + @test string(typemin(UInt8), base = 3) == "0" + @test string(typemax(UInt8), base = 3) == "100110" + @test string(typemin(UInt8), base = 12) == "0" + @test string(typemax(UInt8), base = 12) == "193" + + @test string(typemin(UInt16), base = 2) == "0" + @test string(typemax(UInt16), base = 2) == "1"^16 + @test string(typemin(UInt16), base = 8) == "0" + @test string(typemax(UInt16), base = 8) == "177777" + @test string(typemin(UInt16), base = 10) == "0" + @test string(typemax(UInt16), base = 10) == "65535" + @test string(typemin(UInt16), base = 16) == "0" + @test string(typemax(UInt16), base = 16) == "ffff" @test repr(typemin(UInt16)) == "0x0000" @test string(typemin(UInt16)) == "0" @test repr(typemax(UInt16)) == "0xffff" @test string(typemax(UInt16)) == "65535" - @test base(3,typemin(UInt16)) == "0" - @test base(3,typemax(UInt16)) == "10022220020" - @test base(12,typemin(UInt16)) == "0" - @test base(12,typemax(UInt16)) == "31b13" - - @test base(2, typemin(UInt32)) == "0" - @test base(2, typemax(UInt32)) == "1"^32 - @test base(8, typemin(UInt32)) == "0" - @test base(8, typemax(UInt32)) == "37777777777" - @test base(10, typemin(UInt32)) == "0" - @test base(10, typemax(UInt32)) == "4294967295" - @test base(16, typemin(UInt32)) == "0" - @test base(16, typemax(UInt32)) == "ffffffff" + @test string(typemin(UInt16), base = 3) == "0" + @test string(typemax(UInt16), base = 3) == "10022220020" + @test string(typemin(UInt16), base = 12) == "0" + @test string(typemax(UInt16), base = 12) == "31b13" + + @test string(typemin(UInt32), base = 2) == "0" + @test string(typemax(UInt32), base = 2) == "1"^32 + @test string(typemin(UInt32), base = 8) == "0" + @test string(typemax(UInt32), base = 8) == "37777777777" + @test string(typemin(UInt32), base = 10) == "0" + @test string(typemax(UInt32), base = 10) == "4294967295" + @test string(typemin(UInt32), base = 16) == "0" + @test string(typemax(UInt32), base = 16) == "ffffffff" @test repr(typemin(UInt32)) == "0x00000000" @test string(typemin(UInt32)) == "0" @test repr(typemax(UInt32)) == "0xffffffff" @test string(typemax(UInt32)) == "4294967295" - @test base(3,typemin(UInt32)) == "0" - @test base(3,typemax(UInt32)) == "102002022201221111210" - @test base(12,typemin(UInt32)) == "0" - @test base(12,typemax(UInt32)) == "9ba461593" - - @test base(2, typemin(UInt64)) == "0" - @test base(2, typemax(UInt64)) == "1"^64 - @test base(8, typemin(UInt64)) == "0" - @test base(8, typemax(UInt64)) == "1777777777777777777777" - @test base(10, typemin(UInt64)) == "0" - @test base(10, typemax(UInt64)) == "18446744073709551615" - @test base(16, typemin(UInt64)) == "0" - @test base(16, typemax(UInt64)) == "ffffffffffffffff" + @test string(typemin(UInt32), base = 3) == "0" + @test string(typemax(UInt32), base = 3) == "102002022201221111210" + @test string(typemin(UInt32), base = 12) == "0" + @test string(typemax(UInt32), base = 12) == "9ba461593" + + @test string(typemin(UInt64), base = 2) == "0" + @test string(typemax(UInt64), base = 2) == "1"^64 + @test string(typemin(UInt64), base = 8) == "0" + @test string(typemax(UInt64), base = 8) == "1777777777777777777777" + @test string(typemin(UInt64), base = 10) == "0" + @test string(typemax(UInt64), base = 10) == "18446744073709551615" + @test string(typemin(UInt64), base = 16) == "0" + @test string(typemax(UInt64), base = 16) == "ffffffffffffffff" @test repr(typemin(UInt64)) == "0x0000000000000000" @test string(typemin(UInt64)) == "0" @test repr(typemax(UInt64)) == "0xffffffffffffffff" @test string(typemax(UInt64)) == "18446744073709551615" - @test base(3,typemin(UInt64)) == "0" - @test base(3,typemax(UInt64)) == "11112220022122120101211020120210210211220" - @test base(12,typemin(UInt64)) == "0" - @test base(12,typemax(UInt64)) == "839365134a2a240713" - - @test base(2, typemin(UInt128)) == "0" - @test base(2, typemax(UInt128)) == "1"^128 - @test base(8, typemin(UInt128)) == "0" - @test base(8, typemax(UInt128)) == "3777777777777777777777777777777777777777777" - @test base(16, typemin(UInt128)) == "0" - @test base(16, typemax(UInt128)) == "ffffffffffffffffffffffffffffffff" + @test string(typemin(UInt64), base = 3) == "0" + @test string(typemax(UInt64), base = 3) == "11112220022122120101211020120210210211220" + @test string(typemin(UInt64), base = 12) == "0" + @test string(typemax(UInt64), base = 12) == "839365134a2a240713" + + @test string(typemin(UInt128), base = 2) == "0" + @test string(typemax(UInt128), base = 2) == "1"^128 + @test string(typemin(UInt128), base = 8) == "0" + @test string(typemax(UInt128), base = 8) == "3777777777777777777777777777777777777777777" + @test string(typemin(UInt128), base = 16) == "0" + @test string(typemax(UInt128), base = 16) == "ffffffffffffffffffffffffffffffff" @test repr(typemin(UInt128)) == "0x00000000000000000000000000000000" @test string(typemin(UInt128)) == "0" @test repr(typemax(UInt128)) == "0xffffffffffffffffffffffffffffffff" @test string(typemax(UInt128)) == "340282366920938463463374607431768211455" - @test base(10, typemin(UInt128)) == "0" - @test base(10, typemax(UInt128)) == "340282366920938463463374607431768211455" - @test base(3,typemin(UInt128)) == "0" - @test base(3,typemax(UInt128)) == + @test string(typemin(UInt128), base = 10) == "0" + @test string(typemax(UInt128), base = 10) == "340282366920938463463374607431768211455" + @test string(typemin(UInt128), base = 3) == "0" + @test string(typemax(UInt128), base = 3) == "202201102121002021012000211012011021221022212021111001022110211020010021100121010" - @test base(12,typemin(UInt128)) == "0" - @test base(12,typemax(UInt128)) == "5916b64b41143526a777873841863a6a6993" - - @test base(2, typemin(Int8)) == "-1"*"0"^7 - @test base(2, typemax(Int8)) == "1"^7 - @test base(8, typemin(Int8)) == "-200" - @test base(8, typemax(Int8)) == "177" - @test base(10, typemin(Int8)) == "-128" - @test base(10, typemax(Int8)) == "127" - @test base(16, typemin(Int8)) == "-80" - @test base(16, typemax(Int8)) == "7f" + @test string(typemin(UInt128), base = 12) == "0" + @test string(typemax(UInt128), base = 12) == "5916b64b41143526a777873841863a6a6993" + + @test string(typemin(Int8), base = 2) == "-1"*"0"^7 + @test string(typemax(Int8), base = 2) == "1"^7 + @test string(typemin(Int8), base = 8) == "-200" + @test string(typemax(Int8), base = 8) == "177" + @test string(typemin(Int8), base = 10) == "-128" + @test string(typemax(Int8), base = 10) == "127" + @test string(typemin(Int8), base = 16) == "-80" + @test string(typemax(Int8), base = 16) == "7f" @test string(typemin(Int8)) == "-128" @test string(typemax(Int8)) == "127" - @test base(3,typemin(Int8)) == "-11202" - @test base(3,typemax(Int8)) == "11201" - @test base(12,typemin(Int8)) == "-a8" - @test base(12,typemax(Int8)) == "a7" - - @test base(2, typemin(Int16)) == "-1"*"0"^15 - @test base(2, typemax(Int16)) == "1"^15 - @test base(8, typemin(Int16)) == "-100000" - @test base(8, typemax(Int16)) == "77777" - @test base(10, typemin(Int16)) == "-32768" - @test base(10, typemax(Int16)) == "32767" - @test base(16, typemin(Int16)) == "-8000" - @test base(16, typemax(Int16)) == "7fff" + @test string(typemin(Int8), base = 3) == "-11202" + @test string(typemax(Int8), base = 3) == "11201" + @test string(typemin(Int8), base = 12) == "-a8" + @test string(typemax(Int8), base = 12) == "a7" + + @test string(typemin(Int16), base = 2) == "-1"*"0"^15 + @test string(typemax(Int16), base = 2) == "1"^15 + @test string(typemin(Int16), base = 8) == "-100000" + @test string(typemax(Int16), base = 8) == "77777" + @test string(typemin(Int16), base = 10) == "-32768" + @test string(typemax(Int16), base = 10) == "32767" + @test string(typemin(Int16), base = 16) == "-8000" + @test string(typemax(Int16), base = 16) == "7fff" @test string(typemin(Int16)) == "-32768" @test string(typemax(Int16)) == "32767" - @test base(3,typemin(Int16)) == "-1122221122" - @test base(3,typemax(Int16)) == "1122221121" - @test base(12,typemin(Int16)) == "-16b68" - @test base(12,typemax(Int16)) == "16b67" - - @test base(2, typemin(Int32)) == "-1"*"0"^31 - @test base(2, typemax(Int32)) == "1"^31 - @test base(8, typemin(Int32)) == "-20000000000" - @test base(8, typemax(Int32)) == "17777777777" - @test base(10, typemin(Int32)) == "-2147483648" - @test base(10, typemax(Int32)) == "2147483647" - @test base(16, typemin(Int32)) == "-80000000" - @test base(16, typemax(Int32)) == "7fffffff" + @test string(typemin(Int16), base = 3) == "-1122221122" + @test string(typemax(Int16), base = 3) == "1122221121" + @test string(typemin(Int16), base = 12) == "-16b68" + @test string(typemax(Int16), base = 12) == "16b67" + + @test string(typemin(Int32), base = 2) == "-1"*"0"^31 + @test string(typemax(Int32), base = 2) == "1"^31 + @test string(typemin(Int32), base = 8) == "-20000000000" + @test string(typemax(Int32), base = 8) == "17777777777" + @test string(typemin(Int32), base = 10) == "-2147483648" + @test string(typemax(Int32), base = 10) == "2147483647" + @test string(typemin(Int32), base = 16) == "-80000000" + @test string(typemax(Int32), base = 16) == "7fffffff" @test string(typemin(Int32)) == "-2147483648" @test string(typemax(Int32)) == "2147483647" - @test base(3,typemin(Int32)) == "-12112122212110202102" - @test base(3,typemax(Int32)) == "12112122212110202101" - @test base(12,typemin(Int32)) == "-4bb2308a8" - @test base(12,typemax(Int32)) == "4bb2308a7" - - @test base(2, typemin(Int64)) == "-1"*"0"^63 - @test base(2, typemax(Int64)) == "1"^63 - @test base(8, typemin(Int64)) == "-1000000000000000000000" - @test base(8, typemax(Int64)) == "777777777777777777777" - @test base(10, typemin(Int64)) == "-9223372036854775808" - @test base(10, typemax(Int64)) == "9223372036854775807" - @test base(16, typemin(Int64)) == "-8000000000000000" - @test base(16, typemax(Int64)) == "7fffffffffffffff" + @test string(typemin(Int32), base = 3) == "-12112122212110202102" + @test string(typemax(Int32), base = 3) == "12112122212110202101" + @test string(typemin(Int32), base = 12) == "-4bb2308a8" + @test string(typemax(Int32), base = 12) == "4bb2308a7" + + @test string(typemin(Int64), base = 2) == "-1"*"0"^63 + @test string(typemax(Int64), base = 2) == "1"^63 + @test string(typemin(Int64), base = 8) == "-1000000000000000000000" + @test string(typemax(Int64), base = 8) == "777777777777777777777" + @test string(typemin(Int64), base = 10) == "-9223372036854775808" + @test string(typemax(Int64), base = 16) == "9223372036854775807" + @test string(typemin(Int64), base = 16) == "-8000000000000000" + @test string(typemax(Int64)) == "7fffffffffffffff" @test string(typemin(Int64)) == "-9223372036854775808" @test string(typemax(Int64)) == "9223372036854775807" - @test base(3,typemin(Int64)) == "-2021110011022210012102010021220101220222" - @test base(3,typemax(Int64)) == "2021110011022210012102010021220101220221" - @test base(12,typemin(Int64)) == "-41a792678515120368" - @test base(12,typemax(Int64)) == "41a792678515120367" - - @test base(2, typemin(Int128)) == "-1"*"0"^127 - @test base(2, typemax(Int128)) == "1"^127 - @test base(8, typemin(Int128)) == "-2000000000000000000000000000000000000000000" - @test base(8, typemax(Int128)) == "1777777777777777777777777777777777777777777" - @test base(16, typemin(Int128)) == "-80000000000000000000000000000000" - @test base(16, typemax(Int128)) == "7fffffffffffffffffffffffffffffff" - - @test base(10, typemin(Int128)) == "-170141183460469231731687303715884105728" - @test base(10, typemax(Int128)) == "170141183460469231731687303715884105727" + @test string(typemin(Int64), base = 3) == "-2021110011022210012102010021220101220222" + @test string(typemax(Int64), base = 3) == "2021110011022210012102010021220101220221" + @test string(typemin(Int64), base = 12) == "-41a792678515120368" + @test string(typemax(Int64), base = 12) == "41a792678515120367" + + @test string(typemin(Int128), base = 2) == "-1"*"0"^127 + @test string(typemax(Int128), base = 2) == "1"^127 + @test string(typemin(Int128), base = 8) == "-2000000000000000000000000000000000000000000" + @test string(typemax(Int128), base = 8) == "1777777777777777777777777777777777777777777" + @test string(typemin(Int128), base = 16) == "-80000000000000000000000000000000" + @test string(typemax(Int128), base = 16) == "7fffffffffffffffffffffffffffffff" + + @test string(typemin(Int128), base = 10) == "-170141183460469231731687303715884105728" + @test string(typemax(Int128), base = 10) == "170141183460469231731687303715884105727" @test string(typemin(Int128)) == "-170141183460469231731687303715884105728" @test string(typemax(Int128)) == "170141183460469231731687303715884105727" - @test base(3,typemin(Int128)) == + @test string(typemin(Int128), base = 3) == "-101100201022001010121000102002120122110122221010202000122201220121120010200022002" - @test base(3,typemax(Int128)) == + @test string(typemax(Int128), base = 3) == "101100201022001010121000102002120122110122221010202000122201220121120010200022001" - @test base(12,typemin(Int128)) == "-2a695925806818735399a37a20a31b3534a8" - @test base(12,typemax(Int128)) == "2a695925806818735399a37a20a31b3534a7" + @test string(typemin(Int128), base = 12) == "-2a695925806818735399a37a20a31b3534a8" + @test string(typemax(Int128), base = 12) == "2a695925806818735399a37a20a31b3534a7" end @testset "floating-point printing" begin @test repr(1.0) == "1.0" @@ -2084,7 +2084,7 @@ end @test signif(Float16(1.1), 70) === Float16(1.1) end @testset "issue #1308" begin - @test base(16, ~UInt128(0)) == "f"^32 + @test string(~UInt128(0), base = 16) == "f"^32 @test (~0)%UInt128 == ~UInt128(0) @test Int128(~0) == ~Int128(0) end diff --git a/test/perf/micro/perf.jl b/test/perf/micro/perf.jl index 2aa7491883c82..2c10b22eaba63 100644 --- a/test/perf/micro/perf.jl +++ b/test/perf/micro/perf.jl @@ -20,7 +20,7 @@ function parseintperf(t) local n, m for i=1:t n = rand(UInt32) - s = base(16, n) + s = string(n, base = 16) m = UInt32(parse(Int64,s, base = 16)) end @test m == n diff --git a/test/perf/micro/perf.py b/test/perf/micro/perf.py index 00a29e4920a84..ef008c90dfd21 100644 --- a/test/perf/micro/perf.py +++ b/test/perf/micro/perf.py @@ -95,7 +95,7 @@ def pisum(): def parse_int(t): for i in range(1,t): n = random.randint(0,2**32-1) - s = base(16, n) + s = string(n, base = 16) if s[-1]=='L': s = s[0:-1] m = int(s,16) diff --git a/test/runtests.jl b/test/runtests.jl index 0b63389c91f4c..ba3a7bc52f730 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -277,7 +277,7 @@ cd(dirname(@__FILE__)) do println(" \033[31;1mFAILURE\033[0m\n") skipped > 0 && println("$skipped test", skipped > 1 ? "s were" : " was", " skipped due to failure.") - println("The global RNG seed was 0x$(base(16, seed)).\n") + println("The global RNG seed was 0x$(string(seed, base = 16)).\n") Test.print_test_errors(o_ts) throw(Test.FallbackTestSetException("Test run finished with errors")) end diff --git a/test/strings/basic.jl b/test/strings/basic.jl index 8e78a166c46e6..42ddd941852dd 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -64,7 +64,7 @@ end b = 2:62, _ = 1:10 n = (T != BigInt) ? rand(T) : BigInt(rand(Int128)) - @test parse(T, base(b, n), base = b) == n + @test parse(T, string(n, base = b), base = b) == n end end end diff --git a/test/strings/io.jl b/test/strings/io.jl index 7d580f76b4708..cb928f65f001c 100644 --- a/test/strings/io.jl +++ b/test/strings/io.jl @@ -80,15 +80,15 @@ "\uFFFF","\U10000","\U10FFF","\U10FFFF"] c = Char(i) cp = string(c,p) - op = string(Char(div(i,8)), base(8, i%8), p) - hp = string(Char(div(i,16)), base(16, i%16), p) - @test string(unescape_string(string("\\",base(8,i,pad=1),p))) == cp - @test string(unescape_string(string("\\",base(8,i,pad=2),p))) == cp - @test string(unescape_string(string("\\",base(8,i,pad=3),p))) == cp - @test string(unescape_string(string("\\",base(8,i,pad=4),p))) == op - @test string(unescape_string(string("\\x",base(16,i,pad=1),p))) == cp - @test string(unescape_string(string("\\x",base(16,i,pad=2),p))) == cp - @test string(unescape_string(string("\\x",base(16,i,pad=3),p))) == hp + op = string(Char(div(i,8)), string(i%8, base = 8), p) + hp = string(Char(div(i,16)), string(i%16, base = 16), p) + @test string(unescape_string(string("\\",string(i,base=8,pad=1),p))) == cp + @test string(unescape_string(string("\\",string(i,base=8,pad=2),p))) == cp + @test string(unescape_string(string("\\",string(i,base=8,pad=3),p))) == cp + @test string(unescape_string(string("\\",string(i,base=8,pad=4),p))) == op + @test string(unescape_string(string("\\x",string(i,base=16,pad=1),p))) == cp + @test string(unescape_string(string("\\x",string(i,base=16,pad=2),p))) == cp + @test string(unescape_string(string("\\x",string(i,base=16,pad=3),p))) == hp end @testset "unescape_string" begin From 571fbf390e4a30f1eaacb49c1923a83c2ddc4b72 Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Fri, 26 Jan 2018 01:00:15 -0500 Subject: [PATCH 10/11] misc cleanup --- base/exports.jl | 1 - base/gmp.jl | 2 +- base/grisu/grisu.jl | 2 +- base/show.jl | 4 ++-- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/base/exports.jl b/base/exports.jl index 2de098ca7cf92..3b8aa50e9f37e 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -573,7 +573,6 @@ export # strings and text output ascii, - base, bitstring, bytes2hex, chomp, diff --git a/base/gmp.jl b/base/gmp.jl index c086f657aa41a..dad0ab24298a6 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -595,7 +595,7 @@ flipsign( x::BigInt, y::BigInt) = signbit(y) ? -x : x show(io::IO, x::BigInt) = print(io, string(x)) -function base(n::BigInt; b::Integer = 10, pad::Integer = 1) +function string(n::BigInt; base::Integer = 10, 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")) iszero(n) && pad < 1 && return "" diff --git a/base/grisu/grisu.jl b/base/grisu/grisu.jl index 15c6a934cdc8e..8c4ce7f52d586 100644 --- a/base/grisu/grisu.jl +++ b/base/grisu/grisu.jl @@ -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, string(pt - 1, base = 10)) + write(io, string(pt - 1)) typed && isa(x,Float16) && write(io, ")") return elseif pt <= 0 diff --git a/base/show.jl b/base/show.jl index 57265a4abd67a..adb916c75d4d3 100644 --- a/base/show.jl +++ b/base/show.jl @@ -553,8 +553,8 @@ 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, string(n, base = 100)); nothing) -show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1), base = 16) +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$(string(UInt(p), base = 16, pad = Sys.WORD_SIZE>>2))") From bfe6f36907edf4fbfb4b609225efdd6bf6c5752a Mon Sep 17 00:00:00 2001 From: Brandon Taylor Date: Sun, 28 Jan 2018 15:31:04 -0500 Subject: [PATCH 11/11] misc changes --- base/deprecated.jl | 22 +++++++++------------- base/gmp.jl | 8 ++++---- base/intfuncs.jl | 12 ++++++------ base/libgit2/oid.jl | 6 +++++- test/intfuncs.jl | 4 ++-- test/libgit2.jl | 2 +- 6 files changed, 27 insertions(+), 27 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index 70ee61d36fbec..f74db55dc84ab 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1665,21 +1665,17 @@ 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) string(n, base = b, pad = pad) -@deprecate bin(n, pad) string(n, base = 2, pad = pad) -@deprecate oct(n, pad) string(n, base = 8, pad = pad) -@deprecate dec(n, pad) string(n, pad = pad) -@deprecate hex(n, pad) string(n, base = 16, pad = pad) +@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) string(UInt32(n), base = 2, pad = pad) -@deprecate oct(n::Char, pad) string(UInt32(n), base = 8, pad = pad) -@deprecate dec(n::Char, pad) string(UInt32(n), pad = pad) -@deprecate hex(n::Char, pad) string(UInt32(n), base = 16, pad = pad) -@deprecate bin(n::Char) string(UInt32(n), base = 2) -@deprecate oct(n::Char) string(UInt32(n), base = 8) -@deprecate dec(n::Char) string(UInt32(n)) -@deprecate hex(n::Char) string(UInt32(n), base = 16) +@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 diff --git a/base/gmp.jl b/base/gmp.jl index dad0ab24298a6..0a5edf59d2074 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -596,13 +596,13 @@ flipsign( x::BigInt, y::BigInt) = signbit(y) ? -x : x show(io::IO, x::BigInt) = print(io, string(x)) function string(n::BigInt; base::Integer = 10, 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")) + 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 diff --git a/base/intfuncs.jl b/base/intfuncs.jl index cf2df8baca7ca..4cd2a417ed18d 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -549,7 +549,7 @@ julia> ndigits(12345) julia> ndigits(1022, 16) 3 -julia> string(1022, bass = 16) +julia> string(1022, base = 16) "3fe" ``` """ @@ -647,20 +647,20 @@ julia> string(13, base = 5, pad = 4) ``` """ string(n::Integer; base::Integer = 10, pad::Integer = 1) = - if b == 2 + if base == 2 (n_positive, neg) = split_sign(n) bin(n_positive, pad, neg) - elseif b == 8 + elseif base == 8 (n_positive, neg) = split_sign(n) oct(n_positive, pad, neg) - elseif b == 10 + elseif base == 10 (n_positive, neg) = split_sign(n) dec(n_positive, pad, neg) - elseif b == 16 + elseif base == 16 (n_positive, neg) = split_sign(n) hex(n_positive, pad, neg) else - base(Int(b), b > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (b>0) & (n<0)) + base(Int(base), base > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (base>0) & (n<0)) end """ diff --git a/base/libgit2/oid.jl b/base/libgit2/oid.jl index db5baa31085d7..9b5d11fc1f309 100644 --- a/base/libgit2/oid.jl +++ b/base/libgit2/oid.jl @@ -162,7 +162,11 @@ Obtain the raw bytes of the [`GitHash`](@ref) as a vector of length $OID_RAWSZ. """ raw(id::GitHash) = collect(id.val) -Base.string(id::GitHash) = join([string(i, base = 16, pad = 2) for i in id.val]) +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))\")") diff --git a/test/intfuncs.jl b/test/intfuncs.jl index ecbe7022ecea7..982e49c034787 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -117,8 +117,8 @@ end @test all(n -> n == 1, ndigits(x, b) for b in [-20:-2;2:20] for x in [true, false]) end @testset "bin/oct/dec/hex/bits" begin - @test string('3', base = 2) == "110011" - @test string('3', pad = 7, base = 2) == "0110011" + @test string(UInt32('3'), base = 2) == "110011" + @test string(UInt32('3'), pad = 7, base = 2) == "0110011" @test string(3, base = 2) == "11" @test string(3, pad = 2, base = 2) == "11" @test string(3, pad = 3, base = 2) == "011" diff --git a/test/libgit2.jl b/test/libgit2.jl index 2cd1d542707cc..e817e10a87500 100644 --- a/test/libgit2.jl +++ b/test/libgit2.jl @@ -887,7 +887,7 @@ mktempdir() do dir LibGit2.with(LibGit2.GitRepo(cache_repo)) do repo # this is slightly dubious, as it assumes the object has not been packed # could be replaced by another binary format - hash_string = string(commit_oid1, base = 16) + hash_string = string(commit_oid1) blob_file = joinpath(cache_repo,".git/objects", hash_string[1:2], hash_string[3:end]) id = LibGit2.addblob!(repo, blob_file)