Skip to content

Commit 41c975c

Browse files
committed
Updated utilities
1 parent 21eecd8 commit 41c975c

10 files changed

Lines changed: 175 additions & 163 deletions

Sources/SwiftTimecodeCore/Utilities/Outsourced/CharacterSet.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Foundation/CharacterSet.swift
3+
/// Extensions/Foundation/CharacterSet.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------
1010
/// ----------------------------------------------
11-
///
11+
1212
#if canImport(Foundation)
1313

1414
import Foundation

Sources/SwiftTimecodeCore/Utilities/Outsourced/Decimal.swift

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Foundation/Decimal.swift
3+
/// Extensions/Foundation/Decimal.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------
@@ -37,10 +37,10 @@ extension Decimal {
3737
) -> Self {
3838
var initialDecimal = self
3939
var roundedDecimal = Decimal()
40-
let decimalPlaces = max(0, decimalPlaces)
41-
40+
let decimalPlaces = decimalPlaces.clamped(to: 0...)
41+
4242
NSDecimalRound(&roundedDecimal, &initialDecimal, decimalPlaces, rule)
43-
43+
4444
return roundedDecimal
4545
}
4646

@@ -64,14 +64,14 @@ extension Decimal {
6464
package func truncated(decimalPlaces: Int) -> Self {
6565
var initialDecimal = self
6666
var roundedDecimal = Decimal()
67-
let decimalPlaces = max(0, decimalPlaces)
68-
67+
let decimalPlaces = decimalPlaces.clamped(to: 0...)
68+
6969
if self > 0 {
7070
NSDecimalRound(&roundedDecimal, &initialDecimal, decimalPlaces, .down)
7171
} else {
7272
NSDecimalRound(&roundedDecimal, &initialDecimal, decimalPlaces, .up)
7373
}
74-
74+
7575
return roundedDecimal
7676
}
7777
}
@@ -122,7 +122,7 @@ extension Decimal {
122122
}
123123

124124
extension Decimal {
125-
/// **OTCore:**
125+
/// **swift-extensions:**
126126
/// Returns the number of digit places of the ``integral`` portion (left of the decimal).
127127
package var integralDigitPlaces: Int {
128128
// this works but may be brittle.
@@ -134,7 +134,7 @@ extension Decimal {
134134
return "\(abs(significand))".count + exponent
135135
}
136136

137-
/// **OTCore:**
137+
/// **swift-extensions:**
138138
/// Returns the number of digit places of the ``fraction`` portion (right of the decimal).
139139
package var fractionDigitPlaces: Int {
140140
max(-exponent, 0)

Sources/SwiftTimecodeCore/Utilities/Outsourced/FloatingPoint and Darwin.swift

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Darwin/FloatingPoint and Darwin.swift
3+
/// Extensions/Darwin/FloatingPoint and Darwin.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------
@@ -79,66 +79,3 @@ extension Float80: FloatingPointPowerComputable {
7979
}
8080
}
8181
#endif
82-
83-
// MARK: - .truncated()
84-
85-
extension FloatingPoint where Self: FloatingPointPowerComputable {
86-
/// Replaces this value by truncating it to `decimalPlaces` number of decimal places.
87-
///
88-
/// If `decimalPlaces` <= 0, then `trunc(self)` is returned.
89-
@_disfavoredOverload
90-
package mutating func truncate(decimalPlaces: Int) {
91-
self = truncated(decimalPlaces: decimalPlaces)
92-
}
93-
94-
/// Truncates decimal places to `decimalPlaces` number of decimal places.
95-
///
96-
/// If `decimalPlaces <= 0`, then `trunc(self)` is returned.
97-
@_disfavoredOverload
98-
package func truncated(decimalPlaces: Int) -> Self {
99-
if decimalPlaces < 1 {
100-
return trunc(self)
101-
}
102-
103-
let offset = Self(10).power(Self(decimalPlaces))
104-
return trunc(self * offset) / offset
105-
}
106-
}
107-
108-
extension FloatingPoint {
109-
/// Similar to `Int.quotientAndRemainder(dividingBy:)` from the standard Swift library.
110-
@_disfavoredOverload
111-
package func quotientAndRemainder(dividingBy rhs: Self) -> (quotient: Self, remainder: Self) {
112-
let calculation = self / rhs
113-
let integral = trunc(calculation)
114-
let fraction = self - (integral * rhs)
115-
return (quotient: integral, remainder: fraction)
116-
}
117-
118-
/// Returns both integral part and fractional part.
119-
///
120-
/// - Note: This method is more computationally efficient than calling both `.integral` and .`fraction` properties separately unless you
121-
/// only require one or the other.
122-
///
123-
/// This method can result in a non-trivial loss of precision for the fractional part.
124-
@_disfavoredOverload
125-
package var integralAndFraction: (integral: Self, fraction: Self) {
126-
let integral = trunc(self)
127-
let fraction = self - integral
128-
return (integral: integral, fraction: fraction)
129-
}
130-
131-
/// Returns the integral part (digits before the decimal point)
132-
@_disfavoredOverload
133-
package var integral: Self {
134-
integralAndFraction.integral
135-
}
136-
137-
/// Returns the fractional part (digits after the decimal point)
138-
///
139-
/// - Note: this method can result in a non-trivial loss of precision for the fractional part.
140-
@_disfavoredOverload
141-
package var fraction: Self {
142-
integralAndFraction.fraction
143-
}
144-
}

Sources/SwiftTimecodeCore/Utilities/Outsourced/FloatingPoint and Foundation.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Swift/FloatingPoint.swift
3+
/// Extensions/Swift/FloatingPoint.swift
44
///
5-
/// Borrowed from OTCore 1.4.14 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------
@@ -16,13 +16,11 @@ import Foundation
1616
// MARK: - Digit Places
1717

1818
extension Double {
19-
/// **OTCore:**
2019
/// Returns the number of digit places of the ``integral`` portion (left of the decimal).
2120
package var integralDigitPlaces: Int {
2221
Decimal(self).integralDigitPlaces
2322
}
2423

25-
/// **OTCore:**
2624
/// Returns the number of digit places of the ``fraction`` portion (right of the decimal).
2725
package var fractionDigitPlaces: Int {
2826
Decimal(self).fractionDigitPlaces

Sources/SwiftTimecodeCore/Utilities/Outsourced/FloatingPoint.swift

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Swift/FloatingPoint.swift
3+
/// Extensions/Swift/FloatingPoint.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------
@@ -40,3 +40,66 @@ extension FloatingPoint where Self: FloatingPointPowerComputable {
4040
self = rounded(rule, decimalPlaces: decimalPlaces)
4141
}
4242
}
43+
44+
// MARK: - Truncated
45+
46+
extension FloatingPoint where Self: FloatingPointPowerComputable {
47+
/// Replaces this value by truncating it to `decimalPlaces` number of decimal places.
48+
///
49+
/// If `decimalPlaces <= 0`, then `self.rounded(.towardZero)` is returned.
50+
@_disfavoredOverload
51+
package mutating func truncate(decimalPlaces: Int) {
52+
self = truncated(decimalPlaces: decimalPlaces)
53+
}
54+
55+
/// Truncates decimal places to `decimalPlaces` number of decimal places.
56+
///
57+
/// If `decimalPlaces <= 0`, then `self.rounded(.towardZero)` is returned.
58+
@_disfavoredOverload
59+
package func truncated(decimalPlaces: Int) -> Self {
60+
if decimalPlaces < 1 {
61+
return rounded(.towardZero)
62+
}
63+
64+
let offset = Self(10).power(Self(decimalPlaces))
65+
return (self * offset).rounded(.towardZero) / offset
66+
}
67+
}
68+
69+
extension FloatingPoint {
70+
/// Similar to `Int.quotientAndRemainder(dividingBy:)` from the Swift standard library.
71+
@_disfavoredOverload
72+
package func quotientAndRemainder(dividingBy rhs: Self) -> (quotient: Self, remainder: Self) {
73+
let calculation = self / rhs
74+
let integral = calculation.rounded(.towardZero)
75+
let fraction = self - (integral * rhs)
76+
return (quotient: integral, remainder: fraction)
77+
}
78+
79+
/// Returns both integral part and fractional part.
80+
///
81+
/// - Note: This method is more computationally efficient than calling both `integral` and
82+
/// `fraction` properties separately unless you only require one or the other.
83+
///
84+
/// This method can result in a non-trivial loss of precision for the fractional part.
85+
@inlinable @_disfavoredOverload
86+
package var integralAndFraction: (integral: Self, fraction: Self) {
87+
let integral = rounded(.towardZero)
88+
let fraction = self - integral
89+
return (integral: integral, fraction: fraction)
90+
}
91+
92+
/// Returns the integral part (digits before the decimal point).
93+
@inlinable @_disfavoredOverload
94+
package var integral: Self {
95+
integralAndFraction.integral
96+
}
97+
98+
/// Returns the fractional part (digits after the decimal point).
99+
///
100+
/// - Note: this method can result in a non-trivial loss of precision for the fractional part.
101+
@inlinable @_disfavoredOverload
102+
package var fraction: Self {
103+
integralAndFraction.fraction
104+
}
105+
}

Sources/SwiftTimecodeCore/Utilities/Outsourced/FloatingPointPowerComputable.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Protocols/Protocols.swift
3+
/// Protocols/Protocols.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------

Sources/SwiftTimecodeCore/Utilities/Outsourced/Integers.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Swift/Integers.swift
3+
/// Extensions/Swift/Integers.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------

Sources/SwiftTimecodeCore/Utilities/Outsourced/NSAttributedString.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Foundation/NSAttributedString.swift
3+
/// Extensions/Foundation/NSAttributedString.swift
44
///
5-
/// Borrowed from OTCore 1.4.2 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------

Sources/SwiftTimecodeCore/Utilities/Outsourced/Ranges.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/// ----------------------------------------------
22
/// ----------------------------------------------
3-
/// OTCore/Extensions/Swift/Ranges.swift
3+
/// Extensions/Swift/Ranges.swift
44
///
5-
/// Borrowed from OTCore 1.4.1 under MIT license.
6-
/// https://github.com/orchetect/OTCore
5+
/// Borrowed from swift-extensions 2.1.7 under MIT license.
6+
/// https://github.com/orchetect/swift-extensions
77
/// Methods herein are unit tested at their source
88
/// so no unit tests are necessary.
99
/// ----------------------------------------------

0 commit comments

Comments
 (0)