Skip to content

Commit 78de7ea

Browse files
David DunnDavid Dunn
authored andcommitted
Make reserveCapacity values more readable & test fixes
1 parent 712f349 commit 78de7ea

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

Foundation/NSJSONSerialization.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ internal extension JSONSerialization {
284284
//MARK: - JSONSerializer
285285
private struct JSONWriter {
286286

287+
private let maxUIntLength = String(describing: UInt.max).characters.count
288+
private let maxIntLength = String(describing: Int.max).characters.count
287289
var indent = 0
288290
let pretty: Bool
289291
let writer: (String?) -> Void
@@ -334,8 +336,8 @@ private struct JSONWriter {
334336
var array: [UInt] = []
335337
var stringResult = ""
336338
//Maximum length of an UInt
337-
array.reserveCapacity(20)
338-
stringResult.reserveCapacity(20)
339+
array.reserveCapacity(maxUIntLength)
340+
stringResult.reserveCapacity(maxUIntLength)
339341
var number = value
340342

341343
while number != 0 {
@@ -375,10 +377,9 @@ private struct JSONWriter {
375377
}
376378
var array: [Int] = []
377379
var stringResult = ""
378-
//Maximum length of an Int
379-
array.reserveCapacity(19)
380+
array.reserveCapacity(maxIntLength)
380381
//Account for a negative sign
381-
stringResult.reserveCapacity(20)
382+
stringResult.reserveCapacity(maxIntLength + 1)
382383
var number = value
383384

384385
while number != 0 {

TestFoundation/TestNSJSONSerialization.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,22 +1060,22 @@ extension TestNSJSONSerialization {
10601060

10611061
func test_serialize_IntMax() {
10621062
let json: [Any] = [Int.max]
1063-
XCTAssertEqual(try trySerialize(json), "[9223372036854775807]")
1063+
XCTAssertEqual(try trySerialize(json), "[\(Int.max)]")
10641064
}
10651065

10661066
func test_serialize_IntMin() {
10671067
let json: [Any] = [Int.min]
1068-
XCTAssertEqual(try trySerialize(json), "[-9223372036854775808]")
1068+
XCTAssertEqual(try trySerialize(json), "[\(Int.min)]")
10691069
}
10701070

10711071
func test_serialize_UIntMax() {
10721072
let json: [Any] = [UInt.max]
1073-
XCTAssertEqual(try trySerialize(json), "[18446744073709551615]")
1073+
XCTAssertEqual(try trySerialize(json), "[\(Int.max)]")
10741074
}
10751075

10761076
func test_serialize_UIntMin() {
10771077
let json: [Any] = [UInt.min]
1078-
XCTAssertEqual(try trySerialize(json), "[0]")
1078+
XCTAssertEqual(try trySerialize(json), "[\(Int.min)]")
10791079
}
10801080

10811081
func test_serialize_stringEscaping() {

0 commit comments

Comments
 (0)