Skip to content

Commit

Permalink
Merge pull request #86 from rened/tryparse
Browse files Browse the repository at this point in the history
add tryparse
  • Loading branch information
jakebolewski committed May 12, 2015
2 parents 3328961 + 79a265d commit 7ea58c8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,19 @@ if VERSION < v"0.4.0-dev+656"
include("nullable.jl")
end

if VERSION < v"0.4.0-dev+3864"
function tryparse(T::Type{Float32}, s)
r = Array(T,1)
float32_isvalid(s, r) ? Nullable(r[1]) : Nullable{Float32}()
end

function tryparse(T::Type{Float64}, s)
r = Array(T,1)
float64_isvalid(s, r) ? Nullable(r[1]) : Nullable{Float64}()
end
export tryparse
end

if VERSION < v"0.4.0-dev+3844"
@eval module Libc
const FILE = Base.CFILE
Expand Down
6 changes: 6 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,12 @@ end
@test parse(Float64, "222") == 222.0
@test parse(Float32, "1.1") == convert(Float32, 1.1)
@test parse(BigFloat, "1.125") == convert(BigFloat, 1.125)
@test isa(tryparse(Float32, "1.1"), Nullable)
@test get(tryparse(Float32, "1.1")) == 1.1f0
@test isa(tryparse(Float32, "a"), Nullable{Float32})
@test isa(tryparse(Float64, "1.1"), Nullable)
@test get(tryparse(Float64, "1.1")) == 1.1
@test isa(tryparse(Float64, "a"), Nullable{Float64})

# Make sure exports from Libc and Libdl are defined
for x in [:strftime,:systemsleep,:getpid,:FILE,:malloc,:MS_SYNC,:munmap,:flush_cstdio,:realloc,:strptime,:Libc,:errno,:msync,:TmStruct,:calloc,:MS_INVALIDATE,:MS_ASYNC,:time,:strerror,:gethostname,:free,:mmap]
Expand Down

0 comments on commit 7ea58c8

Please sign in to comment.