Skip to content

Commit 2d0c28b

Browse files
DrviquinnjNHDaly
authored
Apply suggestions from code review
Co-authored-by: Jacob Quinn <quinn.jacobd@gmail.com> Co-authored-by: Nathan Daly <nathan.daly@relational.ai>
1 parent 99ca92e commit 2d0c28b

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

src/parse.jl

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using Parsers
2-
using Parsers: AbstractConf, SourceType, XOPTIONS, Result
2+
using Parsers: AbstractConf, Result
33

44
"""
55
RoundThrows
@@ -8,11 +8,15 @@ Raises an `InexactError` if any rounding is necessary.
88
"""
99
const RoundThrows = RoundingMode{:Throw}()
1010

11+
# make our own conf struct to avoid specializing Parsers.typeparser on each unique precision value
1112
struct FixedDecimalConf{T<:Integer} <: AbstractConf{T}
1213
f::Int
1314
end
15+
# this overload says that when parsing a FixedDecimal type, use our new custom FixedDecimalConf type
1416
Parsers.conf(::Type{FixedDecimal{T,f}}, opts::Parsers.Options, kw...) where {T<:Integer,f} = FixedDecimalConf{T}(f)
17+
# Because the value returned from our `typeparser` isn't a FixedDecimal, we overload here to show we're returning an integer type
1518
Parsers.returntype(::Type{FixedDecimal{T,f}}) where {T,f} = T
19+
# this overload allows us to take the Result{IntegerType} returned from typeparser and turn it into a FixedDecimal Result
1620
function Parsers.result(FD::Type{FixedDecimal{T,f}}, res::Parsers.Result{T}) where {T,f}
1721
return Parsers.invalid(res.code) ? Result{FD}(res.code, res.tlen) :
1822
Result{FD}(res.code, res.tlen, reinterpret(FD, res.val))
@@ -98,11 +102,12 @@ function _divpow10!(x::BigInt, code, pow, ::RoundingMode{:Throw})
98102
end
99103

100104
# Rescale the digits we accumulated so far into the the a an integer representing the decimal
105+
# Note the 2nd argument `FloatType` is used by Parsers.jl for _float_ parsing, but we can ignore in the fixed decimal case
101106
@inline function Parsers.scale(
102107
conf::FixedDecimalConf{T}, ::Parsers.FloatType, digits::V, exp, neg, code, ndigits, f::F, options::Parsers.Options
103108
) where {T,V,F}
104109
rounding = something(options.rounding, RoundThrows)
105-
# Positive: how many trailing zeroes we need to add to out integer
110+
# Positive: how many trailing zeroes we need to add to our integer
106111
# Negative: how many digits are past our precision (we need to handle them in rounding)
107112
decimal_shift = conf.f + exp
108113
# Number of digits we need to accumulate including any trailigng zeros or digits past our precision
@@ -111,6 +116,8 @@ end
111116
if iszero(ndigits)
112117
# all digits are zero
113118
i = zero(T)
119+
# The backing_integer_digits == 0 case is handled in the `else` (it means
120+
# that all the digits are passed the precision but we might get `1` from rounding)
114121
elseif backing_integer_digits < 0
115122
# All digits are past our precision, no overflow possible
116123
i = zero(T)

0 commit comments

Comments
 (0)