Skip to content

Commit

Permalink
change the Unitful.string to Base.string
Browse files Browse the repository at this point in the history
  • Loading branch information
michikawa07 committed Oct 9, 2024
1 parent 1f3387a commit 5f3c39f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "UnitfulParsableString"
uuid = "06c00241-927a-4d5b-bb5e-6b5a2ada3567"
authors = ["michikawa07 <[email protected]> and contributors"]
version = "0.1.6"
version = "0.1.7"

[deps]
Memoization = "6fafb56a-5788-4b4e-91ca-c0cea6611c73"
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# UnitfulParsableString [![Build Status](https://github.com/michikawa07/UnitfulParsableString.jl/actions/workflows/CI.yml/badge.svg?branch=main)](https://github.com/michikawa07/UnitfulParsableString.jl/actions/workflows/CI.yml?query=branch%3Amain) [![Stable](https://img.shields.io/badge/docs-stable-blue.svg)](https://michikawa07.github.io/UnitfulParsableString.jl/stable/) [![Dev](https://img.shields.io/badge/docs-dev-blue.svg)](https://michikawa07.github.io/UnitfulParsableString.jl/dev/) [![Build Status](https://travis-ci.com/michikawa07/UnitfulParsableString.jl.svg?branch=main)](https://travis-ci.com/michikawa07/UnitfulParsableString.jl) [![UnitfulParsableString Downloads](https://shields.io/endpoint?url=https://pkgs.genieframework.com/api/v1/badge/UnitfulParsableString)](https://pkgs.genieframework.com?packages=UnitfulParsableString)

`UnitfulParsableString.jl` expand [Unitful.jl](https://github.com/PainterQubits/Unitful.jl) to add the method `Unitful.string` which convert `Quantity` (or some type) to parsable `String`.
`UnitfulParsableString.jl` expand [Unitful.jl](https://github.com/PainterQubits/Unitful.jl) to add the multiple dispatch of `Base.string` which convert `Quantity` (or some type) to parsable `String`.

```julia
julia> using Unitful
Expand All @@ -23,7 +23,7 @@ julia> string(1.0u"m*s") |> Unitful.uparse
## Expression of Unit

```julia
Unitful.string(unit::Units)
string(unit::Units)
```

Values of `Unitful.Units` subtypes are converted to `string` that julia can parse as following rules.
Expand Down Expand Up @@ -68,7 +68,7 @@ julia> string(u"m^(1//3)") # 1//3 != 1/3
## Expression of Quantity

```julia
Unitful.string(x::Quantity)
string(x::Quantity)
```

Values of `Unitful.Quantity` subtypes to `string` that julia can parse as following rules.
Expand All @@ -86,7 +86,7 @@ However, if `typeof(x)` is a specific type, the process is lightened by multiple

The `has_unit_bracket(x)` returns false if the `unit(x)` consists of single type unit, and true if it consists of multi type units.

Note: see `Unitful.string(x::Unitlike)` about the string expression of unit.
Note: see `string(x::Unitlike)` about the string expression of unit.
Note: if `unit(x)` == NoUnits, this method output only `string(x.val)`.

At the case of `Int` the bracket is absence and, at the case of the unit consists of only `s` the bracket is absence.
Expand Down
30 changes: 16 additions & 14 deletions src/UnitfulParsableString.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ using Unitful: # need for print
prefix, abbr, power, ustrcheck_bool
using Memoization

import Base: string

has_value_bracket(::Union{Gain, Level}) = true
has_value_bracket(::Union{Complex, Rational}) = true
has_value_bracket(::Union{BigInt,Int128,Int16,Int32,Int64,Int8}) = false
Expand Down Expand Up @@ -70,7 +72,7 @@ function symbol(unit::Units{U, D, A}) where {U, D, A<:Affine}
end

"""
Unitful.string(unit::Units)
string(unit::Units)
This function provied by `UnitfulParsableString` converts the value of `Unitful.Units` subtypes to `string` that julia can parse.
Expand Down Expand Up @@ -119,7 +121,7 @@ julia> string(u"m^(1//3)" # 1//3 != 1/3
"m^(1//3)"
```
"""
function Unitful.string(u::Units)
function string(u::Units)
unit_list = sortedunits(u)
is_div_note = any(power(u)>0 for u in unit_list) && all(power(u).den==1 for u in unit_list)
str = ""
Expand All @@ -140,13 +142,13 @@ function Unitful.string(u::Units)
is_u_str_expression() ? string("u\"", str, "\"") : str
end

function Unitful.string(u::Units{U, D, A}) where {U, D, A<:Affine}
function string(u::Units{U, D, A}) where {U, D, A<:Affine}
str = string(symbol(u))
is_u_str_expression() ? string("u\"", str, "\"") : str
end

"""
Unitful.string(x::Quantity)
string(x::Quantity)
This function provied by `UnitfulParsableString` converts the value of `Unitful.Quantity` subtypes to `string` that julia can parse.
Expand All @@ -158,7 +160,7 @@ The presence or absence of each bracket is determined by the return values of th
if `has_value_bracket(x) && has_unit_bracket(x) == true`, the operator "\\*" is inserted.
Note: see `Unitful.string(x::Units)` about the string expression of unit
Note: see `string(x::Units)` about the string expression of unit
The generated strings are checked to see if they can be parsed in `Unitful` and `Unitful.unitmodules`, and a warning is issued if an unparsable string is generated.
Expand All @@ -178,7 +180,7 @@ julia> string((1+2im)u"m/s") # (1+2im)u"m/s" -> (1 + 2im) m s⁻¹
"(1 + 2im)*(m/s)"
```
"""
function Unitful.string(x::Quantity)
function string(x::Quantity)
v = string(x.val)
u = string(unit(x))
unit(x) == NoUnits && return v
Expand All @@ -189,9 +191,9 @@ function Unitful.string(x::Quantity)
end

"""
Unitful.string(r::StepRange{T}) where T<:Quantity
string(r::StepRange{T}) where T<:Quantity
"""
function Unitful.string(r::StepRange{T}) where T<:Quantity
function string(r::StepRange{T}) where T<:Quantity
a,s,b = first(r), step(r), last(r)
U,u = unit(a), string(unit(a))
rng = ustrip(U, s)==1 ? repr(ustrip(U, a):ustrip(U, b)) :
Expand All @@ -201,9 +203,9 @@ function Unitful.string(r::StepRange{T}) where T<:Quantity
end

"""
Unitful.string(r::StepRangeLen{T}) where T<:Quantity
string(r::StepRangeLen{T}) where T<:Quantity
"""
function Unitful.string(r::StepRangeLen{T}) where T<:Quantity
function string(r::StepRangeLen{T}) where T<:Quantity
a,s,b = first(r), step(r), last(r)
U,u = unit(a), string(unit(a))
rng = repr(ustrip(U, a):ustrip(U, s):ustrip(U, b))
Expand All @@ -212,16 +214,16 @@ function Unitful.string(r::StepRangeLen{T}) where T<:Quantity
end

"""
Unitful.string(x::typeof(NoUnits))
string(x::typeof(NoUnits))
"""
function Unitful.string(x::typeof(NoUnits))
function string(x::typeof(NoUnits))
"NoUnits"
end

"""
Unitful.string(uarr::AbstractArray{T})
string(uarr::AbstractArray{T})
"""
function Unitful.string(uarr::AbstractArray{T}; karg...) where T<:Quantity
function string(uarr::AbstractArray{T}; karg...) where T<:Quantity
uarr_sym = map( _->gensym(), uarr )
str = string(uarr_sym)
for (u,sym) in zip(uarr,uarr_sym)
Expand Down

3 comments on commit 5f3c39f

@michikawa07
Copy link
Owner 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/116878

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

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 v0.1.7 -m "<description of version>" 5f3c39f195475e41a6639caea41779aaffb17e62
git push origin v0.1.7

@michikawa07
Copy link
Owner Author

Choose a reason for hiding this comment

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

#12

Please sign in to comment.