From 90fc23205c2187315e744b7c4c0288ed5d3c7bd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 9 Jun 2016 06:21:39 +0200 Subject: [PATCH] Cleanup --- GRDB/Core/Support/Foundation/NSNumber.swift | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/GRDB/Core/Support/Foundation/NSNumber.swift b/GRDB/Core/Support/Foundation/NSNumber.swift index 33a3981bb..cef713ca4 100644 --- a/GRDB/Core/Support/Foundation/NSNumber.swift +++ b/GRDB/Core/Support/Foundation/NSNumber.swift @@ -1,23 +1,19 @@ import Foundation private let integerRoundingBehavior = NSDecimalNumberHandler(roundingMode: .RoundPlain, scale: 0, raiseOnExactness: false, raiseOnOverflow: false, raiseOnUnderflow: false, raiseOnDivideByZero: false) -private let int64maxDecimal = NSDecimalNumber(mantissa: UInt64(Int64.max), exponent: 0, isNegative: false) -private let int64minDecimal = NSDecimalNumber(mantissa: UInt64(Int64.max) + 1, exponent: 0, isNegative: true) // Int64.min = -(Int64.max + 1) because of two's complement /// NSNumber adopts DatabaseValueConvertible extension NSNumber: DatabaseValueConvertible { /// Returns a value that can be stored in the database. public var databaseValue: DatabaseValue { - if let decimal = self as? NSDecimalNumber { - // Don't lose precision: store integers that fits in Int64 as Int64 - let rounded = decimal.decimalNumberByRoundingAccordingToBehavior(integerRoundingBehavior) - if (rounded == decimal) - && (decimal.compare(int64maxDecimal) != .OrderedDescending) // decimal <= Int64.max - && (decimal.compare(int64minDecimal) != .OrderedAscending) // decimal >= Int64.min - { - return longLongValue.databaseValue - } + // Don't lose precision: store integers that fits in Int64 as Int64 + if let decimal = self as? NSDecimalNumber + where decimal == decimal.decimalNumberByRoundingAccordingToBehavior(integerRoundingBehavior) // integer + && decimal.compare(NSDecimalNumber(longLong: Int64.max)) != .OrderedDescending // decimal <= Int64.max + && decimal.compare(NSDecimalNumber(longLong: Int64.min)) != .OrderedAscending // decimal >= Int64.min + { + return longLongValue.databaseValue } switch String.fromCString(objCType)! {