From 6e8c33d90b6d1dc105e2292b8690b02dbf2e227c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefanos=20Carlstr=C3=B6m?= Date: Wed, 24 Mar 2021 09:13:55 +0100 Subject: [PATCH] Support Unicode in term symbols (#36) --- src/terms.jl | 15 +++++++++++---- test/terms.jl | 5 +++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/src/terms.jl b/src/terms.jl index 768b056..06b53c8 100644 --- a/src/terms.jl +++ b/src/terms.jl @@ -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) @@ -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 """ diff --git a/test/terms.jl b/test/terms.jl index af8533e..5c09105 100644 --- a/test/terms.jl +++ b/test/terms.jl @@ -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)