Skip to content

Commit

Permalink
Extend Base.tryparse (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
giordano authored Dec 28, 2021
1 parent b7b7c8d commit c79947c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
# History of Measurements.jl

## v2.7.0 (2021-??-??)
## v2.7.0 (2021-12-28)

### New features

* Support hashing of `Measurement` objects
([#103](https://github.com/JuliaPhysics/Measurements.jl/issues/103),
[#104](https://github.com/JuliaPhysics/Measurements.jl/pull/104)).
* New method `Base.tryparse(::Type{Measurement}, ::AbstractString)`
([#110](https://github.com/JuliaPhysics/Measurements.jl/issues/110),
[#112](https://github.com/JuliaPhysics/Measurements.jl/pull/112)).

### Deprecations

Expand Down
10 changes: 8 additions & 2 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ julia> measurement("-1234e-1")
"""
measurement(str::AbstractString) = parse(Measurement{Float64}, str)

function Base.parse(::Type{Measurement{T}}, str::S) where {T<:AbstractFloat, S<:AbstractString}
function Base.tryparse(::Type{Measurement{T}}, str::S) where {T<:AbstractFloat, S<:AbstractString}
m = match(rxp_error_with_parentheses, str)
if m !== nothing # "123(45)e6"
val_str::S, val_dec, err_str::S, err_dec_str, expn = m.captures
Expand All @@ -105,7 +105,7 @@ function Base.parse(::Type{Measurement{T}}, str::S) where {T<:AbstractFloat, S<:
val_str, err_str, val_dec, expn =
m.captures[1], "0", nothing, nothing
else
throw(ArgumentError("cannot parse $(repr(str)) as Measurement{$T}"))
return nothing
end
end
end
Expand All @@ -127,3 +127,9 @@ function Base.parse(::Type{Measurement{T}}, str::S) where {T<:AbstractFloat, S<:
end
return measurement(val, err)
end

function Base.parse(::Type{Measurement{T}}, str::S) where {T<:AbstractFloat, S<:AbstractString}
out = tryparse(Measurement{T}, str)
out === nothing && throw(ArgumentError("cannot parse $(repr(str)) as Measurement{$T}"))
return out
end

2 comments on commit c79947c

@giordano
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/51327

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.7.0 -m "<description of version>" c79947cfe207a62afbee0415cc0db33bf3b3fc4c
git push origin v2.7.0

Please sign in to comment.