Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

faster digits(::BigInt) #37075

Merged
merged 14 commits into from
Aug 18, 2020
11 changes: 11 additions & 0 deletions base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -694,6 +694,17 @@ function string(n::BigInt; base::Integer = 10, pad::Integer = 1)
String(sv)
end

function digits(n::BigInt; base::Integer = 10, pad::Integer = 1)
if base ≥ 2
if base ≤ 36 # 0-9, plus a-z for 10-35
return reverse!(map(x -> Int(x>0x39 ? x-0x57 : x-0x30), codeunits(string(n; base, pad))))
elseif base ≤ 62 # 0-9, plus A-Z for 10-35 and a-z for 36..61
return reverse!(map(x -> Int(x>0x39 ? (x>0x60 ? x-0x3d : x-0x37) : x-0x30), codeunits(string(n; base, pad))))
end
end
return invoke(digits, Tuple{Integer}, n; base, pad) # slow generic fallback
end

function ndigits0zpb(x::BigInt, b::Integer)
b < 2 && throw(DomainError(b, "`b` cannot be less than 2."))
x.size == 0 && return 0 # for consistency with other ndigits0z methods
Expand Down