Skip to content

Commit

Permalink
raise an error if bad unit prefix encountered, closes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
Vindaar committed Mar 27, 2024
1 parent e657bf3 commit 1cb94af
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/unchained/parse_units.nim
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ proc parsePrefixAndUnit(tab: UnitTable, x: string, start, stop: int):
# first char must be short prefix & second a short unit, e.g. `mN`
result.prefix = parseSiPrefixShort(x.runeAt(start))
result.unit = tab.getShort($x[stop-1])
if result.prefix == siIdentity:
error("The prefix `" & $x.runeAt(start) & "` of the unit `" & x & "` is not a valid prefix!")
else:
# try any unit
var unitOpt = tab.tryLookupUnit(x[start ..< stop])
Expand All @@ -61,6 +63,8 @@ proc parsePrefixAndUnit(tab: UnitTable, x: string, start, stop: int):
if unitOpt.isSome:
result.unit = unitOpt.get
result.prefix = parseSiPrefixShort(x.runeAt(start)) # in this case prefix must be short
if result.prefix == siIdentity:
error("The prefix `" & $x.runeAt(start) & "` of the unit `" & x & "` is not a valid prefix!")
else:
# must be long + long, e.g. `KiloGram`
# must have prefix, thus parse until upper, that defines prefix & unit
Expand All @@ -70,6 +74,8 @@ proc parsePrefixAndUnit(tab: UnitTable, x: string, start, stop: int):
# look up long prefix
result.prefix = parseSiPrefixLong(x[start] & prefixStr)
result.unit = tab.lookupUnit(x[start + prefixNum + 1 ..< stop])
if result.prefix == siIdentity:
error("The prefix `" & $x[start] & prefixStr & "` of the unit `" & x & "` is not a valid prefix!")

template addUnit(): untyped {.dirty.} =
## Dirty template used in both parsing procedures (unicode & ascii)
Expand Down

0 comments on commit 1cb94af

Please sign in to comment.