Skip to content

Commit f8ddb59

Browse files
committed
implement getNumberTypeID() without CoreFoundation
1 parent c25718b commit f8ddb59

File tree

1 file changed

+37
-16
lines changed

1 file changed

+37
-16
lines changed

Sources/KeyValueDecoder.swift

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
// SOFTWARE.
3030
//
3131

32-
import CoreFoundation
3332
import Foundation
3433

3534
/// Top level encoder that converts `[String: Any]`, `[Any]` or `Any` into `Codable` types.
@@ -779,22 +778,44 @@ extension NSNumber {
779778
}
780779
}
781780

782-
func getNumberTypeID() -> CFNumberType? {
783-
guard CFGetTypeID(self as CFTypeRef) == CFNumberGetTypeID() else { return nil }
784-
#if canImport(Darwin)
785-
return CFNumberGetType(self as CFNumber)
786-
#else
787-
guard type(of: self) != type(of: NSNumber(true)) else { return nil }
781+
enum NumberTypeID: Int, Sendable {
782+
case sInt8Type = 1
783+
case sInt16Type = 2
784+
case sInt32Type = 3
785+
case sInt64Type = 4
786+
case float32Type = 5
787+
case float64Type = 6
788+
case charType = 7
789+
case shortType = 8
790+
case intType = 9
791+
case longType = 10
792+
case longLongType = 11
793+
case floatType = 12
794+
case doubleType = 13
795+
case cfIndexType = 14
796+
case nsIntegerType = 15
797+
case cgFloatType = 16
798+
}
799+
800+
func getNumberTypeID() -> NumberTypeID? {
801+
// Prevent misclassifying Bool as charType
802+
if type(of: self) == type(of: NSNumber(value: true)) { return nil }
803+
788804
switch String(cString: objCType) {
789-
case "c": return CFNumberType.charType
790-
case "s": return CFNumberType.shortType
791-
case "i": return CFNumberType.intType
792-
case "q": return CFNumberType.longLongType
793-
case "d": return CFNumberType.doubleType
794-
case "f": return CFNumberType.floatType
795-
case "Q": return CFNumberType.longLongType
796-
default: return nil
805+
case "c": return .charType
806+
case "C": return .sInt8Type
807+
case "s": return .shortType
808+
case "S": return .sInt16Type
809+
case "i": return .intType
810+
case "I": return .sInt32Type
811+
case "l": return .longType
812+
case "L": return .sInt32Type
813+
case "q": return .longLongType
814+
case "Q": return .sInt64Type
815+
case "f": return .floatType
816+
case "d": return .doubleType
817+
case "B": return nil
818+
default: return nil
797819
}
798-
#endif
799820
}
800821
}

0 commit comments

Comments
 (0)