Skip to content

Commit 1abbc8d

Browse files
committed
Add internal utils file
1 parent f8b6e98 commit 1abbc8d

File tree

4 files changed

+27
-14
lines changed

4 files changed

+27
-14
lines changed

src/DynamicQuantities.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export ustrip, dimension
77
export ulength, umass, utime, ucurrent, utemperature, uluminosity, uamount
88
export uparse, @u_str, sym_uparse, @us_str, uexpand, uconvert
99

10+
include("internal_utils.jl")
1011
include("fixed_rational.jl")
1112
include("types.jl")
1213
include("utils.jl")

src/internal_utils.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
This file contains utility functions that are not specific to the
3+
library, but are used throughout.
4+
"""
5+
6+
@generated function fieldnames_equal(::Type{T1}, ::Type{T2}) where {T1,T2}
7+
# Needs to be a generated function as might not be inlined
8+
return Base.fieldnames(T1) == Base.fieldnames(T2)
9+
end
10+
11+
const SUPERSCRIPT_MAPPING = ('', '¹', '²', '³', '', '', '', '', '', '')
12+
const INTCHARS = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
13+
14+
function to_superscript(s::AbstractString)
15+
chars = map(replace(s, "//" => "")) do c
16+
if c INTCHARS
17+
SUPERSCRIPT_MAPPING[parse(Int, c)+1]
18+
elseif c == '-'
19+
''
20+
else
21+
c
22+
end
23+
end
24+
return join(chars)
25+
end

src/types.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ end
9797
(::Type{D})(; kws...) where {R,D<:AbstractDimensions{R}} = constructor_of(D)(R; kws...)
9898
(::Type{D})(; kws...) where {D<:AbstractDimensions} = D(DEFAULT_DIM_BASE_TYPE; kws...)
9999
function (::Type{D})(d::D2) where {R,D<:AbstractDimensions{R},D2<:AbstractDimensions}
100-
issetequal(static_fieldnames(D), static_fieldnames(D2)) ||
100+
fieldnames_equal(D, D2) ||
101101
error("Cannot create a dimensions of `$(D)` from `$(D2)`. Please write a custom method for construction.")
102102
D((getproperty(d, k) for k in static_fieldnames(D))...)
103103
end

src/utils.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,6 @@ end
162162

163163
string_rational(x) = isinteger(x) ? string(round(Int, x)) : string(x)
164164
pretty_print_exponent(io::IO, x) = print(io, to_superscript(string_rational(x)))
165-
const SUPERSCRIPT_MAPPING = ('', '¹', '²', '³', '', '', '', '', '', '')
166-
const INTCHARS = ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')
167-
to_superscript(s::AbstractString) = join(
168-
map(replace(s, "//" => "")) do c
169-
if c INTCHARS
170-
SUPERSCRIPT_MAPPING[parse(Int, c)+1]
171-
elseif c == '-'
172-
''
173-
else
174-
c
175-
end
176-
end
177-
)
178165

179166
tryrationalize(::Type{R}, x::R) where {R} = x
180167
tryrationalize(::Type{R}, x::Union{Rational,Integer}) where {R} = convert(R, x)

0 commit comments

Comments
 (0)