Closed YDX-2147483647 closed 1 year ago
#unit("hz")
h
#unit("Hz")
zH
Results of #qty is similar.
#qty
I am using v0.4.2 and I've verified this issue on current main (https://github.com/ChHecker/unify/commit/d2ab4c363246a84e82a6a82e842c397f25716b32).
Don't use units-short: #unit("hertz") works as expected.
#unit("hertz")
H
z
Therefore the checks pass, and we try to parse z and fail. However, we don't #panic and let it go…
#panic
In #format-unit-short, remove the else if in the for loop… https://github.com/ChHecker/unify/blob/d2ab4c363246a84e82a6a82e842c397f25716b32/lib.typ#L230-L232 …and add the following immediately after the loop.
#format-unit-short
else if
for
if acc.len() > 0 { panic("invalid unit: " + quantity) }
This will make #unit("hz") into error: panicked with: "invalid unit: hz".
error: panicked with: "invalid unit: hz"
In the same for loop, add and unit == none in this if, because the prefix must appear before the unit: https://github.com/ChHecker/unify/blob/d2ab4c363246a84e82a6a82e842c397f25716b32/lib.typ#L223-L225
and unit == none
This will make #unit("Hz") into error: panicked with: "invalid unit: Hz".
error: panicked with: "invalid unit: Hz"
[!WARNING] This does NOT make #unit("Hz") ⇒ Hz, because #format-unit-short is eager — it consumes H as Henry first.
[!WARNING]
This does NOT make #unit("Hz") ⇒ Hz, because #format-unit-short is eager — it consumes H as Henry first.
Hz
I suggest a bigger refactor of the parsing logic in #format-unit-short…
quantity in units-short
unit = units-short.at(quantity)
quantitiy
panic
How do you think so? If you agree but are busy, I can try to work out a PR.
#unit("hz")
⇒h
(hecto ?)#unit("Hz")
⇒zH
(zepto Henry)Results of
#qty
is similar.I am using v0.4.2 and I've verified this issue on current main (https://github.com/ChHecker/unify/commit/d2ab4c363246a84e82a6a82e842c397f25716b32).
Workaround
Don't use units-short:
#unit("hertz")
works as expected.Reason
h
is a valid prefix (hecto or 100).H
is a valid unit (Henry).z
is a valid prefix (zepto or $10^{-21}$).Therefore the checks pass, and we try to parse
z
and fail. However, we don't#panic
and let it go…Possible Fix
In
#format-unit-short
, remove theelse if
in thefor
loop… https://github.com/ChHecker/unify/blob/d2ab4c363246a84e82a6a82e842c397f25716b32/lib.typ#L230-L232 …and add the following immediately after the loop.This will make
#unit("hz")
intoerror: panicked with: "invalid unit: hz"
.In the same
for
loop, addand unit == none
in this if, because the prefix must appear before the unit: https://github.com/ChHecker/unify/blob/d2ab4c363246a84e82a6a82e842c397f25716b32/lib.typ#L223-L225This will make
#unit("Hz")
intoerror: panicked with: "invalid unit: Hz"
.A Bigger Refactor?
I suggest a bigger refactor of the parsing logic in
#format-unit-short
…quantity in units-short
,unit = units-short.at(quantity)
, ✓.quantitiy
into two parts, if the first part is a prefix and the second is a unit, ✓.panic
.How do you think so? If you agree but are busy, I can try to work out a PR.