2022-03-13 21:31:34 +00:00
|
|
|
//
|
|
|
|
// DataUnit.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 3/30/18.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2022-03-13 21:31:34 +00:00
|
|
|
//
|
|
|
|
// https://github.com/passepartoutvpn
|
|
|
|
//
|
|
|
|
// This file is part of TunnelKit.
|
|
|
|
//
|
|
|
|
// TunnelKit is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// TunnelKit is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
2022-09-23 19:45:04 +00:00
|
|
|
/// Helps expressing integers in bytes, kB, MB, GB.
|
2022-03-13 21:31:34 +00:00
|
|
|
public enum DataUnit: UInt, CustomStringConvertible {
|
|
|
|
case byte = 1
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
case kilobyte = 1024
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
case megabyte = 1048576
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
case gigabyte = 1073741824
|
|
|
|
|
|
|
|
fileprivate var showsDecimals: Bool {
|
|
|
|
switch self {
|
|
|
|
case .byte, .kilobyte:
|
|
|
|
return false
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
case .megabyte, .gigabyte:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
fileprivate var boundary: UInt {
|
|
|
|
return UInt(0.1 * Double(rawValue))
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
// MARK: CustomStringConvertible
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
public var description: String {
|
|
|
|
switch self {
|
|
|
|
case .byte:
|
|
|
|
return "B"
|
|
|
|
|
|
|
|
case .kilobyte:
|
|
|
|
return "kB"
|
|
|
|
|
|
|
|
case .megabyte:
|
|
|
|
return "MB"
|
|
|
|
|
|
|
|
case .gigabyte:
|
|
|
|
return "GB"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:45:04 +00:00
|
|
|
/// Supports being represented in data unit.
|
|
|
|
public protocol DataUnitRepresentable {
|
|
|
|
|
|
|
|
/// Returns self expressed in bytes, kB, MB, GB.
|
|
|
|
var descriptionAsDataUnit: String { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
extension UInt: DataUnitRepresentable {
|
2022-03-13 21:31:34 +00:00
|
|
|
private static let allUnits: [DataUnit] = [
|
|
|
|
.gigabyte,
|
|
|
|
.megabyte,
|
|
|
|
.kilobyte,
|
|
|
|
.byte
|
|
|
|
]
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2022-03-13 21:31:34 +00:00
|
|
|
public var descriptionAsDataUnit: String {
|
|
|
|
if self == 0 {
|
|
|
|
return "0B"
|
|
|
|
}
|
|
|
|
for u in Self.allUnits {
|
|
|
|
if self >= u.boundary {
|
|
|
|
if !u.showsDecimals {
|
|
|
|
return "\(self / u.rawValue)\(u)"
|
|
|
|
}
|
|
|
|
let count = Double(self) / Double(u.rawValue)
|
|
|
|
return String(format: "%.2f%@", count, u.description)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fatalError("Number is negative")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 19:45:04 +00:00
|
|
|
extension Int: DataUnitRepresentable {
|
2022-03-13 21:31:34 +00:00
|
|
|
public var descriptionAsDataUnit: String {
|
|
|
|
return UInt(self).descriptionAsDataUnit
|
|
|
|
}
|
|
|
|
}
|