Skip to content

Commit 172561a

Browse files
spevansianpartridge
authored andcommitted
ByteCountFormatter: Match Darwin behaviour
1 parent ae21fb3 commit 172561a

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

Foundation/ByteCountFormatter.swift

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ extension ByteCountFormatter {
1313
public let rawValue : UInt
1414
public init(rawValue: UInt) { self.rawValue = rawValue }
1515

16-
// This causes default units appropriate for the platform to be used. Specifying any units explicitly causes just those units to be used in showing the number.
17-
public static let useDefault = Units(rawValue: 0)
1816
// Specifying any of the following causes the specified units to be used in showing the number.
1917
public static let useBytes = Units(rawValue: 1 << 0)
2018
public static let useKB = Units(rawValue: 1 << 1)
@@ -50,9 +48,9 @@ open class ByteCountFormatter : Formatter {
5048
NSUnimplemented()
5149
}
5250

53-
/* Specify the units that can be used in the output. If ByteCountFormatter.Units.useDefault, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
51+
/* Specify the units that can be used in the output. If ByteCountFormatter.Units is empty, uses platform-appropriate settings; otherwise will only use the specified units. This is the default value. Note that ZB and YB cannot be covered by the range of possible values, but you can still choose to use these units to get fractional display ("0.0035 ZB" for instance).
5452
*/
55-
open var allowedUnits: Units = .useDefault
53+
open var allowedUnits: Units = []
5654

5755
/* Specify how the count is displayed by indicating the number of bytes to be used for kilobyte. The default setting is ByteCountFormatter.CountStyle.fileCount, which is the system specific value for file and storage sizes.
5856
*/
@@ -127,52 +125,52 @@ open class ByteCountFormatter : Formatter {
127125
*/
128126
private func convertValue(fromByteCount byteCount: Int64, for byteSize: [Unit: Double]) -> String {
129127
let byte = Double(byteCount)
130-
if byte == 0, allowsNonnumericFormatting, allowedUnits == .useDefault, includesUnit, includesCount {
128+
if byte == 0, allowsNonnumericFormatting, allowedUnits == [], includesUnit, includesCount {
131129
return partsToIncludeFor(value: "Zero", unit: Unit.KB)
132130
} else if byte == 1 || byte == -1 {
133-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
131+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
134132
return formatNumberFor(bytes: byte, unit: Unit.byte)
135133
} else {
136134
return valueToUseFor(byteCount: byte, unit: allowedUnits)
137135
}
138136
} else if byte < byteSize[Unit.KB]! && byte > -byteSize[Unit.KB]!{
139-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
137+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
140138
return formatNumberFor(bytes: byte, unit: Unit.bytes)
141139
} else {
142140
return valueToUseFor(byteCount: byte, unit: allowedUnits)
143141
}
144142
} else if byte < byteSize[Unit.MB]! && byte > -byteSize[Unit.MB]! {
145-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
143+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
146144
return divide(byte, by: byteSize, for: .KB)
147145
}
148146
return valueToUseFor(byteCount: byte, unit: allowedUnits)
149147

150148
} else if byte < byteSize[Unit.GB]! && byte > -byteSize[Unit.GB]! {
151-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
149+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
152150
return divide(byte, by: byteSize, for: .MB)
153151
}
154152
return valueToUseFor(byteCount: byte, unit: allowedUnits)
155153

156154
} else if byte < byteSize[Unit.TB]! && byte > -byteSize[Unit.TB]! {
157-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
155+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
158156
return divide(byte, by: byteSize, for: .GB)
159157
}
160158
return valueToUseFor(byteCount: byte, unit: allowedUnits)
161159

162160
} else if byte < byteSize[Unit.PB]! && byte > -byteSize[Unit.PB]! {
163-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
161+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
164162
return divide(byte, by: byteSize, for: .TB)
165163
}
166164
return valueToUseFor(byteCount: byte, unit: allowedUnits)
167165

168166
} else if byte < byteSize[Unit.EB]! && byte > -byteSize[Unit.EB]! {
169-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
167+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
170168
return divide(byte, by: byteSize, for: .PB)
171169
}
172170
return valueToUseFor(byteCount: byte, unit: allowedUnits)
173171

174172
} else {
175-
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
173+
if allowedUnits.contains(.useAll) || allowedUnits == [] {
176174
return divide(byte, by: byteSize, for: .EB)
177175
}
178176
return valueToUseFor(byteCount: byte, unit: allowedUnits)
@@ -297,7 +295,7 @@ open class ByteCountFormatter : Formatter {
297295
} else {
298296
if lengthOfInt(number: Int(bytes)) == 3 {
299297
numberFormatter.usesSignificantDigits = false
300-
numberFormatter.maximumFractionDigits = 1
298+
numberFormatter.maximumFractionDigits = 0
301299
} else {
302300
numberFormatter.maximumSignificantDigits = 3
303301
numberFormatter.minimumSignificantDigits = 3
@@ -376,7 +374,7 @@ open class ByteCountFormatter : Formatter {
376374
} else if includesCount, includesUnit {
377375
return "\(value) \(unit)"
378376
} else if includesCount, !includesUnit {
379-
if value == "Zero", allowedUnits == .useDefault {
377+
if value == "Zero", allowedUnits == [] {
380378
return "0"
381379
} else {
382380
return value

TestFoundation/TestByteCountFormatter.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class TestByteCountFormatter : XCTestCase {
4949

5050
func test_DefaultValues() {
5151
let formatter = ByteCountFormatter()
52-
XCTAssertEqual(formatter.allowedUnits, ByteCountFormatter.Units.useDefault)
52+
XCTAssertEqual(formatter.allowedUnits, [])
5353
XCTAssertEqual(formatter.countStyle, ByteCountFormatter.CountStyle.file)
5454
XCTAssertEqual(formatter.allowsNonnumericFormatting, true)
5555
XCTAssertEqual(formatter.includesUnit, true)
@@ -68,7 +68,7 @@ class TestByteCountFormatter : XCTestCase {
6868
formatter.allowedUnits = .useGB
6969
XCTAssertEqual(formatter.string(fromByteCount: 0), "Zero KB")
7070

71-
formatter.allowedUnits = .useDefault
71+
formatter.allowedUnits = []
7272
formatter.allowsNonnumericFormatting = false
7373
XCTAssertEqual(formatter.string(fromByteCount: 0), "0 bytes")
7474

@@ -91,7 +91,7 @@ class TestByteCountFormatter : XCTestCase {
9191
formatter.allowedUnits = .useGB
9292
XCTAssertEqual(formatter.string(fromByteCount: 1), "0 GB")
9393

94-
formatter.allowedUnits = .useDefault
94+
formatter.allowedUnits = []
9595
formatter.isAdaptive = false
9696
XCTAssertEqual(formatter.string(fromByteCount: 1), "1 byte")
9797

@@ -365,7 +365,7 @@ class TestByteCountFormatter : XCTestCase {
365365
XCTAssertEqual(formatter.string(fromByteCount: 11999), "12.0 KB")
366366
XCTAssertEqual(formatter.string(fromByteCount: 900000), "900 KB")
367367
XCTAssertEqual(formatter.string(fromByteCount: 12345678), "12.3 MB")
368-
XCTAssertEqual(formatter.string(fromByteCount: 123456789), "123.5 MB")
368+
XCTAssertEqual(formatter.string(fromByteCount: 123456789), "123 MB")
369369
XCTAssertEqual(formatter.string(fromByteCount: 1234567898), "1.23 GB")
370370
XCTAssertEqual(formatter.string(fromByteCount: 12345678987), "12.3 GB")
371371
}

0 commit comments

Comments
 (0)