Skip to content

Commit 4264839

Browse files
committed
Change pad option handling
1 parent c50a1f6 commit 4264839

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
@@ -601,9 +601,9 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba
601601

602602
## integer to string functions ##
603603

604-
function bin(x::Unsigned, pad::Integer, neg::Bool)
604+
function bin(x::Unsigned, pad::Int, neg::Bool)
605605
m = 8sizeof(x) - leading_zeros(x)::Int
606-
n = neg + max((pad % Int)::Int, m)
606+
n = neg + max(pad, m)
607607
a = StringVector(n)
608608
# for i in 0x0:UInt(n-1) # automatic vectorization produces redundant codes
609609
# @inbounds a[n - i] = 0x30 + (((x >> i) % UInt8)::UInt8 & 0x1)
@@ -628,9 +628,9 @@ function bin(x::Unsigned, pad::Integer, neg::Bool)
628628
String(a)
629629
end
630630

631-
function oct(x::Unsigned, pad::Integer, neg::Bool)
631+
function oct(x::Unsigned, pad::Int, neg::Bool)
632632
m = div(8sizeof(x) - leading_zeros(x)::Int + 2, 3)
633-
n = neg + max((pad % Int)::Int, m)
633+
n = neg + max(pad, m)
634634
a = StringVector(n)
635635
i = n
636636
while i > neg
@@ -645,8 +645,8 @@ end
645645
# 2-digit decimal characters ("00":"99")
646646
const _dec_d100 = UInt16[(0x30 + i % 10) << 0x8 + (0x30 + i ÷ 10) for i = 0:99]
647647

648-
function dec(x::Unsigned, pad::Integer, neg::Bool)
649-
n = neg + ndigits(x, base=10, pad=(pad % Int)::Int)::Int
648+
function dec(x::Unsigned, pad::Int, neg::Bool)
649+
n = neg + (ndigits(x, base=10, pad=pad) % Int)::Int
650650
a = StringVector(n)
651651
i = n
652652
@inbounds while i >= 2
@@ -664,9 +664,9 @@ function dec(x::Unsigned, pad::Integer, neg::Bool)
664664
String(a)
665665
end
666666

667-
function hex(x::Unsigned, pad::Integer, neg::Bool)
667+
function hex(x::Unsigned, pad::Int, neg::Bool)
668668
m = 2sizeof(x) - (leading_zeros(x)::Int >> 2)
669-
n = neg + max((pad % Int)::Int, m)
669+
n = neg + max(pad, m)
670670
a = StringVector(n)
671671
i = n
672672
while i >= 2
@@ -688,12 +688,12 @@ end
688688
const base36digits = UInt8['0':'9';'a':'z']
689689
const base62digits = UInt8['0':'9';'A':'Z';'a':'z']
690690

691-
function _base(base::Integer, x::Integer, pad::Integer, neg::Bool)
691+
function _base(base::Integer, x::Integer, pad::Int, neg::Bool)
692+
(x >= 0) | (base < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
693+
2 <= abs(base) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62"))
692694
b = (base % Int)::Int
693-
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
694-
2 <= abs(b) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62"))
695695
digits = abs(b) <= 36 ? base36digits : base62digits
696-
n = neg + ndigits(x, base=b, pad=(pad % Int)::Int)::Int
696+
n = neg + (ndigits(x, base=b, pad=pad) % Int)::Int
697697
a = StringVector(n)
698698
i = n
699699
@inbounds while i > neg
@@ -728,6 +728,7 @@ julia> string(13, base = 5, pad = 4)
728728
```
729729
"""
730730
function string(n::Integer; base::Integer = 10, pad::Integer = 1)
731+
pad = (min(max(pad, typemin(Int)), typemax(Int)) % Int)::Int
731732
if base == 2
732733
(n_positive, neg) = split_sign(n)
733734
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)