Skip to content

Commit 021a020

Browse files
Replace Half with Float16 and fix linting warnings
1 parent da372df commit 021a020

File tree

8 files changed

+178
-193
lines changed

8 files changed

+178
-193
lines changed

Package.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ let package = Package(
55
name: "CBORCoding",
66

77
platforms: [
8-
.iOS("12.0"),
9-
.macOS("10.13"),
10-
.tvOS("12.0"),
11-
.watchOS("4.0")
8+
.iOS("14.0"),
9+
.macOS("11.0"),
10+
.tvOS("14.0"),
11+
.watchOS("7.0"),
1212
],
1313

1414
products: [
@@ -19,5 +19,7 @@ let package = Package(
1919
targets: [
2020
.target(name: "CBORCoding", dependencies: []),
2121
.testTarget(name: "CBORCodingTests", dependencies: ["CBORCoding"])
22-
]
22+
],
23+
24+
swiftLanguageVersions: [.v5]
2325
)

Sources/CBORCoding/CBORDecoder.swift

Lines changed: 47 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -288,34 +288,34 @@ internal class __CBORDecoder: Decoder, SingleValueDecodingContainer {
288288
}
289289

290290
fileprivate func decode<T: Decodable>(_ value: Any, as type: T.Type) throws -> T {
291-
let result: T
292-
293291
// swiftlint:disable force_cast
294292
if type == Data.self, let value = value as? CBORDecodedData {
295-
result = value.decodedDataValue() as! T
296-
} else if type == String.self, let value = value as? CBORDecodedString {
297-
result = try value.decodedStringValue() as! T
298-
} else if type == CBOR.NegativeUInt64.self {
299-
result = try decode(value, as: CBOR.NegativeUInt64.self) as! T
300-
} else if type == CBOR.Undefined.self {
293+
return value.decodedDataValue() as! T
294+
}
295+
if type == String.self, let value = value as? CBORDecodedString {
296+
return try value.decodedStringValue() as! T
297+
}
298+
if type == CBOR.NegativeUInt64.self {
299+
return try decode(value, as: CBOR.NegativeUInt64.self) as! T
300+
}
301+
if type == CBOR.Undefined.self {
301302
guard let decodedValue = value as? T else {
302303
throw DecodingError._typeMismatch(at: codingPath, expectation: type, reality: value)
303304
}
304305

305-
result = decodedValue
306-
} else if let decodedValue = value as? T {
307-
result = decodedValue
308-
} else if type == Half.self {
309-
result = try decodeFloatingPoint(value, as: Half.self) as! T
310-
} else {
311-
storage.push(container: value)
312-
defer { storage.popContainer() }
313-
314-
result = try type.init(from: self)
306+
return decodedValue
307+
}
308+
if let decodedValue = value as? T {
309+
return decodedValue
310+
}
311+
if type == Float16.self {
312+
return try decodeFloatingPoint(value, as: Float16.self) as! T
315313
}
316-
// swiftlint:enable force_cast
317314

318-
return result
315+
storage.push(container: value)
316+
defer { storage.popContainer() }
317+
// swiftlint:enable force_cast
318+
return try type.init(from: self)
319319
}
320320

321321
//
@@ -380,54 +380,39 @@ internal class __CBORDecoder: Decoder, SingleValueDecodingContainer {
380380
}
381381

382382
fileprivate func decodeFloatingPoint<T>(_ value: Any, as type: T.Type) throws -> T where T: BinaryFloatingPoint {
383-
let floatingPoint: T
384-
if let half = value as? Half {
385-
if half.isNaN {
386-
if half.isSignalingNaN {
387-
floatingPoint = .signalingNaN
388-
} else {
389-
floatingPoint = .nan
390-
}
391-
} else {
392-
guard let value = T(exactly: half) else {
393-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(half)> does not fit in \(type)."))
394-
}
395-
396-
floatingPoint = value
383+
if let half = value as? Float16 {
384+
guard !half.isNaN else {
385+
return half.isSignalingNaN ? .signalingNaN : .nan
397386
}
398-
} else if let float = value as? Float {
399-
if float.isNaN {
400-
if float.isSignalingNaN {
401-
floatingPoint = .signalingNaN
402-
} else {
403-
floatingPoint = .nan
404-
}
405-
} else {
406-
guard let value = T(exactly: float) else {
407-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(float)> does not fit in \(type)."))
408-
}
409-
410-
floatingPoint = value
387+
guard let value = T(exactly: half) else {
388+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(half)> does not fit in \(type)."))
411389
}
412-
} else if let double = value as? Double {
413-
if double.isNaN {
414-
if double.isSignalingNaN {
415-
floatingPoint = .signalingNaN
416-
} else {
417-
floatingPoint = .nan
418-
}
419-
} else {
420-
guard let value = T(exactly: double) else {
421-
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(double)> does not fit in \(type)."))
422-
}
423390

424-
floatingPoint = value
391+
return value
392+
}
393+
394+
if let float = value as? Float {
395+
guard !float.isNaN else {
396+
return float.isSignalingNaN ? .signalingNaN : .nan
425397
}
426-
} else {
427-
throw DecodingError._typeMismatch(at: codingPath, expectation: type, reality: value)
398+
guard let value = T(exactly: float) else {
399+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(float)> does not fit in \(type)."))
400+
}
401+
402+
return value
428403
}
404+
if let double = value as? Double {
405+
guard !double.isNaN else {
406+
return double.isSignalingNaN ? .signalingNaN : .nan
407+
}
408+
guard let value = T(exactly: double) else {
409+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(double)> does not fit in \(type)."))
410+
}
429411

430-
return floatingPoint
412+
return value
413+
414+
}
415+
throw DecodingError._typeMismatch(at: codingPath, expectation: type, reality: value)
431416
}
432417

433418
fileprivate func decode(_ value: Any, as type: CBOR.NegativeUInt64.Type) throws -> CBOR.NegativeUInt64 {

Sources/CBORCoding/CBOREncoder.swift

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ open class CBOREncoder {
229229
return Data(bytes)
230230
}
231231

232-
internal static func encode(_ value: Half) -> Data {
232+
internal static func encode(_ value: Float16) -> Data {
233233
var half = value
234234
let data = Data(bytes: &half, count: MemoryLayout.size(ofValue: half))
235235

@@ -710,60 +710,65 @@ internal class __CBOREncoder: CBOREncoderProtocol, SingleValueEncodingContainer
710710
}
711711

712712
fileprivate func encodeValueToCBOR(_ value: Encodable) throws -> Any? {
713-
let result: Any?
714-
715713
if let encoded = value as? CBOR.CBOREncoded {
716-
result = encoded.encodedData
717-
} else if let date = value as? Date {
718-
result = CBOREncoder.encode(date, using: options.dateEncodingStrategy)
719-
} else if let data = value as? Data {
720-
result = CBOREncoder.encode(data)
721-
} else if let url = value as? URL {
714+
return encoded.encodedData
715+
}
716+
if let date = value as? Date {
717+
return CBOREncoder.encode(date, using: options.dateEncodingStrategy)
718+
}
719+
if let data = value as? Data {
720+
return CBOREncoder.encode(data)
721+
}
722+
if let url = value as? URL {
722723
// swiftlint:disable force_try
723-
result = try! CBOREncoder.encode(url, forTag: .uri).encodedData
724+
return try! CBOREncoder.encode(url, forTag: .uri).encodedData
724725
// swiftlint:enable force_try
725-
} else if value is NSNull || value is CBOR.Null {
726-
result = CBOREncoder.encodeNil()
727-
} else if value is CBOR.Undefined {
728-
result = CBOREncoder.encodeUndefined()
729-
} else if let dict = value as? [String: Encodable] {
730-
result = try encode(dict)
731-
} else if let dict = value as? [Int: Encodable] {
732-
result = try encode(dict)
733-
} else if let data = value as? CBOR.IndefiniteLengthData {
734-
result = try encode(data)
735-
} else if let string = value as? CBOR.IndefiniteLengthString {
736-
result = try encode(string)
737-
} else if let value = value as? CBOR.NegativeUInt64 {
738-
result = CBOREncoder.encode(value)
739-
} else if let value = value as? Half {
740-
result = CBOREncoder.encode(value)
741-
} else {
742-
let action: () throws -> Any? = {
743-
let depth = self.storage.count
744-
745-
do {
746-
try value.encode(to: self)
747-
} catch {
748-
if self.storage.count > depth {
749-
_ = self.storage.popContainer()
750-
}
751-
752-
throw error
726+
}
727+
if value is NSNull || value is CBOR.Null {
728+
return CBOREncoder.encodeNil()
729+
}
730+
if value is CBOR.Undefined {
731+
return CBOREncoder.encodeUndefined()
732+
}
733+
if let dict = value as? [String: Encodable] {
734+
return try encode(dict)
735+
}
736+
if let dict = value as? [Int: Encodable] {
737+
return try encode(dict)
738+
}
739+
if let data = value as? CBOR.IndefiniteLengthData {
740+
return try encode(data)
741+
}
742+
if let string = value as? CBOR.IndefiniteLengthString {
743+
return try encode(string)
744+
}
745+
if let value = value as? CBOR.NegativeUInt64 {
746+
return CBOREncoder.encode(value)
747+
}
748+
if let value = value as? Float16 {
749+
return CBOREncoder.encode(value)
750+
}
751+
let action: () throws -> Any? = {
752+
let depth = self.storage.count
753+
754+
do {
755+
try value.encode(to: self)
756+
} catch {
757+
if self.storage.count > depth {
758+
_ = self.storage.popContainer()
753759
}
754-
755-
guard self.storage.count > depth else { return nil }
756-
return self.storage.popContainer()
760+
throw error
757761
}
758762

759-
if newContainerLength.contains(.includeSubcontainers) {
760-
result = try action()
761-
} else {
762-
result = try definiteLengthContainerContext(action)
763-
}
763+
guard self.storage.count > depth else { return nil }
764+
return self.storage.popContainer()
764765
}
765766

766-
return result
767+
if newContainerLength.contains(.includeSubcontainers) {
768+
return try action()
769+
} else {
770+
return try definiteLengthContainerContext(action)
771+
}
767772
}
768773

769774
// MARK: Encoder Protocol Requirements

Sources/CBORCoding/CBORParser.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ internal class CBORParser {
225225
index += simple.decodedBytes
226226

227227
case 25:
228-
let half = try decode(Half.self, from: data[index...])
228+
let half = try decode(Float16.self, from: data[index...])
229229
try storage.append(half.value)
230230

231231
index += half.decodedBytes
@@ -626,10 +626,10 @@ internal class CBORParser {
626626
}
627627

628628
let result: (value: T, decodedBytes: Int)
629-
if header == CBOR.Bits.half.rawValue { // Half
629+
if header == CBOR.Bits.half.rawValue { // Float16
630630
if data.count >= 3 {
631631
// swiftlint:disable force_unwrapping
632-
let half = data[data.index(data.startIndex, offsetBy: 1) ..< data.index(data.startIndex, offsetBy: 3)].reversed().withUnsafeBytes { $0.bindMemory(to: Half.self).baseAddress!.pointee }
632+
let half = data[data.index(data.startIndex, offsetBy: 1) ..< data.index(data.startIndex, offsetBy: 3)].reversed().withUnsafeBytes { $0.bindMemory(to: Float16.self).baseAddress!.pointee }
633633
// swiftlint:enable force_unwrapping
634634

635635
if half.isNaN {

Sources/CBORCoding/Half.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import Foundation
22

3-
#if !canImport(Half)
4-
typealias Half = Float16
3+
#if canImport(Half) && swift(<5.3)
4+
import Half
5+
6+
typealias Float16 = Half
57
#endif

0 commit comments

Comments
 (0)