Skip to content

Commit

Permalink
Change pad option handling
Browse files Browse the repository at this point in the history
  • Loading branch information
kimikage committed Jul 2, 2020
1 parent c50a1f6 commit 4264839
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
25 changes: 13 additions & 12 deletions base/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -601,9 +601,9 @@ ndigits(x::Integer; base::Integer=10, pad::Integer=1) = max(pad, ndigits0z(x, ba

## integer to string functions ##

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

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

function dec(x::Unsigned, pad::Integer, neg::Bool)
n = neg + ndigits(x, base=10, pad=(pad % Int)::Int)::Int
function dec(x::Unsigned, pad::Int, neg::Bool)
n = neg + (ndigits(x, base=10, pad=pad) % Int)::Int
a = StringVector(n)
i = n
@inbounds while i >= 2
Expand All @@ -664,9 +664,9 @@ function dec(x::Unsigned, pad::Integer, neg::Bool)
String(a)
end

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

function _base(base::Integer, x::Integer, pad::Integer, neg::Bool)
function _base(base::Integer, x::Integer, pad::Int, neg::Bool)
(x >= 0) | (base < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
2 <= abs(base) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62"))
b = (base % Int)::Int
(x >= 0) | (b < 0) || throw(DomainError(x, "For negative `x`, `base` must be negative."))
2 <= abs(b) <= 62 || throw(DomainError(b, "base must satisfy 2 ≤ abs(base) ≤ 62"))
digits = abs(b) <= 36 ? base36digits : base62digits
n = neg + ndigits(x, base=b, pad=(pad % Int)::Int)::Int
n = neg + (ndigits(x, base=b, pad=pad) % Int)::Int
a = StringVector(n)
i = n
@inbounds while i > neg
Expand Down Expand Up @@ -728,6 +728,7 @@ julia> string(13, base = 5, pad = 4)
```
"""
function string(n::Integer; base::Integer = 10, pad::Integer = 1)
pad = (min(max(pad, typemin(Int)), typemax(Int)) % Int)::Int
if base == 2
(n_positive, neg) = split_sign(n)
bin(n_positive, pad, neg)
Expand Down
1 change: 1 addition & 0 deletions test/intfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ end
@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 = typemin(Int128) + 3, base = 0x2) == "11"
@test string(3, pad = 3, base = 2) == "011"
@test string(-3, base = 2) == "-11"
@test string(-3, pad = 3, base = 2) == "-011"
Expand Down

0 comments on commit 4264839

Please sign in to comment.