Skip to content

Commit

Permalink
Strip (s), (l), (g), (aq)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlatanvasovic committed Aug 10, 2020
1 parent 0c7b096 commit 6c75d99
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "ChemEquations"
uuid = "75e6b969-eb85-49eb-b654-b2cb515e226f"
authors = ["zdroid <zlatanvasovic@gmail.com> and contributors"]
version = "0.2.0"
version = "0.2.1"

[deps]
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand Down
6 changes: 5 additions & 1 deletion docs/src/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ julia> balance(equation)
2 Fe + 3 Cl2 = 2 FeCl3
```

Parsing the input is insensitive to whitespace, so you don't have to be pedantic if you don't want to.
Parsing the input is insensitive to whitespace and to state symbols (`(s)`, `(l)`, `(g)`, `(aq)`),
so you don't have to be pedantic if you don't want to.

```julia-repl
julia> balance(ce"KMnO4+ HCl = KCl+MnCl2 +H2O + Cl2")
2 KMnO4 + 16 HCl = 2 KCl + 2 MnCl2 + 8 H2O + 5 Cl2
julia> balance(ce"Zn(s) + O2(g) = ZnO(s)")
2 Zn + O2 = 2 ZnO
```

Parentheses (`()`), compounds written with `*` and electrical charges are all supported.
Expand Down
7 changes: 5 additions & 2 deletions src/balance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ Returns an array in which each column represents a solution.
balancematrix(equation::ChemEquation) = _balancematrix(equation)

"""
Same as [`balancematrix(::ChemEquation)`](@ref), but with special format options.
Same as [`balancematrix(::ChemEquation)`](@ref),
but for a chemical equation with integer coefficients.
By default, the solutions of integer matrices are displayed as integers.
If `fractions` is true, they will be displayed as rational fractions instead.
"""
Expand All @@ -62,7 +64,6 @@ end

"""
Balances the coefficients of a chemical equation.
If the equation cannot be balanced, an error is thrown.
!!! info
Expand All @@ -75,11 +76,13 @@ julia> balance(ce"Fe + Cl2 = FeCl3")
julia> balance(ChemEquation{Rational}("H2 + Cl2 = HCl"))
1//2 H2 + 1//2 Cl2 = HCl
```
"""
balance(equation::ChemEquation) = _balance(equation)

"""
Balances the coefficients of a chemical equation with integer coefficients.
The minimal integer solution is displayed by default.
If `fractions` is true, they solution will be displayed as rational fractions instead.
Expand Down
28 changes: 11 additions & 17 deletions src/chemequation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,9 @@ struct ChemEquation{T<:Real}
tuples::Vector{CompoundTuple{T}}
end

"""
Constructs a chemical equation of specified type from `str`.
Follows the same rules as [`ChemEquation(::AbstractString)`](@ref).
# Examples
```jldoctest
julia> ChemEquation{Rational}("1//2 H2 → H")
1//2 H2 = H
julia> ChemEquation{Float64}("0.5 H2 + 0.5 Cl2 = HCl")
0.5 H2 + 0.5 Cl2 = HCl
```
"""
ChemEquation{T}(str::AbstractString) where T<:Real = ChemEquation(_compoundtuples(str, T))

"""
Constructs a chemical equation from the given string.
If no `{Type}` is provided, it defaults to `Int`.
Parsing is insensitive to whitespace.
Any character in [`EQUALCHARS`](@ref) separates the equation into two sides,
Expand All @@ -52,13 +38,21 @@ C2H4O2 + Na = H2 + C2H3O2Na
julia> ChemEquation("⏣H + Cl2 = ⏣Cl + HCl")
⏣H + Cl2 = ⏣Cl + HCl
julia> ChemEquation{Rational}("1//2 H2 → H")
1//2 H2 = H
julia> ChemEquation{Float64}("0.5 H2 + 0.5 Cl2 = HCl")
0.5 H2 + 0.5 Cl2 = HCl
```
```
"""
ChemEquation{T}(str::AbstractString) where T<:Real = ChemEquation(_compoundtuples(str, T))
ChemEquation(str::AbstractString) = ChemEquation{Int}(str)

"Extracts compound tuples from equation's string."
function _compoundtuples(str::AbstractString, T::Type)
strs = replace(str, ' ' => "") |>
strs = replace(str, [' ', '_'] => "") |>
x -> split(x, EQUALCHARS) |>
x -> split.(x, PLUSREGEX)
splitindex = length(strs[1])
Expand Down Expand Up @@ -112,7 +106,7 @@ Creates a string to represent the chemical equation.
All compounds are displayed with [`Base.string(::Compound)`](@ref),
in the order in which they were originally given,
with coefficients equal to 1 not displayed.
'=' and '+' are used as separators, with spaces inserted for easier reading.
`'='` and `'+'` are used as separators, with spaces inserted for easier reading.
# Examples
```jldoctest
Expand Down
8 changes: 5 additions & 3 deletions src/compound.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ ends with a lowercase unicode letter or a unicode symbol.
An element can also begin with a symbol if
the symbol is the first character (e.g. `"⬡H"`).
Parsing is insensitive to whitespace and underscores (`_`).
Parsing is insensitive to whitespace and underscores (`_`),
but also to state symbols (`(s)`, `(l)`, `(g)`, `(aq)`).
Special parsing is implemented for:
- parens (e.g. `"(CH3COO)2Mg"`)
- compounds with `"*"` (e.g. `"CuSO4 * 5H2O"`)
Expand All @@ -35,7 +36,7 @@ It is automatically deduced for electron (`"e"`).
# Examples
```jldoctest
julia> Compound("H2O")
julia> Compound("H2O(s)")
H2O
julia> Compound("H3O{+}")
Expand All @@ -53,6 +54,7 @@ julia> Compound("⬡Cl")
"""
function Compound(str::AbstractString)
str = replace(str, [' ', '_'] => "")
str = replace(str, r"\((s|l|g|aq)\)$" => "") # remove state symbols
Compound(_elementtuples(str), _charge(str))
end

Expand Down Expand Up @@ -153,7 +155,7 @@ Creates a string to represent the compound.
All elements are displayed only once (e.g. `"H2O"` and not `"HOH"`),
in the order in which they were originally given (e.g. `"MgO2H2"` from `cc"Mg(OH)2"`),
with coefficients equal to 1 not displayed (e.g. `"O"` and not `"O1"`).
with coefficients equal to 1 not displayed (e.g. `"H"` and not `"H1"`).
# Examples
```jldoctest
Expand Down
1 change: 1 addition & 0 deletions test/balance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ end
"5 PhCH3 + 6 KMnO4 + 9 H2SO4 = 5 PhCO2H + 3 K2SO4 + 6 MnSO4 + 14 H2O",
"CuSO4*5H2O = CuSO4 + H2O" =>
"CuSO4*5H2O = CuSO4 + 5 H2O",
"Zn(s) + O2(g) = ZnO(s)" => "2 Zn + O2 = 2 ZnO"
]
for equation equations
@test balance(ChemEquation(equation[1])) == ChemEquation(equation[2])
Expand Down
4 changes: 4 additions & 0 deletions test/compound.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ end
@test cc"H2" == cc"H2"
@test cc"{-}" == cc"{-1}"
@test cc"CH2(OH)2(CH)" == cc"O2H5C2"

@test cc"H H" == cc"H2"
@test cc"H_1C_1O_1" == cc"HCO"
@test cc"Mg(OH)2(s)" == cc"Mg(OH)2(aq)"
end

@testset "string" begin
Expand Down

2 comments on commit 6c75d99

@zlatanvasovic
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/19269

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.2.1 -m "<description of version>" 6c75d9979202bf683c41c4f6dd0a4a6ce7625d3c
git push origin v0.2.1

Please sign in to comment.