diff --git a/base/char.jl b/base/char.jl index be185b4c55581..4d994d690a726 100644 --- a/base/char.jl +++ b/base/char.jl @@ -165,7 +165,7 @@ function show(io::IO, ::MIME"text/plain", c::Char) else u = UInt32(c) end - h = hex(u, u ≤ 0xffff ? 4 : 6) + h = string(u, base = 16, pad = u ≤ 0xffff ? 4 : 6) print(io, (isascii(c) ? "ASCII/" : ""), "Unicode U+", h) else print(io, ": Malformed UTF-8") diff --git a/base/deprecated.jl b/base/deprecated.jl index 5ffaeab35a8e7..7196f1f4bddbe 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -419,8 +419,8 @@ function hex2num(s::AbstractString) end export hex2num -@deprecate num2hex(x::Union{Float16,Float32,Float64}) hex(reinterpret(Unsigned, x), sizeof(x)*2) -@deprecate num2hex(n::Integer) hex(n, sizeof(n)*2) +@deprecate num2hex(x::Union{Float16,Float32,Float64}) string(reinterpret(Unsigned, x), base = 16, pad = sizeof(x)*2) +@deprecate num2hex(n::Integer) string(n, base = 16, pad = sizeof(n)*2) # PR #22742: change in isapprox semantics @deprecate rtoldefault(x,y) rtoldefault(x,y,0) false @@ -1394,6 +1394,25 @@ end @deprecate print_with_color(color, args...; kwargs...) printstyled(args...; kwargs..., color=color) +@deprecate base(b, n) string(n, base = b) +@deprecate base(b, n, pad) string(n, base = b, pad = pad) +@deprecate bin(n) string(n, base = 2) +@deprecate bin(n, pad) string(n, base = 2, pad = pad) +@deprecate oct(n) string(n, base = 8) +@deprecate oct(n, pad) string(n, base = 8, pad = pad) +@deprecate dec(n) string(n) +@deprecate dec(n, pad) string(n, pad = pad) +@deprecate hex(n) string(n, base = 16) +@deprecate hex(n, pad) string(n, base = 16, pad = pad) +@deprecate bin(n::Char) string(UInt32(n), base = 2) +@deprecate bin(n::Char, pad) string(UInt32(n), base = 2, pad = pad) +@deprecate oct(n::Char) string(UInt32(n), base = 8) +@deprecate oct(n::Char, pad) string(UInt32(n), base = 8, pad = pad) +@deprecate dec(n::Char) string(UInt32(n)) +@deprecate dec(n::Char, pad) string(UInt32(n), pad = pad) +@deprecate hex(n::Char) string(UInt32(n), base = 16) +@deprecate hex(n::Char, pad) string(UInt32(n), base = 16, pad = pad) + @deprecate which(s::Symbol) which(Main, s) @deprecate IOBuffer(data::AbstractVector{UInt8}, read::Bool, write::Bool=false, maxsize::Integer=typemax(Int)) IOBuffer(data, read=read, write=write, maxsize=maxsize) diff --git a/base/exports.jl b/base/exports.jl index d5f68d99eb436..b7d3241a7eeba 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -559,19 +559,15 @@ export # strings ascii, - base, - bin, bitstring, bytes2hex, chomp, chop, codeunit, codeunits, - dec, digits, digits!, escape_string, - hex, hex2bytes, hex2bytes!, isalpha, @@ -594,7 +590,6 @@ export ncodeunits, ndigits, nextind, - oct, prevind, repeat, replace, diff --git a/base/gmp.jl b/base/gmp.jl index 62a4fe0dacb8c..cd53081e6024b 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -7,7 +7,7 @@ export BigInt import .Base: *, +, -, /, <, <<, >>, >>>, <=, ==, >, >=, ^, (~), (&), (|), xor, binomial, cmp, convert, div, divrem, factorial, fld, gcd, gcdx, lcm, mod, ndigits, promote_rule, rem, show, isqrt, string, powermod, - sum, trailing_zeros, trailing_ones, count_ones, base, tryparse_internal, + sum, trailing_zeros, trailing_ones, count_ones, tryparse_internal, bin, oct, dec, hex, isequal, invmod, prevpow2, nextpow2, ndigits0zpb, widen, signed, unsafe_trunc, trunc, iszero, isone, big, flipsign, signbit, hastypemax @@ -593,27 +593,16 @@ flipsign( x::BigInt, y::Integer) = signbit(y) ? -x : x flipsign( x::BigInt, y::BigInt) = signbit(y) ? -x : x # above method to resolving ambiguities with flipsign(::T, ::T) where T<:Signed -string(x::BigInt) = dec(x) show(io::IO, x::BigInt) = print(io, string(x)) -bin(n::BigInt) = base( 2, n) -oct(n::BigInt) = base( 8, n) -dec(n::BigInt) = base(10, n) -hex(n::BigInt) = base(16, n) - -bin(n::BigInt, pad::Int) = base( 2, n, pad) -oct(n::BigInt, pad::Int) = base( 8, n, pad) -dec(n::BigInt, pad::Int) = base(10, n, pad) -hex(n::BigInt, pad::Int) = base(16, n, pad) - -function base(b::Integer, n::BigInt, pad::Integer=1) - b < 0 && return base(Int(b), n, pad, (b>0) & (n.size<0)) - 2 <= b <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $b")) +function string(n::BigInt; base::Integer = 10, pad::Integer = 1) + base < 0 && return Base._base(Int(base), n, pad, (base>0) & (n.size<0)) + 2 <= base <= 62 || throw(ArgumentError("base must be 2 ≤ base ≤ 62, got $base")) iszero(n) && pad < 1 && return "" - nd1 = ndigits(n, b) + nd1 = ndigits(n, base) nd = max(nd1, pad) sv = Base.StringVector(nd + isneg(n)) - GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, b, n) + GC.@preserve sv MPZ.get_str!(pointer(sv) + nd - nd1, base, n) @inbounds for i = (1:nd-nd1) .+ isneg(n) sv[i] = '0' % UInt8 end diff --git a/base/grisu/grisu.jl b/base/grisu/grisu.jl index 953b386514106..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, dec(pt-1)) + write(io, string(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, string(e)) return elseif pt <= 0 # => 0.000######## diff --git a/base/int.jl b/base/int.jl index 85ab5eda73b2c..51af020a7fc2e 100644 --- a/base/int.jl +++ b/base/int.jl @@ -330,10 +330,10 @@ julia> a = bswap(4) julia> bswap(a) 4 -julia> bin(1) +julia> string(1, base = 2) "1" -julia> bin(bswap(1)) +julia> string(bswap(1), base = 2) "100000000000000000000000000000000000000000000000000000000" ``` """ @@ -717,7 +717,7 @@ if Core.sizeof(Int) == 4 return Int128(div(BigInt(x), BigInt(y))) end function div(x::UInt128, y::UInt128) - return UInt128(div(BigInt(x), BigInt(y))) + return UInt128(div(BigInt(x), BigInt(y)))::UInt128 end function rem(x::Int128, y::Int128) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 198e3417526b7..fd565cb7a9e0a 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -558,7 +558,7 @@ julia> ndigits(12345) julia> ndigits(1022, 16) 3 -julia> base(16, 1022) +julia> string(1022, base = 16) "3fe" ``` """ @@ -566,8 +566,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}) = dec(x) - function bin(x::Unsigned, pad::Int, neg::Bool) i = neg + max(pad,sizeof(x)<<3-leading_zeros(x)) a = StringVector(i) @@ -620,8 +618,7 @@ end const base36digits = ['0':'9';'a':'z'] const base62digits = ['0':'9';'A':'Z';'a':'z'] - -function base(b::Int, x::Integer, pad::Int, neg::Bool) +function _base(b::Int, x::Integer, pad::Int, neg::Bool) (x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `b` must be negative.")) 2 <= abs(b) <= 62 || throw(ArgumentError("base must satisfy 2 ≤ abs(base) ≤ 62, got $b")) digits = abs(b) <= 36 ? base36digits : base62digits @@ -641,97 +638,42 @@ function base(b::Int, x::Integer, pad::Int, neg::Bool) String(a) end +split_sign(n::Integer) = unsigned(abs(n)), n < 0 +split_sign(n::Unsigned) = n, false + """ - base(base::Integer, n::Integer, pad::Integer=1) + string(n::Integer; base::Integer = 10, pad::Integer = 1) Convert an integer `n` to a string in the given `base`, optionally specifying a number of digits to pad to. ```jldoctest -julia> base(13,5,4) +julia> string(5, base = 13, pad = 4) "0005" -julia> base(5,13,4) +julia> string(13, base = 5, pad = 4) "0023" ``` """ -base(b::Integer, n::Integer, pad::Integer=1) = - base(Int(b), b > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (b>0) & (n<0)) - -for sym in (:bin, :oct, :dec, :hex) - @eval begin - ($sym)(x::Unsigned, p::Int) = ($sym)(x,p,false) - ($sym)(x::Unsigned) = ($sym)(x,1,false) - ($sym)(x::Char, p::Int) = ($sym)(UInt32(x),p,false) - ($sym)(x::Char) = ($sym)(UInt32(x),1,false) - ($sym)(x::Integer, p::Int) = ($sym)(unsigned(abs(x)),p,x<0) - ($sym)(x::Integer) = ($sym)(unsigned(abs(x)),1,x<0) +function string(n::Integer; base::Integer = 10, pad::Integer = 1) + if base == 2 + (n_positive, neg) = split_sign(n) + bin(n_positive, pad, neg) + elseif base == 8 + (n_positive, neg) = split_sign(n) + oct(n_positive, pad, neg) + elseif base == 10 + (n_positive, neg) = split_sign(n) + dec(n_positive, pad, neg) + elseif base == 16 + (n_positive, neg) = split_sign(n) + hex(n_positive, pad, neg) + else + _base(Int(base), base > 0 ? unsigned(abs(n)) : convert(Signed, n), Int(pad), (base>0) & (n<0)) end end -""" - bin(n, pad::Int=1) - -Convert an integer to a binary string, optionally specifying a number of digits to pad to. - -```jldoctest -julia> bin(10,2) -"1010" - -julia> bin(10,8) -"00001010" -``` -""" -bin - -""" - hex(n, pad::Int=1) - -Convert an integer to a hexadecimal string, optionally specifying a number of -digits to pad to. - -```jldoctest -julia> hex(20) -"14" - -julia> hex(20, 3) -"014" -``` -""" -hex - -""" - oct(n, pad::Int=1) - -Convert an integer to an octal string, optionally specifying a number of digits -to pad to. - -```jldoctest -julia> oct(20) -"24" - -julia> oct(20, 3) -"024" -``` -""" -oct - -""" - dec(n, pad::Int=1) - -Convert an integer to a decimal string, optionally specifying a number of digits -to pad to. - -# Examples -```jldoctest -julia> dec(20) -"20" - -julia> dec(20, 3) -"020" -``` -""" -dec +string(b::Bool) = b ? "true" : "false" """ bitstring(n) @@ -749,11 +691,11 @@ julia> bitstring(2.2) """ function bitstring end -bitstring(x::Union{Bool,Int8,UInt8}) = bin(reinterpret(UInt8,x),8) -bitstring(x::Union{Int16,UInt16,Float16}) = bin(reinterpret(UInt16,x),16) -bitstring(x::Union{Char,Int32,UInt32,Float32}) = bin(reinterpret(UInt32,x),32) -bitstring(x::Union{Int64,UInt64,Float64}) = bin(reinterpret(UInt64,x),64) -bitstring(x::Union{Int128,UInt128}) = bin(reinterpret(UInt128,x),128) +bitstring(x::Union{Bool,Int8,UInt8}) = string(reinterpret(UInt8,x), pad = 8, base = 2) +bitstring(x::Union{Int16,UInt16,Float16}) = string(reinterpret(UInt16,x), pad = 16, base = 2) +bitstring(x::Union{Char,Int32,UInt32,Float32}) = string(reinterpret(UInt32,x), pad = 32, base = 2) +bitstring(x::Union{Int64,UInt64,Float64}) = string(reinterpret(UInt64,x), pad = 64, base = 2) +bitstring(x::Union{Int128,UInt128}) = string(reinterpret(UInt128,x), pad = 128, base = 2) """ digits([T<:Integer], n::Integer; base::T = 10, pad::Integer = 1) diff --git a/base/logging.jl b/base/logging.jl index 6bcb82cdf6156..976825ed646c3 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, '_', 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/operators.jl b/base/operators.jl index 1ec7d76bad003..ff6e233d0f163 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -782,14 +782,11 @@ entered in the Julia REPL (and most editors, appropriately configured) by typing # Examples ```jldoctest -julia> map(uppercase∘hex, 250:255) -6-element Array{String,1}: - "FA" - "FB" - "FC" - "FD" - "FE" - "FF" +julia> map(uppercase∘first, ["apple", "banana", "carrot"]) +3-element Array{Char,1}: + 'A' + 'B' + 'C' ``` """ ∘(f, g) = (x...)->f(g(x...)) @@ -874,4 +871,4 @@ julia> map(splat(+), zip(1:3,4:6)) 9 ``` """ -splat(f) = args->f(args...) \ No newline at end of file +splat(f) = args->f(args...) diff --git a/base/printf.jl b/base/printf.jl index 223695889a757..9b62a635bdd4d 100644 --- a/base/printf.jl +++ b/base/printf.jl @@ -695,7 +695,7 @@ function gen_p(flags::String, width::Int, precision::Int, c::Char) end push!(blk.args, :(write(out, '0'))) push!(blk.args, :(write(out, 'x'))) - push!(blk.args, :(write(out, String(hex(unsigned($x), $ptrwidth))))) + push!(blk.args, :(write(out, String(string(unsigned($x), pad = $ptrwidth, base = 16))))) if width > 0 && '-' in flags push!(blk.args, pad(width, width, ' ')) end diff --git a/base/show.jl b/base/show.jl index 2208427a7ec58..5341d26e4d51e 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, string(unsafe_load(convert(Ptr{UInt8}, p + i)), base = 16, pad = 2)) end end end @@ -552,11 +552,11 @@ end show(io::IO, ::Nothing) = print(io, "nothing") show(io::IO, b::Bool) = print(io, b ? "true" : "false") -show(io::IO, n::Signed) = (write(io, dec(n)); nothing) -show(io::IO, n::Unsigned) = print(io, "0x", hex(n,sizeof(n)<<1)) -print(io::IO, n::Unsigned) = print(io, dec(n)) +show(io::IO, n::Signed) = (write(io, string(n)); nothing) +show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16)) +print(io::IO, n::Unsigned) = print(io, string(n)) -show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(hex(UInt(p), Sys.WORD_SIZE>>2))") +show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(string(UInt(p), base = 16, pad = Sys.WORD_SIZE>>2))") has_tight_type(p::Pair) = typeof(p.first) == typeof(p).parameters[1] && diff --git a/base/stat.jl b/base/stat.jl index 945d8ef3d92dc..fc1e3c399c7f2 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$(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 a2968fdaef19d..5601fd49c03d0 100644 --- a/base/strings/io.jl +++ b/base/strings/io.jl @@ -271,16 +271,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", string(UInt32(c), base = 16, 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(peek(a)) ? 4 : 2)) : - print(io, "\\U", hex(c, need_full_hex(peek(a)) ? 8 : 4)) + c <= '\x7f' ? print(io, "\\x", string(UInt32(c), base = 16, pad = 2)) : + c <= '\uffff' ? print(io, "\\u", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 4 : 2)) : + print(io, "\\U", string(UInt32(c), base = 16, pad = need_full_hex(peek(a)) ? 8 : 4)) else # malformed or overlong u = bswap(reinterpret(UInt32, c)) while true - print(io, "\\x", hex(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 c20ed86fb7aee..397613e57f271 100644 --- a/base/strings/util.jl +++ b/base/strings/util.jl @@ -446,7 +446,7 @@ See also [`hex2bytes!`](@ref) for an in-place version, and [`bytes2hex`](@ref) f # Examples ```jldoctest -julia> s = hex(12345) +julia> s = string(12345, base = 16) "3039" julia> hex2bytes(s) @@ -512,7 +512,7 @@ Convert an array of bytes to its hexadecimal representation. All characters are in lower-case. # Examples ```jldoctest -julia> a = hex(12345) +julia> a = string(12345, base = 16) "3039" julia> b = hex2bytes(a) diff --git a/base/task.jl b/base/task.jl index 79da4c4e07959..9c89a78165c3a 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$(string(convert(UInt, pointer_from_objref(t)), base = 16, pad = Sys.WORD_SIZE>>2))") end """ diff --git a/base/util.jl b/base/util.jl index 6eb83e6cb7f50..b8e4094cc9add 100644 --- a/base/util.jl +++ b/base/util.jl @@ -675,7 +675,7 @@ function runtests(tests = ["all"]; ncores = ceil(Int, Sys.CPU_CORES / 2), tests = split(tests) end exit_on_error && push!(tests, "--exit-on-error") - seed != nothing && push!(tests, "--seed=0x$(hex(seed % UInt128))") # cast to UInt128 to avoid a minus sign + seed != nothing && push!(tests, "--seed=0x$(string(seed % UInt128, base=16))") # cast to UInt128 to avoid a minus sign ENV2 = copy(ENV) ENV2["JULIA_CPU_CORES"] = "$ncores" try diff --git a/doc/src/base/numbers.md b/doc/src/base/numbers.md index 033590a69c0f6..deea1c2746ea1 100644 --- a/doc/src/base/numbers.md +++ b/doc/src/base/numbers.md @@ -41,11 +41,6 @@ Base.Irrational ## Data Formats ```@docs -Base.bin -Base.hex -Base.dec -Base.oct -Base.base Base.digits Base.digits! Base.bitstring diff --git a/doc/src/manual/unicode-input.md b/doc/src/manual/unicode-input.md index b95130160478c..e7dd6532a3c94 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(string(UInt32(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 0efef534b7510..ffcde456ddf49 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, 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 = dec(y, n) + str = string(y, base = 10, pad = n) l = lastindex(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 = string(div(ms, 100)) elseif ms % 10 == 0 - str = dec(div(ms, 10), 2) + str = string(div(ms, 10), pad = 2) else - str = dec(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 dd27fa6e62b8d..6b7545bed4f2a 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, "$(string(LPROC.bind_port))#") # print port print(out, LPROC.bind_addr) print(out, '\n') flush(out) diff --git a/stdlib/LibGit2/src/blob.jl b/stdlib/LibGit2/src/blob.jl index d306ab96924bf..bbc69ef390bc9 100644 --- a/stdlib/LibGit2/src/blob.jl +++ b/stdlib/LibGit2/src/blob.jl @@ -56,7 +56,7 @@ Return the [`GitHash`](@ref) of the resulting blob. # Examples ```julia -hash_str = hex(commit_oid) +hash_str = string(commit_oid) blob_file = joinpath(repo_path, ".git", "objects", hash_str[1:2], hash_str[3:end]) id = LibGit2.addblob!(repo, blob_file) ``` diff --git a/stdlib/LibGit2/src/deprecated.jl b/stdlib/LibGit2/src/deprecated.jl index 5e92ffe733865..d5e880ec5af3d 100644 --- a/stdlib/LibGit2/src/deprecated.jl +++ b/stdlib/LibGit2/src/deprecated.jl @@ -58,4 +58,7 @@ end # PR #24594 @deprecate AbstractCredentials AbstractCredential false @deprecate UserPasswordCredentials UserPasswordCredential false -@deprecate SSHCredentials SSHCredential false \ No newline at end of file +@deprecate SSHCredentials SSHCredential false + +@deprecate hex(id::LibGit2.GitHash) string(id) +@deprecate hex(id::LibGit2.GitShortHash) string(id) diff --git a/stdlib/LibGit2/src/oid.jl b/stdlib/LibGit2/src/oid.jl index e54600adb25c3..904f845d1434f 100644 --- a/stdlib/LibGit2/src/oid.jl +++ b/stdlib/LibGit2/src/oid.jl @@ -157,9 +157,6 @@ function GitShortHash(obj::GitObject) return sid end -Base.hex(id::GitHash) = join([hex(i,2) for i in id.val]) -Base.hex(id::GitShortHash) = hex(id.hash)[1:id.len] - """ raw(id::GitHash) -> Vector{UInt8} @@ -167,7 +164,12 @@ Obtain the raw bytes of the [`GitHash`](@ref) as a vector of length $OID_RAWSZ. """ raw(id::GitHash) = collect(id.val) -Base.string(id::AbstractGitHash) = hex(id) +function Base.print(io::IO, id::GitHash) + for i in id.val + print(io, string(i, base = 16, pad = 2)) + end +end +Base.string(id::GitShortHash) = string(id.hash)[1:id.len] Base.show(io::IO, id::GitHash) = print(io, "GitHash(\"$(string(id))\")") Base.show(io::IO, id::GitShortHash) = print(io, "GitShortHash(\"$(string(id))\")") diff --git a/stdlib/LibGit2/src/types.jl b/stdlib/LibGit2/src/types.jl index 45981a7d5801d..46e5812da95e9 100644 --- a/stdlib/LibGit2/src/types.jl +++ b/stdlib/LibGit2/src/types.jl @@ -469,7 +469,7 @@ The fields represent: flags. The `i`th bit of this integer sets the `i`th flag. * `mode`: the [`stat`](@ref) mode for the item. * `id_abbrev`: only present in LibGit2 versions newer than or equal to `0.25.0`. - The length of the `id` field when converted using [`hex`](@ref). Usually equal to `OID_HEXSZ` ($OID_HEXSZ). + The length of the `id` field when converted using [`string`](@ref). Usually equal to `OID_HEXSZ` ($OID_HEXSZ). """ struct DiffFile id::GitHash diff --git a/stdlib/LibGit2/test/libgit2.jl b/stdlib/LibGit2/test/libgit2.jl index 4fda8e6dc5cfd..f12cb8cc4d027 100644 --- a/stdlib/LibGit2/test/libgit2.jl +++ b/stdlib/LibGit2/test/libgit2.jl @@ -778,7 +778,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 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) @@ -787,7 +787,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(string(commit_oid1), string(short_oid2)) LibGit2.with(LibGit2.GitCommit(repo, short_oid2)) do cmt2 @test commit_oid1 == LibGit2.GitHash(cmt2) @@ -985,7 +985,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 = string(commit_oid1) blob_file = joinpath(cache_repo,".git/objects", hash_string[1:2], hash_string[3:end]) id = LibGit2.addblob!(repo, blob_file) @@ -2764,4 +2764,4 @@ let cache = LibGit2.CachedCredentials() @test cache["foo"].pass == "\0\0\0" end -end # module \ No newline at end of file +end # module diff --git a/stdlib/Profile/src/Profile.jl b/stdlib/Profile/src/Profile.jl index 58287d4e4caa9..5b109d96d2105 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})), + 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 c9ab7d8ca25a2..28ff70fda8d62 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 42ec3cd38408b..6b2e423e9a8c6 100644 --- a/stdlib/Serialization/src/Serialization.jl +++ b/stdlib/Serialization/src/Serialization.jl @@ -307,7 +307,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/stdlib/Sockets/src/IPAddr.jl b/stdlib/Sockets/src/IPAddr.jl index 529956197dc1c..f024b1f94e7f3 100644 --- a/stdlib/Sockets/src/IPAddr.jl +++ b/stdlib/Sockets/src/IPAddr.jl @@ -44,10 +44,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,string((ip.host&(0xFF000000))>>24),".", + string((ip.host&(0xFF0000))>>16),".", + string((ip.host&(0xFF00))>>8),".", + string(ip.host&0xFF)) struct IPv6 <: IPAddr host::UInt128 @@ -97,7 +97,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,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/test/bigint.jl b/test/bigint.jl index b4886df8220c2..c1372afea4f52 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 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,33 +347,33 @@ 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 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) -for f in (bin, oct, dec, hex) - local f - @test f(big(0), 0) == "" +for base in (2, 8, 10, 16) + local base + @test string(big(0), base=base, pad=0) == "" end -@test base(rand(2:62), big(0), 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 dcf9653bbb380..484fd47c3cbad 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -142,23 +142,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 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 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" + @test string(-3, base = 2) == "-11" + @test string(-3, pad = 3, base = 2) == "-011" - @test oct(9) == "11" - @test oct(-9) == "-11" + @test string(9, base = 8) == "11" + @test string(-9, base = 8) == "-11" - @test dec(121) == "121" + @test string(121, base = 10) == "121" - @test hex(12) == "c" - @test hex(-12, 3) == "-00c" + @test string(12, base = 16) == "c" + @test string(-12, pad = 3, base = 16) == "-00c" - @test base(2, 5, 7) == "0000101" + @test string(5, pad = 7, base = 2) == "0000101" @test bitstring(Int16(3)) == "0000000000000011" @test bitstring('3') == "00110011000000000000000000000000" @@ -177,7 +177,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/numbers.jl b/test/numbers.jl index ff87fa148ab50..fbff83d03c501 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 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 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 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 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 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 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 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 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 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 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 dec(typemin(UInt128)) == "0" - @test dec(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 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 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 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 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 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 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 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 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 = 10) == "9223372036854775807" + @test string(typemin(Int64), base = 16) == "-8000000000000000" + @test string(typemax(Int64), base = 16) == "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 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 dec(typemin(Int128)) == "-170141183460469231731687303715884105728" - @test dec(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" @@ -1778,7 +1778,7 @@ end @test isinf(nextfloat(0x1.fffffffffffffp1023)) end @testset "issue #1308" begin - @test hex(~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/operators.jl b/test/operators.jl index 5161a2977c69a..7cf493296e00b 100644 --- a/test/operators.jl +++ b/test/operators.jl @@ -101,7 +101,7 @@ Base.promote_rule(::Type{T19714}, ::Type{Int}) = T19714 # pr #17155 @testset "function composition" begin - @test (uppercase∘hex)(239487) == "3A77F" + @test (uppercase∘(x->string(x,base=16)))(239487) == "3A77F" end @testset "function negation" begin str = randstring(20) diff --git a/test/perf/micro/perf.jl b/test/perf/micro/perf.jl index 93ef57e124a75..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 = hex(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 843da237e31d0..18d1edd0fd264 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 = string(n, base = 16) 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 e1a36c521a26c..f4630e82b80b2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -287,7 +287,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$(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 92273526fa442..a63d618c2633c 100644 --- a/test/strings/basic.jl +++ b/test/strings/basic.jl @@ -66,7 +66,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 28f7d9bdf5e4b..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)), 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)), 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