From 101abe3e46909a712fe0d7de93b6569bb04f7dc0 Mon Sep 17 00:00:00 2001 From: Damiaan Dufaux Date: Wed, 2 Aug 2017 22:15:07 +0200 Subject: [PATCH] Fix Float decoding bug --- MsgPack/Decoder.swift | 3 ++- Playground.playground/Contents.swift | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/MsgPack/Decoder.swift b/MsgPack/Decoder.swift index 48bb657..2dba09c 100644 --- a/MsgPack/Decoder.swift +++ b/MsgPack/Decoder.swift @@ -135,7 +135,8 @@ struct MsgPckSingleValueDecodingContainer: SingleValueDecodingContainer { guard decoder.storage[base] == FormatID.float32.rawValue else { throw Error.invalidFormat(decoder.storage[base]) } - return decoder.storage.read(at: base + 1) + let bitPattern: UInt32 = decoder.storage.bigEndianInteger(at: base + 1) + return .init(bitPattern: bitPattern) } func decode(_ type: Double.Type) throws -> Double { diff --git a/Playground.playground/Contents.swift b/Playground.playground/Contents.swift index b0cae1d..bef399e 100644 --- a/Playground.playground/Contents.swift +++ b/Playground.playground/Contents.swift @@ -40,5 +40,9 @@ for byte in encodedGraph { let decoder = Decoder() -let int8 = try encoder.encode(Double(-10)) -try decoder.decode(Double.self, from: int8) +func roundtrip(value: T) throws -> T { + return try decoder.decode(T.self, from: encoder.encode(value)) +} + +var x: UInt64? = nil +try roundtrip(value: false)