Skip to content

Commit

Permalink
Fix pad and base type restriction (JuliaLang#30881)
Browse files Browse the repository at this point in the history
  • Loading branch information
musm authored and ararslan committed Jan 29, 2019
1 parent 05acc22 commit c26954e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
14 changes: 7 additions & 7 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
9 changes: 9 additions & 0 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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)
Expand Down

0 comments on commit c26954e

Please sign in to comment.