From c26954ef961722a18453de31997002eaae9b6ded Mon Sep 17 00:00:00 2001 From: Mus Date: Tue, 29 Jan 2019 12:11:47 -0500 Subject: [PATCH] Fix pad and base type restriction (#30881) --- base/intfuncs.jl | 14 +++++++------- test/intfuncs.jl | 9 +++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/base/intfuncs.jl b/base/intfuncs.jl index 8da0845258d13..da758c2c60941 100644 --- a/base/intfuncs.jl +++ b/base/intfuncs.jl @@ -537,11 +537,11 @@ julia> ndigits(123, pad=5) 5 ``` """ -ndigits(x::Integer; base::Integer=10, pad::Int=1) = max(pad, ndigits0z(x, base)) +ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, base)) ## integer to string functions ## -function bin(x::Unsigned, pad::Int, neg::Bool) +function bin(x::Unsigned, pad::Integer, neg::Bool) i = neg + max(pad,sizeof(x)<<3-leading_zeros(x)) a = StringVector(i) while i > neg @@ -553,7 +553,7 @@ function bin(x::Unsigned, pad::Int, neg::Bool) String(a) end -function oct(x::Unsigned, pad::Int, neg::Bool) +function oct(x::Unsigned, pad::Integer, neg::Bool) i = neg + max(pad,div((sizeof(x)<<3)-leading_zeros(x)+2,3)) a = StringVector(i) while i > neg @@ -565,7 +565,7 @@ function oct(x::Unsigned, pad::Int, neg::Bool) String(a) end -function dec(x::Unsigned, pad::Int, neg::Bool) +function dec(x::Unsigned, pad::Integer, neg::Bool) i = neg + ndigits(x, base=10, pad=pad) a = StringVector(i) while i > neg @@ -577,7 +577,7 @@ function dec(x::Unsigned, pad::Int, neg::Bool) String(a) end -function hex(x::Unsigned, pad::Int, neg::Bool) +function hex(x::Unsigned, pad::Integer, neg::Bool) i = neg + max(pad,(sizeof(x)<<1)-(leading_zeros(x)>>2)) a = StringVector(i) while i > neg @@ -593,7 +593,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::Integer, x::Integer, pad::Integer, 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 @@ -644,7 +644,7 @@ function string(n::Integer; base::Integer = 10, pad::Integer = 1) (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)) + _base(base, base > 0 ? unsigned(abs(n)) : convert(Signed, n), pad, (base>0) & (n<0)) end end diff --git a/test/intfuncs.jl b/test/intfuncs.jl index 37033bde31fa4..4bf9d3b71b4bf 100644 --- a/test/intfuncs.jl +++ b/test/intfuncs.jl @@ -154,17 +154,23 @@ end @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 = Int32(2), base = Int32(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 string(9, base = 8) == "11" @test string(-9, base = 8) == "-11" + @test string(-9, base = 8, pad = 5) == "-00011" + @test string(-9, base = 8, pad = Int32(5)) == "-00011" @test string(121, base = 10) == "121" + @test string(121, base = 10, pad = 5) == "00121" + @test string(121, base = 10, pad = 5) == "00121" @test string(12, base = 16) == "c" @test string(-12, pad = 3, base = 16) == "-00c" + @test string(-12, pad = Int32(3), base = Int32(16)) == "-00c" @test string(5, pad = 7, base = 2) == "0000101" @@ -177,6 +183,9 @@ end @testset "digits/base" begin @test digits(4, base = 2) == [0, 0, 1] @test digits(5, base = 3) == [2, 1] + @test digits(5, base = Int32(2), pad=Int32(3)) == [1, 0, 1] + @test digits(5, pad = 3) == [5, 0, 0] + @test digits(5, pad = Int32(3)) == [5, 0, 0] @testset "digits/base with negative bases" begin @testset "digits(n::$T, base = b)" for T in (Int, UInt, BigInt, Int32, UInt32)