Skip to content

Commit 11e62ef

Browse files
authored
Merge pull request #12 from Deduction42/DEV
Dev
2 parents d42a746 + 9fb761d commit 11e62ef

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

src/RegistryTools.jl

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,8 @@ function qparse(str::String, reg::AbstractDict{Symbol,U}) where {U<:AbstractUnit
211211
end
212212

213213
# Expression parsing (for dynamic and macros) ==============================================================
214-
function uparse_expr(str0::String, reg::AbstractDict{Symbol, U}) where U <: AbstractUnitLike
215-
str = _expr_preprocessing(str0)
216-
parsed = Meta.parse(str)
214+
function uparse_expr(str::String, reg::AbstractDict{Symbol, U}) where U <: AbstractUnitLike
215+
parsed = Meta.parse(_expr_preprocessing(str))
217216

218217
if !(parsed isa PARSE_CASES)
219218
throw(ArgumentError("Unexpected expression: String input \"$(str)\" was not parsed as $(PARSE_CASES)"))
@@ -223,9 +222,8 @@ function uparse_expr(str0::String, reg::AbstractDict{Symbol, U}) where U <: Abst
223222
end
224223
end
225224

226-
function qparse_expr(str0::String, reg::AbstractDict{Symbol, U}) where U <: AbstractUnitLike
227-
str = _expr_preprocessing(str0)
228-
parsed = Meta.parse(str)
225+
function qparse_expr(str::String, reg::AbstractDict{Symbol, U}) where U <: AbstractUnitLike
226+
parsed = Meta.parse(_expr_preprocessing(str))
229227

230228
if !(parsed isa PARSE_CASES)
231229
throw(ArgumentError("Unexpected expression: String input \"$(str)\" was not parsed as $(PARSE_CASES)"))
@@ -288,7 +286,12 @@ function change_symbol(u::U, s::Symbol) where U<:AbstractAffineUnits
288286
return constructorof(U)(scale=uscale(u), offset=uoffset(u), dims=dimension(u), symbol=s)
289287
end
290288

291-
_expr_preprocessing(str::String) = replace(str, " "=>"", "%"=>"percent")
289+
function _expr_preprocessing(str1::AbstractString)
290+
space2mult = r"([\%\w]) +([\%\w])" => s"\1*\2"
291+
str2 = replace(str1, space2mult) #Spaces between words are multiplications
292+
str3 = replace(str2, space2mult) #Spaces between words are multiplications
293+
return replace(str3, " "=>"", "%"=>"percent") #Eliminates extra spaces
294+
end
292295

293296
end
294297

test/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ end
445445
@test q"1.0" == 1.0u""
446446
@test qparse("1%") == 0.01u""
447447
@test qparse("1.0") == 1.0u""
448+
@test qparse("5 5kg") == 25u"kg"
448449

449450
#Test showerror
450451
testio = IOBuffer()

0 commit comments

Comments
 (0)