Skip to content

Commit 8c5c9aa

Browse files
xwuLukasa
andauthored
Update for open-source Decimal fixes (#16)
* Update to future-proof usage of Decimal.significand * Update all remaining uses of Decimal.significand * Update usage of `Decimal(sign:exponent:significand:)` * Apply suggestions from code review Co-authored-by: Cory Benfield <lukasa@apple.com> Co-authored-by: Cory Benfield <lukasa@apple.com>
1 parent e31cbd3 commit 8c5c9aa

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

Sources/StructuredFieldValues/Decoder/BareItemDecoder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ extension BareItemDecoder: SingleValueDecodingContainer {
116116

117117
return Decimal(sign: pseudoDecimal.mantissa > 0 ? .plus : .minus,
118118
exponent: Int(pseudoDecimal.exponent),
119-
significand: Decimal(pseudoDecimal.mantissa))
119+
significand: Decimal(pseudoDecimal.mantissa.magnitude))
120120
}
121121

122122
func decodeNil() -> Bool {

Sources/StructuredFieldValues/Encoder/StructuredFieldValueEncoder.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -281,12 +281,12 @@ extension _StructuredFieldEncoder: SingleValueEncodingContainer {
281281
}
282282

283283
func encode(_ data: Decimal) throws {
284-
let significand = (data.significand as NSNumber).intValue // Yes, really.
284+
let significand = (data.significand.magnitude as NSNumber).intValue // Yes, really.
285285
guard let exponent = Int8(exactly: data.exponent) else {
286286
throw StructuredHeaderError.invalidIntegerOrDecimal
287287
}
288288

289-
let pd = PseudoDecimal(mantissa: significand, exponent: Int(exponent))
289+
let pd = PseudoDecimal(mantissa: significand * (data.isSignMinus ? -1 : 1), exponent: Int(exponent))
290290
try self.currentStackEntry.storage.insertBareItem(.decimal(pd))
291291
}
292292

@@ -438,12 +438,12 @@ extension _StructuredFieldEncoder {
438438
}
439439

440440
func append(_ value: Decimal) throws {
441-
let significand = (value.significand as NSNumber).intValue // Yes, really.
441+
let significand = (value.significand.magnitude as NSNumber).intValue // Yes, really.
442442
guard let exponent = Int8(exactly: value.exponent) else {
443443
throw StructuredHeaderError.invalidIntegerOrDecimal
444444
}
445445

446-
let pd = PseudoDecimal(mantissa: significand, exponent: Int(exponent))
446+
let pd = PseudoDecimal(mantissa: significand * (value.isSignMinus ? -1 : 1), exponent: Int(exponent))
447447
try self.currentStackEntry.storage.appendBareItem(.decimal(pd))
448448
}
449449

@@ -605,12 +605,12 @@ extension _StructuredFieldEncoder {
605605
}
606606

607607
func encode(_ value: Decimal, forKey key: String) throws {
608-
let significand = (value.significand as NSNumber).intValue // Yes, really.
608+
let significand = (value.significand.magnitude as NSNumber).intValue // Yes, really.
609609
guard let exponent = Int8(exactly: value.exponent) else {
610610
throw StructuredHeaderError.invalidIntegerOrDecimal
611611
}
612612

613-
let pd = PseudoDecimal(mantissa: significand, exponent: Int(exponent))
613+
let pd = PseudoDecimal(mantissa: significand * (value.isSignMinus ? -1 : 1), exponent: Int(exponent))
614614
try self.currentStackEntry.storage.insertBareItem(.decimal(pd), atKey: key)
615615
}
616616

0 commit comments

Comments
 (0)