Skip to content
Open
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
18 changes: 12 additions & 6 deletions Sources/NIOFS/ByteCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of kilobytes
public static func kilobytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1000 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1000)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of megabytes
Expand All @@ -38,7 +39,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of megabytes
public static func megabytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1000 * 1000 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1000 * 1000)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of gigabytes
Expand All @@ -47,7 +49,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of gigabytes
public static func gigabytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1000 * 1000 * 1000 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1000 * 1000 * 1000)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of kibibytes
Expand All @@ -56,7 +59,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of kibibytes
public static func kibibytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1024 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1024)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of mebibytes
Expand All @@ -65,7 +69,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of mebibytes
public static func mebibytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1024 * 1024 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1024 * 1024)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of gibibytes
Expand All @@ -74,7 +79,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of gibibytes
public static func gibibytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1024 * 1024 * 1024 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1024 * 1024 * 1024)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}
}

Expand Down
18 changes: 12 additions & 6 deletions Sources/_NIOFileSystem/ByteCount.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of kilobytes
public static func kilobytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1000 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1000)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of megabytes
Expand All @@ -38,7 +39,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of megabytes
public static func megabytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1000 * 1000 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1000 * 1000)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of gigabytes
Expand All @@ -47,7 +49,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of gigabytes
public static func gigabytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1000 * 1000 * 1000 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1000 * 1000 * 1000)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of kibibytes
Expand All @@ -56,7 +59,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of kibibytes
public static func kibibytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1024 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1024)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of mebibytes
Expand All @@ -65,7 +69,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of mebibytes
public static func mebibytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1024 * 1024 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1024 * 1024)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}

/// Returns a ``ByteCount`` with a given number of gibibytes
Expand All @@ -74,7 +79,8 @@ public struct ByteCount: Hashable, Sendable {
///
/// - Parameter count: The number of gibibytes
public static func gibibytes(_ count: Int64) -> ByteCount {
ByteCount(bytes: 1024 * 1024 * 1024 * count)
let (result, overflow) = count.multipliedReportingOverflow(by: 1024 * 1024 * 1024)
return ByteCount(bytes: overflow ? (count > 0 ? .max : .min) : result)
}
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/NIOFSTests/ByteCountTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,29 @@ class ByteCountTests: XCTestCase {
XCTAssertLessThan(byteCount1, byteCount2)
XCTAssertGreaterThan(byteCount2, byteCount1)
}

func testByteCountNegative() {
XCTAssertEqual(ByteCount.kilobytes(-10).bytes, -10_000)
XCTAssertEqual(ByteCount.megabytes(-10).bytes, -10_000_000)
XCTAssertEqual(ByteCount.gigabytes(-10).bytes, -10_000_000_000)
XCTAssertEqual(ByteCount.kibibytes(-10).bytes, -10_240)
XCTAssertEqual(ByteCount.mebibytes(-10).bytes, -10_485_760)
XCTAssertEqual(ByteCount.gibibytes(-10).bytes, -10_737_418_240)
}

func testByteCountOverflow() {
XCTAssertEqual(ByteCount.kilobytes(.max).bytes, .max)
XCTAssertEqual(ByteCount.megabytes(.max).bytes, .max)
XCTAssertEqual(ByteCount.gigabytes(.max).bytes, .max)
XCTAssertEqual(ByteCount.kibibytes(.max).bytes, .max)
XCTAssertEqual(ByteCount.mebibytes(.max).bytes, .max)
XCTAssertEqual(ByteCount.gibibytes(.max).bytes, .max)

XCTAssertEqual(ByteCount.kilobytes(.min).bytes, .min)
XCTAssertEqual(ByteCount.megabytes(.min).bytes, .min)
XCTAssertEqual(ByteCount.gigabytes(.min).bytes, .min)
XCTAssertEqual(ByteCount.kibibytes(.min).bytes, .min)
XCTAssertEqual(ByteCount.mebibytes(.min).bytes, .min)
XCTAssertEqual(ByteCount.gibibytes(.min).bytes, .min)
}
}