Skip to content

Commit 4ea7604

Browse files
Replace Half with Float16 and fix linting warnings
1 parent 35ac557 commit 4ea7604

File tree

9 files changed

+181
-202
lines changed

9 files changed

+181
-202
lines changed

Package.swift

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

77
platforms: [
8-
.iOS("9.0"),
8+
.iOS(.v14),
99
.macOS(.v11),
10-
.tvOS("9.0"),
11-
.watchOS("2.0")
10+
.tvOS(.v14),
11+
.watchOS(.v7),
12+
.macCatalyst(.v14),
1213
],
1314

1415
products: [
@@ -22,5 +23,5 @@ let package = Package(
2223
.testTarget(name: "CBORCodingTests", dependencies: ["CBORCoding"])
2324
],
2425

25-
swiftLanguageVersions: [.version("4.2"), .version("5")]
26+
swiftLanguageVersions: [.v5]
2627
)

Sources/CBORCoding/CBOR.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ public enum CBOR {
321321

322322
/// The string composed by joining together all of the data chunks and interpreting
323323
/// it as a UTF8 encoded string.
324-
@inline(__always) public var stringValue: String? {
324+
@inline(__always)
325+
public var stringValue: String? {
325326
return stringValue(as: .utf8)
326327
}
327328

Sources/CBORCoding/CBORDecoder.swift

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
//
77

88
import Foundation
9-
#if canImport(Half)
10-
import Half
11-
#endif
129

1310
#if canImport(Combine)
1411
import Combine
@@ -288,34 +285,34 @@ internal class __CBORDecoder: Decoder, SingleValueDecodingContainer {
288285
}
289286

290287
fileprivate func decode<T: Decodable>(_ value: Any, as type: T.Type) throws -> T {
291-
let result: T
292-
293288
// swiftlint:disable force_cast
294289
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 {
290+
return value.decodedDataValue() as! T
291+
}
292+
if type == String.self, let value = value as? CBORDecodedString {
293+
return try value.decodedStringValue() as! T
294+
}
295+
if type == CBOR.NegativeUInt64.self {
296+
return try decode(value, as: CBOR.NegativeUInt64.self) as! T
297+
}
298+
if type == CBOR.Undefined.self {
301299
guard let decodedValue = value as? T else {
302300
throw DecodingError._typeMismatch(at: codingPath, expectation: type, reality: value)
303301
}
304302

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)
303+
return decodedValue
304+
}
305+
if let decodedValue = value as? T {
306+
return decodedValue
307+
}
308+
if type == Float16.self {
309+
return try decodeFloatingPoint(value, as: Float16.self) as! T
315310
}
316-
// swiftlint:enable force_cast
317311

318-
return result
312+
storage.push(container: value)
313+
defer { storage.popContainer() }
314+
// swiftlint:enable force_cast
315+
return try type.init(from: self)
319316
}
320317

321318
//
@@ -380,54 +377,39 @@ internal class __CBORDecoder: Decoder, SingleValueDecodingContainer {
380377
}
381378

382379
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
380+
if let half = value as? Float16 {
381+
guard !half.isNaN else {
382+
return half.isSignalingNaN ? .signalingNaN : .nan
397383
}
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
384+
guard let value = T(exactly: half) else {
385+
throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: codingPath, debugDescription: "Decoded CBOR number <\(half)> does not fit in \(type)."))
411386
}
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-
}
423387

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

430-
return floatingPoint
409+
return value
410+
411+
}
412+
throw DecodingError._typeMismatch(at: codingPath, expectation: type, reality: value)
431413
}
432414

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

Sources/CBORCoding/CBOREncoder.swift

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
//
77

88
import Foundation
9-
#if canImport(Half)
10-
import Half
11-
#endif
129

1310
#if canImport(Combine)
1411
import Combine
@@ -229,7 +226,7 @@ open class CBOREncoder {
229226
return Data(bytes)
230227
}
231228

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

@@ -714,60 +711,65 @@ internal class __CBOREncoder: CBOREncoderProtocol, SingleValueEncodingContainer
714711
}
715712

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

763-
if newContainerLength.contains(.includeSubcontainers) {
764-
result = try action()
765-
} else {
766-
result = try definiteLengthContainerContext(action)
767-
}
764+
guard self.storage.count > depth else { return nil }
765+
return self.storage.popContainer()
768766
}
769767

770-
return result
768+
if newContainerLength.contains(.includeSubcontainers) {
769+
return try action()
770+
} else {
771+
return try definiteLengthContainerContext(action)
772+
}
771773
}
772774

773775
// MARK: Encoder Protocol Requirements

Sources/CBORCoding/CBORParser.swift

Lines changed: 5 additions & 5 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 {
@@ -725,7 +725,7 @@ internal class CBORParser {
725725
if data.count >= 5 {
726726
let upper = UInt32(data[data.index(data.startIndex, offsetBy: 1)]) << 24 |
727727
UInt32(data[data.index(data.startIndex, offsetBy: 2)]) << 16
728-
let lower = UInt32(data[data.index(data.startIndex, offsetBy: 3)]) << 8 |
728+
let lower = UInt32(data[data.index(data.startIndex, offsetBy: 3)]) << 8 |
729729
UInt32(data[data.index(data.startIndex, offsetBy: 4)])
730730

731731
result = (T(exactly: upper | lower), 5)
@@ -746,7 +746,7 @@ internal class CBORParser {
746746
do {
747747
let lower1 = UInt64(data[data.index(data.startIndex, offsetBy: 5)]) << 24 |
748748
UInt64(data[data.index(data.startIndex, offsetBy: 6)]) << 16
749-
let lower2 = UInt64(data[data.index(data.startIndex, offsetBy: 7)]) << 8 |
749+
let lower2 = UInt64(data[data.index(data.startIndex, offsetBy: 7)]) << 8 |
750750
UInt64(data[data.index(data.startIndex, offsetBy: 8)])
751751

752752
lower = lower1 | lower2

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)