Skip to content

Commit 9ed0985

Browse files
committed
Support Unicode in term symbols (#36)
1 parent be66c6c commit 9ed0985

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/terms.jl

+11-4
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,15 @@ See also: [`@T_str`](@ref)
6464
"""
6565
function Base.parse(::Type{Term}, s::AbstractString)
6666
m = match(r"([0-9]+)([A-Z]|\[[0-9/]+\])([oe ]{0,1})", s)
67-
isnothing(m) && throw(ArgumentError("Invalid term string $s"))
68-
L = lowercase(m[2])
67+
m2 = match(r"([¹²³⁴⁵⁶⁷⁸⁹⁰]+)([A-Z]|\[[0-9/]+\])([ᵒᵉ]{0,1})", s)
68+
Sstr,Lstr,Pstr = if !isnothing(m)
69+
m[1],m[2],m[3]
70+
elseif !isnothing(m2)
71+
from_superscript(m2[1]),m2[2],m2[3]
72+
else
73+
throw(ArgumentError("Invalid term string $s"))
74+
end
75+
L = lowercase(Lstr)
6976
L = if L[1] == '['
7077
L = strip(L, ['[',']'])
7178
if occursin("/", L)
@@ -80,8 +87,8 @@ function Base.parse(::Type{Term}, s::AbstractString)
8087
findfirst(L, spectroscopic)[1]-1
8188
end
8289
denominator(L) [1,2] || throw(ArgumentError("L must be integer or half-integer"))
83-
S = (parse(Int, m[1]) - 1)//2
84-
Term(L, S, m[3] == "o" ? p"odd" : p"even")
90+
S = (parse(Int, Sstr) - 1)//2
91+
Term(L, S, Pstr == "o" || Pstr == "" ? p"odd" : p"even")
8592
end
8693

8794
"""

test/terms.jl

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ using Test
2020
@test T"2[3/2]o" == Term(3//2, 1//2, p"odd")
2121
@test T"2Z" == Term(20, 1//2, p"even")
2222

23+
for T in [T"1S", T"1Se", T"1So", T"2So", T"4P", T"3D", T"3Do",
24+
T"1[54]", T"1[3/2]", T"2[3/2]o", T"2Z"]
25+
@test parse(Term, string(T)) == T
26+
end
27+
2328
@test_throws DomainError Term(HalfInteger(-1//2), HalfInteger(1//2), p"even")
2429
@test_throws DomainError Term(3//2, -1//2, p"odd")
2530
@test_throws DomainError Term(-2, 1//2, 1)

0 commit comments

Comments
 (0)