Skip to content

Commit

Permalink
Add Swift 5.8 support, general cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ffried committed Apr 9, 2023
1 parent a647cc8 commit 44c5f32
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 36 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version: 5.7
// swift-tools-version: 5.8
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
Expand Down
59 changes: 59 additions & 0 deletions [email protected]
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// swift-tools-version: 5.7
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription
import Foundation

let package = Package(
name: "simple-formatting",
products: [
.library(name: "DurationFormatting",
targets: ["DurationFormatting"]),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages this package depends on.
.systemLibrary(
name: "CICUCommon",
pkgConfig: "icu-uc",
providers: [
.apt(["libicu-dev"]),
.brew(["icu4c"]),
]),
.systemLibrary(
name: "CICUI18N",
pkgConfig: "icu-i18n",
providers: [
.apt(["libicu-dev"]),
.brew(["icu4c"]),
]),
.systemLibrary(
name: "CICUIO",
pkgConfig: "icu-io",
providers: [
.apt(["libicu-dev"]),
.brew(["icu4c"]),
]),
.target(
name: "CICUShims",
dependencies: [
"CICUCommon",
"CICUI18N",
// "CICUIO",
]),
.target(
name: "DurationFormatting",
dependencies: ["CICUShims"]),
.testTarget(
name: "DurationFormattingTests",
dependencies: [
"CICUShims",
"DurationFormatting",
]),
],
cxxLanguageStandard: .cxx14
)

if ProcessInfo.processInfo.environment["ENABLE_DOCC_SUPPORT"] == "1" {
package.dependencies.append(.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"))
}
41 changes: 6 additions & 35 deletions Sources/DurationFormatting/DurationFormatter.swift
Original file line number Diff line number Diff line change
@@ -1,41 +1,12 @@
#if swift(>=5.7) && canImport(Darwin)
import Foundation
#else
@preconcurrency import Foundation
#endif
@_implementationOnly import CICUShims

public struct TimeComponents: Sendable {
public var hour: Int?
public var minute: Int?
public var second: Int?

@inlinable
public var dateComponents: DateComponents {
.init(hour: hour, minute: minute, second: second)
}

public init(hour: Int? = nil, minute: Int? = nil, second: Int? = nil) {
self.hour = hour
self.minute = minute
self.second = second
}

@inlinable
public init(dateComponents: DateComponents) {
self.init(hour: dateComponents.hour,
minute: dateComponents.minute,
second: dateComponents.second)
}

fileprivate func _cicuTimeComponents(nullingZeros: Bool) -> CICUTimeComponents {
func _component(for value: Int?) -> CICUTimeComponent {
value.map(numericCast).map(nullingZeros ? CICUTimeComponent.init(nullingZeroOf:) : CICUTimeComponent.init(_:)) ?? .null
}
return .init(hours: _component(for: hour),
minutes: _component(for: minute),
seconds: _component(for: second))
}
}

public struct DurationFormatter {
public enum Width: Sendable {
public struct DurationFormatter: Sendable {
public enum Width: Sendable, Hashable {
case numeric, short, narrow

fileprivate var _cicuFormatWidth: CICUDurationFormatWidth {
Expand Down
56 changes: 56 additions & 0 deletions Sources/DurationFormatting/TimeComponents.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import Foundation
@_implementationOnly import CICUShims

fileprivate extension CICUTimeComponent {
func value(droppingZero dropZero: Bool) -> Int? {
isNull || (dropZero && value == .zero) ? nil : numericCast(value)
}
}

public struct TimeComponents: Hashable, Comparable, Sendable {
public var hour: Int?
public var minute: Int?
public var second: Int?

@inlinable
public var dateComponents: DateComponents {
.init(hour: hour, minute: minute, second: second)
}

public init(hour: Int? = nil,
minute: Int? = nil,
second: Int? = nil) {
self.hour = hour
self.minute = minute
self.second = second
}

@inlinable
public init(dateComponents: DateComponents) {
self.init(hour: dateComponents.hour,
minute: dateComponents.minute,
second: dateComponents.second)
}

init(_cicuTimeComponents timeComponents: CICUTimeComponents, dropZeros: Bool) {
self.init(hour: timeComponents.hours.value(droppingZero: dropZeros),
minute: timeComponents.minutes.value(droppingZero: dropZeros),
second: timeComponents.seconds.value(droppingZero: dropZeros))
}

func _cicuTimeComponents(nullingZeros: Bool) -> CICUTimeComponents {
func _component(for value: Int?) -> CICUTimeComponent {
value.map(numericCast).map(nullingZeros ? CICUTimeComponent.init(nullingZeroOf:) : CICUTimeComponent.init(_:)) ?? .null
}
return .init(hours: _component(for: hour),
minutes: _component(for: minute),
seconds: _component(for: second))
}

public static func <(lhs: Self, rhs: Self) -> Bool {
guard lhs != rhs else { return false }
return (lhs.hour ?? 0, lhs.minute ?? 0, lhs.second ?? 0)
<
(rhs.hour ?? 0, rhs.minute ?? 0, rhs.second ?? 0)
}
}

0 comments on commit 44c5f32

Please sign in to comment.