Skip to content

ByteCountFormatter: Match Darwin behaviour #1227

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 13 additions & 15 deletions Foundation/ByteCountFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ extension ByteCountFormatter {
public let rawValue : UInt
public init(rawValue: UInt) { self.rawValue = rawValue }

// 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.
public static let useDefault = Units(rawValue: 0)
// Specifying any of the following causes the specified units to be used in showing the number.
public static let useBytes = Units(rawValue: 1 << 0)
public static let useKB = Units(rawValue: 1 << 1)
Expand Down Expand Up @@ -50,9 +48,9 @@ open class ByteCountFormatter : Formatter {
NSUnimplemented()
}

/* 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).
/* 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).
*/
open var allowedUnits: Units = .useDefault
open var allowedUnits: Units = []

/* 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.
*/
Expand Down Expand Up @@ -127,52 +125,52 @@ open class ByteCountFormatter : Formatter {
*/
private func convertValue(fromByteCount byteCount: Int64, for byteSize: [Unit: Double]) -> String {
let byte = Double(byteCount)
if byte == 0, allowsNonnumericFormatting, allowedUnits == .useDefault, includesUnit, includesCount {
if byte == 0, allowsNonnumericFormatting, allowedUnits == [], includesUnit, includesCount {
return partsToIncludeFor(value: "Zero", unit: Unit.KB)
} else if byte == 1 || byte == -1 {
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
if allowedUnits.contains(.useAll) || allowedUnits == [] {
return formatNumberFor(bytes: byte, unit: Unit.byte)
} else {
return valueToUseFor(byteCount: byte, unit: allowedUnits)
}
} else if byte < byteSize[Unit.KB]! && byte > -byteSize[Unit.KB]!{
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
if allowedUnits.contains(.useAll) || allowedUnits == [] {
return formatNumberFor(bytes: byte, unit: Unit.bytes)
} else {
return valueToUseFor(byteCount: byte, unit: allowedUnits)
}
} else if byte < byteSize[Unit.MB]! && byte > -byteSize[Unit.MB]! {
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
if allowedUnits.contains(.useAll) || allowedUnits == [] {
return divide(byte, by: byteSize, for: .KB)
}
return valueToUseFor(byteCount: byte, unit: allowedUnits)

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

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

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

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

} else {
if allowedUnits.contains(.useAll) || allowedUnits == .useDefault {
if allowedUnits.contains(.useAll) || allowedUnits == [] {
return divide(byte, by: byteSize, for: .EB)
}
return valueToUseFor(byteCount: byte, unit: allowedUnits)
Expand Down Expand Up @@ -297,7 +295,7 @@ open class ByteCountFormatter : Formatter {
} else {
if lengthOfInt(number: Int(bytes)) == 3 {
numberFormatter.usesSignificantDigits = false
numberFormatter.maximumFractionDigits = 1
numberFormatter.maximumFractionDigits = 0
} else {
numberFormatter.maximumSignificantDigits = 3
numberFormatter.minimumSignificantDigits = 3
Expand Down Expand Up @@ -376,7 +374,7 @@ open class ByteCountFormatter : Formatter {
} else if includesCount, includesUnit {
return "\(value) \(unit)"
} else if includesCount, !includesUnit {
if value == "Zero", allowedUnits == .useDefault {
if value == "Zero", allowedUnits == [] {
return "0"
} else {
return value
Expand Down
8 changes: 4 additions & 4 deletions TestFoundation/TestByteCountFormatter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TestByteCountFormatter : XCTestCase {

func test_DefaultValues() {
let formatter = ByteCountFormatter()
XCTAssertEqual(formatter.allowedUnits, ByteCountFormatter.Units.useDefault)
XCTAssertEqual(formatter.allowedUnits, [])
XCTAssertEqual(formatter.countStyle, ByteCountFormatter.CountStyle.file)
XCTAssertEqual(formatter.allowsNonnumericFormatting, true)
XCTAssertEqual(formatter.includesUnit, true)
Expand All @@ -68,7 +68,7 @@ class TestByteCountFormatter : XCTestCase {
formatter.allowedUnits = .useGB
XCTAssertEqual(formatter.string(fromByteCount: 0), "Zero KB")

formatter.allowedUnits = .useDefault
formatter.allowedUnits = []
formatter.allowsNonnumericFormatting = false
XCTAssertEqual(formatter.string(fromByteCount: 0), "0 bytes")

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

formatter.allowedUnits = .useDefault
formatter.allowedUnits = []
formatter.isAdaptive = false
XCTAssertEqual(formatter.string(fromByteCount: 1), "1 byte")

Expand Down Expand Up @@ -365,7 +365,7 @@ class TestByteCountFormatter : XCTestCase {
XCTAssertEqual(formatter.string(fromByteCount: 11999), "12.0 KB")
XCTAssertEqual(formatter.string(fromByteCount: 900000), "900 KB")
XCTAssertEqual(formatter.string(fromByteCount: 12345678), "12.3 MB")
XCTAssertEqual(formatter.string(fromByteCount: 123456789), "123.5 MB")
XCTAssertEqual(formatter.string(fromByteCount: 123456789), "123 MB")
XCTAssertEqual(formatter.string(fromByteCount: 1234567898), "1.23 GB")
XCTAssertEqual(formatter.string(fromByteCount: 12345678987), "12.3 GB")
}
Expand Down