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

Implement tryparse() #123

Merged
merged 1 commit into from
May 13, 2020
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
9 changes: 9 additions & 0 deletions src/DecFP.jl
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ for w in (32,64,128)
return @xchk(x, nothing)
end

function Base.tryparse(::Type{$BID}, s::AbstractString)
x = _parse($BID, s)
if isnan(x) && !isnanstr(s)
@xchk(x, nothing)
return nothing
end
return @xchk(x, nothing)
end

$BID(x::AbstractString) = parse($BID, x)

function $BID(x::AbstractString, mode::RoundingMode)
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ for T in (Dec32, Dec64, Dec128)
@test isinf(parse(T, "inf")::T) && !isfinite(parse(T, "inf")::T)
@test parse(T,"inf")::T == typemax(T)::T
@test parse(T,"-inf")::T == typemin(T)::T
@test_throws ArgumentError parse(T, "1.0.1")
@test_throws ArgumentError parse(T, "NaNny")
@test tryparse(T, "1.5")::T == 1.5
@test isnan(tryparse(T, "NaN"))
@test tryparse(T, "NaNny") === nothing
@test tryparse(T, "1.0.1") === nothing

@test parse(T, "0.1")::T == T(1//10)
@test T("0.1")::T == T(1//10)
Expand Down