Skip to content

Commit 6e06b3d

Browse files
committed
Change pad option handling
1 parent 5ca8669 commit 6e06b3d

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed

base/intfuncs.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -615,9 +615,9 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba
615615

616616
## integer to string functions ##
617617

618-
function bin(x::Unsigned, pad::Integer, neg::Bool)
618+
function bin(x::Unsigned, pad::Int, neg::Bool)
619619
m = 8sizeof(x) - leading_zeros(x)::Int
620-
n = neg + max((pad % Int)::Int, m)
620+
n = neg + max(pad, m)
621621
a = StringVector(n)
622622
# for i in 0x0:UInt(n-1) # automatic vectorization produces redundant codes
623623
# @inbounds a[n - i] = 0x30 + (((x >> i) % UInt8)::UInt8 & 0x1)
@@ -642,9 +642,9 @@ function bin(x::Unsigned, pad::Integer, neg::Bool)
642642
String(a)
643643
end
644644

645-
function oct(x::Unsigned, pad::Integer, neg::Bool)
645+
function oct(x::Unsigned, pad::Int, neg::Bool)
646646
m = div(8sizeof(x) - leading_zeros(x)::Int + 2, 3)
647-
n = neg + max((pad % Int)::Int, m)
647+
n = neg + max(pad, m)
648648
a = StringVector(n)
649649
i = n
650650
while i > neg
@@ -659,8 +659,8 @@ end
659659
# 2-digit decimal characters ("00":"99")
660660
const _dec_d100 = UInt16[(0x30 + i % 10) << 0x8 + (0x30 + i ÷ 10) for i = 0:99]
661661

662-
function dec(x::Unsigned, pad::Integer, neg::Bool)
663-
n = neg + ndigits(x, base=10, pad=(pad % Int)::Int)::Int
662+
function dec(x::Unsigned, pad::Int, neg::Bool)
663+
n = neg + (ndigits(x, base=10, pad=pad) % Int)::Int
664664
a = StringVector(n)
665665
i = n
666666
@inbounds while i >= 2
@@ -678,9 +678,9 @@ function dec(x::Unsigned, pad::Integer, neg::Bool)
678678
String(a)
679679
end
680680

681-
function hex(x::Unsigned, pad::Integer, neg::Bool)
681+
function hex(x::Unsigned, pad::Int, neg::Bool)
682682
m = 2sizeof(x) - (leading_zeros(x)::Int >> 2)
683-
n = neg + max((pad % Int)::Int, m)
683+
n = neg + max(pad, m)
684684
a = StringVector(n)
685685
i = n
686686
while i >= 2
@@ -702,12 +702,12 @@ end
702702
const base36digits = UInt8['0':'9';'a':'z']
703703
const base62digits = UInt8['0':'9';'A':'Z';'a':'z']
704704

705-
function _base(base::Integer, x::Integer, pad::Integer, neg::Bool)
705+
function _base(base::Integer, x::Integer, pad::Int, neg::Bool)
706+
(x >= 0) | (base < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
707+
2 <= abs(base) <= 62 || throw(DomainError(base, "base must satisfy 2 ≤ abs(base) ≤ 62"))
706708
b = (base % Int)::Int
707-
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
708-
2 <= abs(b) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62"))
709709
digits = abs(b) <= 36 ? base36digits : base62digits
710-
n = neg + ndigits(x, base=b, pad=(pad % Int)::Int)::Int
710+
n = neg + (ndigits(x, base=b, pad=pad) % Int)::Int
711711
a = StringVector(n)
712712
i = n
713713
@inbounds while i > neg
@@ -742,6 +742,7 @@ julia> string(13, base = 5, pad = 4)
742742
```
743743
"""
744744
function string(n::Integer; base::Integer = 10, pad::Integer = 1)
745+
pad = (min(max(pad, typemin(Int)), typemax(Int)) % Int)::Int
745746
if base == 2
746747
(n_positive, neg) = split_sign(n)
747748
bin(n_positive, pad, neg)

test/intfuncs.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ end
304304
@test string(3, base = 2) == "11"
305305
@test string(3, pad = 2, base = 2) == "11"
306306
@test string(3, pad = Int32(2), base = Int32(2)) == "11"
307+
@test string(3, pad = typemin(Int128) + 3, base = 0x2) == "11"
307308
@test string(3, pad = 3, base = 2) == "011"
308309
@test string(-3, base = 2) == "-11"
309310
@test string(-3, pad = 3, base = 2) == "-011"

0 commit comments

Comments
 (0)