Fix float parsing

This commit is contained in:
Emery Hemingway 2023-12-23 22:39:24 +02:00
parent e43371da87
commit 8a70cd0987
2 changed files with 12 additions and 14 deletions

View File

@ -30,24 +30,22 @@ proc decodePreserves*(s: Stream; E = void): Preserve[E] =
result = decodePreserves(s, E)
result.embedded = true
of 0x87:
let n = s.readUint8()
var N: int
let n = int s.readUint8()
case n
of 4:
when system.cpuEndian == bigEndian:
result = Preserve[E](kind: pkFloat, float: s.readFloat32())
else:
result = Preserve[E](kind: pkFloat)
var be = s.readFloat32()
swapEndian32(result.float.addr, be.addr)
result = Preserve[E](kind: pkFloat)
var buf: uint32
N = s.readData(addr buf, sizeof(buf))
bigEndian32(addr result.float, addr buf)
of 8:
when system.cpuEndian == bigEndian:
result = Preserve[E](kind: pkDouble, double: s.readFloat64())
else:
result = Preserve[E](kind: pkDouble)
var be = s.readFloat64()
swapEndian64(result.double.addr, be.addr)
result = Preserve[E](kind: pkDouble)
var buf: uint64
N = s.readData(addr buf, sizeof(buf))
bigEndian64(addr result.double, addr buf)
else:
raise newException(IOError, "unhandled IEEE754 value of " & $n & " bytes")
if N != n: raise newException(IOError, "short read")
of 0xb0:
var n = int s.readVarint()
if n <= sizeof(int):

View File

@ -88,7 +88,7 @@ proc pushHexNibble[T](result: var T; c: char) =
of '0'..'9': T(ord(c) - ord('0'))
of 'a'..'f': T(ord(c) - ord('a') + 10)
of 'A'..'F': T(ord(c) - ord('A') + 10)
else: 0
else: return
result = (result shl 4) or n
proc parsePreserves*(text: string): Preserve[void] =