diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..133fe6d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,11 @@ +FROM liveui/boost-base:1.0 + +WORKDIR /boost + +ADD scripts ./scripts +ADD Sources ./Sources +ADD Tests ./Tests +ADD Package.swift ./ + +RUN swift build --configuration debug + diff --git a/Sources/XMLCoding/Decoder/XMLDecoder.swift b/Sources/XMLCoding/Decoder/XMLDecoder.swift index 78ee8e9..dc5cd12 100644 --- a/Sources/XMLCoding/Decoder/XMLDecoder.swift +++ b/Sources/XMLCoding/Decoder/XMLDecoder.swift @@ -324,15 +324,18 @@ extension _XMLDecoder : SingleValueDecodingContainer { // MARK: - Concrete Value Representations extension _XMLDecoder { + /// Returns the given value unboxed from a container. internal func unbox(_ value: Any, as type: Bool.Type) throws -> Bool? { guard !(value is NSNull) else { return nil } - guard let value = value as? String else { return nil } + guard let value = value as? String else { + return nil + } - if value == "true" || value == "1" { + if value.lowercased() == "true" || value == "1" { return true - } else if value == "false" || value == "0" { + } else if value.lowercased() == "false" || value == "0" { return false } @@ -348,18 +351,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let int = number.intValue - guard NSNumber(value: int) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return int + return Int(exactly: value) } internal func unbox(_ value: Any, as type: Int8.Type) throws -> Int8? { @@ -371,18 +363,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let int8 = number.int8Value - guard NSNumber(value: int8) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return int8 + return Int8(exactly: value) } internal func unbox(_ value: Any, as type: Int16.Type) throws -> Int16? { @@ -394,18 +375,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let int16 = number.int16Value - guard NSNumber(value: int16) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return int16 + return Int16(exactly: value) } internal func unbox(_ value: Any, as type: Int32.Type) throws -> Int32? { @@ -417,18 +387,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let int32 = number.int32Value - guard NSNumber(value: int32) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return int32 + return Int32(exactly: value) } internal func unbox(_ value: Any, as type: Int64.Type) throws -> Int64? { @@ -440,18 +399,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let int64 = number.int64Value - guard NSNumber(value: int64) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return int64 + return Int64(exactly: value) } internal func unbox(_ value: Any, as type: UInt.Type) throws -> UInt? { @@ -463,18 +411,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let uint = number.uintValue - guard NSNumber(value: uint) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return uint + return UInt(exactly: value) } internal func unbox(_ value: Any, as type: UInt8.Type) throws -> UInt8? { @@ -486,18 +423,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let uint8 = number.uint8Value - guard NSNumber(value: uint8) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return uint8 + return UInt8(exactly: value) } internal func unbox(_ value: Any, as type: UInt16.Type) throws -> UInt16? { @@ -509,18 +435,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let uint16 = number.uint16Value - guard NSNumber(value: uint16) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return uint16 + return UInt16(exactly: value) } internal func unbox(_ value: Any, as type: UInt32.Type) throws -> UInt32? { @@ -532,18 +447,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let uint32 = number.uint32Value - guard NSNumber(value: uint32) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return uint32 + return UInt32(exactly: value) } internal func unbox(_ value: Any, as type: UInt64.Type) throws -> UInt64? { @@ -555,18 +459,7 @@ extension _XMLDecoder { throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let uint64 = number.uint64Value - guard NSNumber(value: uint64) == number else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number <\(number)> does not fit in \(type).")) - } - - return uint64 + return UInt64(exactly: value) } internal func unbox(_ value: Any, as type: Float.Type) throws -> Float? { @@ -574,30 +467,11 @@ extension _XMLDecoder { guard let string = value as? String else { return nil } - if let value = Double(string) { - let number = NSNumber(value: value) - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - let double = number.doubleValue - guard abs(double) <= Double(Float.greatestFiniteMagnitude) else { - throw DecodingError.dataCorrupted(DecodingError.Context(codingPath: self.codingPath, debugDescription: "Parsed XML number \(number) does not fit in \(type).")) - } - - return Float(double) - } else if case let .convertFromString(posInfString, negInfString, nanString) = self.options.nonConformingFloatDecodingStrategy { - if string == posInfString { - return Float.infinity - } else if string == negInfString { - return -Float.infinity - } else if string == nanString { - return Float.nan - } + guard let value = Float(string) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + return value } internal func unbox(_ value: Any, as type: Double.Type) throws -> Double? { @@ -605,24 +479,11 @@ extension _XMLDecoder { guard let string = value as? String else { return nil } - if let number = Decimal(string: string) as NSDecimalNumber? { - - guard number !== kCFBooleanTrue, number !== kCFBooleanFalse else { - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) - } - - return number.doubleValue - } else if case let .convertFromString(posInfString, negInfString, nanString) = self.options.nonConformingFloatDecodingStrategy { - if string == posInfString { - return Double.infinity - } else if string == negInfString { - return -Double.infinity - } else if string == nanString { - return Double.nan - } + guard let value = Double(string) else { + throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: string) } - throw DecodingError._typeMismatch(at: self.codingPath, expectation: type, reality: value) + return value } internal func unbox(_ value: Any, as type: String.Type) throws -> String? { diff --git a/Sources/XMLCoding/Encoder/XMLEncoder.swift b/Sources/XMLCoding/Encoder/XMLEncoder.swift index e4961af..2ab2861 100644 --- a/Sources/XMLCoding/Encoder/XMLEncoder.swift +++ b/Sources/XMLCoding/Encoder/XMLEncoder.swift @@ -852,17 +852,7 @@ extension _XMLEncoder { internal func box(_ value: Float) throws -> NSObject { if value.isInfinite || value.isNaN { - guard case let .convertToString(positiveInfinity: posInfString, negativeInfinity: negInfString, nan: nanString) = self.options.nonConformingFloatEncodingStrategy else { - throw EncodingError._invalidFloatingPointValue(value, at: codingPath) - } - - if value == Float.infinity { - return posInfString as NSObject - } else if value == -Float.infinity { - return negInfString as NSObject - } else { - return nanString as NSObject - } + throw EncodingError._invalidFloatingPointValue(value, at: codingPath) } else { return NSNumber(value: value) } @@ -870,17 +860,7 @@ extension _XMLEncoder { internal func box(_ value: Double) throws -> NSObject { if value.isInfinite || value.isNaN { - guard case let .convertToString(positiveInfinity: posInfString, negativeInfinity: negInfString, nan: nanString) = self.options.nonConformingFloatEncodingStrategy else { - throw EncodingError._invalidFloatingPointValue(value, at: codingPath) - } - - if value == Double.infinity { - return posInfString as NSObject - } else if value == -Double.infinity { - return negInfString as NSObject - } else { - return nanString as NSObject - } + throw EncodingError._invalidFloatingPointValue(value, at: codingPath) } else { return NSNumber(value: value) } @@ -897,12 +877,12 @@ extension _XMLEncoder { return NSNumber(value: value.timeIntervalSince1970 * 1000.0) case .iso8601: if #available(macOS 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) { - return _iso8601Formatter.string(from: value) as NSObject + return NSString(string: _iso8601Formatter.string(from: value)) } else { fatalError("ISO8601DateFormatter is unavailable on this platform.") } case .formatted(let formatter): - return formatter.string(from: value) as NSObject + return NSString(string: formatter.string(from: value)) case .custom(let closure): let depth = self.storage.count try closure(value, self) @@ -919,7 +899,7 @@ extension _XMLEncoder { try value.encode(to: self) return self.storage.popContainer() case .base64: - return value.base64EncodedString() as NSObject + return NSString(string: value.base64EncodedString()) case .custom(let closure): let depth = self.storage.count try closure(value, self)