Skip to content

Commit

Permalink
Support Unicode in term symbols (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
jagot committed Mar 24, 2021
1 parent 70e15e9 commit 4bd11fb
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,15 @@ See also: [`@T_str`](@ref)
"""
function Base.parse(::Type{Term}, s::AbstractString)
m = match(r"([0-9]+)([A-Z]|\[[0-9/]+\])([oe ]{0,1})", s)
isnothing(m) && throw(ArgumentError("Invalid term string $s"))
L = lowercase(m[2])
m2 = match(r"([¹²³⁴⁵⁶⁷⁸⁹⁰]+)([A-Z]|\[[0-9/]+\])([ᵒᵉ]{0,1})", s)
Sstr,Lstr,Pstr = if !isnothing(m)
m[1],m[2],m[3]
elseif !isnothing(m2)
from_superscript(m2[1]),m2[2],m2[3]
else
throw(ArgumentError("Invalid term string $s"))
end
L = lowercase(Lstr)
L = if L[1] == '['
L = strip(L, ['[',']'])
if occursin("/", L)
Expand All @@ -80,8 +87,8 @@ function Base.parse(::Type{Term}, s::AbstractString)
findfirst(L, spectroscopic)[1]-1
end
denominator(L) [1,2] || throw(ArgumentError("L must be integer or half-integer"))
S = (parse(Int, m[1]) - 1)//2
Term(L, S, m[3] == "o" ? p"odd" : p"even")
S = (parse(Int, Sstr) - 1)//2
Term(L, S, Pstr == "o" || Pstr == "" ? p"odd" : p"even")
end

"""
Expand Down
5 changes: 5 additions & 0 deletions test/terms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ using Test
@test T"2[3/2]o" == Term(3//2, 1//2, p"odd")
@test T"2Z" == Term(20, 1//2, p"even")

for T in [T"1S", T"1Se", T"1So", T"2So", T"4P", T"3D", T"3Do",
T"1[54]", T"1[3/2]", T"2[3/2]o", T"2Z"]
@test parse(Term, string(T)) == T
end

@test_throws DomainError Term(HalfInteger(-1//2), HalfInteger(1//2), p"even")
@test_throws DomainError Term(3//2, -1//2, p"odd")
@test_throws DomainError Term(-2, 1//2, 1)
Expand Down

0 comments on commit 4bd11fb

Please sign in to comment.