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

Introduce rounding option #171

Merged
merged 2 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/Parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ end
* `stripwhitespace=nothing`: if true, leading and trailing whitespace is stripped from string fields, note that for *quoted* strings however, whitespace is preserved within quotes (but ignored before/after quote characters). To also strip *within* quotes, see `stripquoted`
* `stripquoted=false`: if true, whitespace is also stripped within quoted strings. If true, `stripwhitespace` is also set to true.
* `groupmark=nothing`: optionally specify a single-byte character denoting the number grouping mark, this allows parsing of numbers that have, e.g., thousand separators (`1,000.00`).
* `rounding=RoundNearest`: optionally specify a rounding mode to use when parsing. No rounding means the result will be marked with `INEXACT` code if the value is not exactly representable in the target type.
"""
struct Options
flags::Flags
Expand All @@ -126,6 +127,7 @@ struct Options
falses::Union{Nothing, Vector{String}}
dateformat::Union{Nothing, Format}
groupmark::Union{Nothing,UInt8}
rounding::Union{Nothing,RoundingMode}
end

# backwards compat
Expand All @@ -141,10 +143,10 @@ end

const OPTIONS = Options(Flags(false, false, false, false, false, false, false, false, false), UInt8('.'),
Token(UInt8('"')), Token(UInt8('"')), UInt8('"'), Token[], Token(""), Token(""),
nothing, nothing, nothing, nothing)
nothing, nothing, nothing, nothing, nothing)
const XOPTIONS = Options(Flags(false, false, false, false, true, true, true, false, false), UInt8('.'),
Token(UInt8('"')), Token(UInt8('"')), UInt8('"'), Token[], Token(UInt8(',')), Token(""),
nothing, nothing, nothing, nothing)
nothing, nothing, nothing, nothing, nothing)

prepare!(x::Vector) = sort!(x, by=x->sizeof(x), rev=true)
asciival(c::Char) = isascii(c)
Expand Down Expand Up @@ -198,8 +200,9 @@ function Options(
debug::Bool=false,
stripwhitespace::Bool=false,
stripquoted::Bool=false,
groupmark::Union{Nothing,Char,UInt8}=nothing)

groupmark::Union{Nothing,Char,UInt8}=nothing,
rounding::Union{Nothing,RoundingMode}=nothing,
)
# backwards compat; users previously had to pass wh1/wh2 as non-wh to avoid stripping
if wh1 != UInt8(' ') || wh2 != UInt8('\t')
stripwhitespace = false
Expand Down Expand Up @@ -250,7 +253,7 @@ function Options(
end
df = dateformat === nothing ? nothing : dateformat isa String ? Format(dateformat) : dateformat isa Dates.DateFormat ? Format(dateformat) : dateformat
flags = Flags(spacedelim, tabdelim, stripquoted, stripwhitespace, quoted, checksentinel, checkdelim, ignorerepeated, ignoreemptylines)
return Options(flags, decimal, oq, cq, e, sent, delim, token(comment, "comment"), trues, falses, df, groupmark === nothing ? nothing : UInt8(groupmark))
return Options(flags, decimal, oq, cq, e, sent, delim, token(comment, "comment"), trues, falses, df, groupmark === nothing ? nothing : UInt8(groupmark), rounding)
end

function token(x::MaybeToken, arg)
Expand Down Expand Up @@ -287,7 +290,8 @@ Options(;
stripwhitespace::Bool=false,
stripquoted::Bool=false,
groupmark::Union{Nothing,Char,UInt8}=nothing,
) = Options(sentinel, wh1, wh2, openquotechar, closequotechar, escapechar, delim, decimal, trues, falses, dateformat, ignorerepeated, ignoreemptylines, comment, quoted, debug, stripwhitespace, stripquoted, groupmark)
rounding::Union{Nothing,RoundingMode}=nothing,
) = Options(sentinel, wh1, wh2, openquotechar, closequotechar, escapechar, delim, decimal, trues, falses, dateformat, ignorerepeated, ignoreemptylines, comment, quoted, debug, stripwhitespace, stripquoted, groupmark, rounding)

include("components.jl")

Expand Down
10 changes: 5 additions & 5 deletions src/floats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ end
code |= INVALID
x = f === nothing ? x : nothing
else
x, code = scale(T, FT, digits, -signed(frac), neg, code, ndigits, f)
x, code = scale(T, FT, digits, -signed(frac), neg, code, ndigits, f, options)
code |= OK | EOF
end
@goto done
Expand Down Expand Up @@ -414,7 +414,7 @@ end
x = f === nothing ? x : nothing
@goto done
else
x, code = scale(T, FT, digits, -signed(frac), neg, code, ndigits, f)
x, code = scale(T, FT, digits, -signed(frac), neg, code, ndigits, f, options)
end
else
x = handlef(ifelse(neg, -T(digits), T(digits)), f)
Expand Down Expand Up @@ -445,7 +445,7 @@ end
code |= INVALID
x = f === nothing ? x : nothing
else
x, code = scale(T, FT, digits, ee, neg, code, ndigits, f)
x, code = scale(T, FT, digits, ee, neg, code, ndigits, f, options)
code |= OK | EOF
end
@goto done
Expand All @@ -458,7 +458,7 @@ end
code |= INVALID
x = f === nothing ? x : nothing
else
x, code = scale(T, FT, digits, ifelse(negexp, -signed(exp), signed(exp)) - signed(frac), neg, code, ndigits, f)
x, code = scale(T, FT, digits, ifelse(negexp, -signed(exp), signed(exp)) - signed(frac), neg, code, ndigits, f, options)
code |= OK
end
@goto done
Expand Down Expand Up @@ -494,7 +494,7 @@ pow10(::Type{BigFloat}, e) = (@inbounds v = F64_SHORT_POWERS[e+1]; return v)
_unsigned(x::BigInt) = x
_unsigned(x) = unsigned(x)

@inline function scale(::Type{T}, FT::FloatType, v, exp, neg, code, ndigits, f::F) where {T, F}
@inline function scale(::Type{T}, FT::FloatType, v, exp, neg, code, ndigits, f::F, ::Options) where {T, F}
if T === Float64
return handlef(__scale(Float64, _unsigned(v), exp, neg), f), code
elseif T === Float32
Expand Down