Skip to content

Commit 55919c1

Browse files
committed
Cleanup. Offset now conforms to Numeric
1 parent 79304e2 commit 55919c1

File tree

7 files changed

+66
-36
lines changed

7 files changed

+66
-36
lines changed

Sources/DiffableTextKit/Models/Attribute.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,16 @@
1111
// MARK: * Attribute
1212
//*============================================================================*
1313

14-
/// A set of behavioral options.
14+
/// A set of behavioral options, such that plain text has no attributes.
1515
///
16-
/// Plain text has no attributes.
17-
///
18-
/// - Note: The easiest way to unformat text is to exclude symbols marked as virtual.
16+
/// The easiest way to unformat text is to filter characters marked as virtual.
1917
///
2018
public struct Attribute: CustomStringConvertible, OptionSet {
2119

2220
//=------------------------------------------------------------------------=
2321
// MARK: Instances
2422
//=------------------------------------------------------------------------=
25-
23+
2624
/// Marks that a symbol is not real and should not be parsed.
2725
public static let virtual = Self(rawValue: 1 << 0)
2826

Sources/DiffableTextKit/Models/Offset.swift

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
// MARK: * Offset
1212
//*============================================================================*
1313

14-
public struct Offset<Encoding: DiffableTextKit.Encoding>: Strideable,
15-
AdditiveArithmetic, ExpressibleByIntegerLiteral {
14+
public struct Offset<Encoding: DiffableTextKit.Encoding>: Strideable, Numeric {
1615

1716
//=------------------------------------------------------------------------=
1817
// MARK: State
@@ -36,6 +35,16 @@ AdditiveArithmetic, ExpressibleByIntegerLiteral {
3635
self.distance = distance
3736
}
3837

38+
public init?(exactly source: some BinaryInteger) {
39+
guard let distance = Int(exactly: source) else { return nil }; self.distance = distance
40+
}
41+
42+
//=------------------------------------------------------------------------=
43+
// MARK: Accessors
44+
//=------------------------------------------------------------------------=
45+
46+
@inlinable public var magnitude: UInt { distance.magnitude }
47+
3948
//=------------------------------------------------------------------------=
4049
// MARK: Utilities
4150
//=------------------------------------------------------------------------=
@@ -75,6 +84,14 @@ AdditiveArithmetic, ExpressibleByIntegerLiteral {
7584
@inlinable @inline(__always) public static func -= (lhs: inout Self, rhs: Self) {
7685
lhs = lhs - rhs
7786
}
87+
88+
@inlinable @inline(__always) public static func * (lhs: Self, rhs: Self) -> Self {
89+
Self(lhs.distance * rhs.distance)
90+
}
91+
92+
@inlinable @inline(__always) public static func *= (lhs: inout Self, rhs: Self) {
93+
lhs = lhs * rhs
94+
}
7895
}
7996

8097
//*============================================================================*

Sources/DiffableTextKit/Models/Proposal.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct Proposal {
3131
}
3232

3333
@inlinable public init<T>(_ base: Snapshot, with replacement: Snapshot, in range: Range<Offset<T>>) {
34-
self.init(base, with: replacement, in: base.indices(at: range))
34+
self.base = base; self.replacement = replacement; self.range = base.indices(at: range)
3535
}
3636

3737
//=------------------------------------------------------------------------=

Sources/DiffableTextKit/Utilities/Options.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
public extension OptionSet {
1515

16-
@inlinable func callAsFunction(_ mask: Bool) -> Self {
16+
@inlinable @inline(__always) func callAsFunction(_ mask: Bool) -> Self {
1717
mask ? self : Self()
1818
}
1919

20-
@inlinable static func += (lhs: inout Self, rhs: Self) {
20+
@inlinable @inline(__always) static func += (lhs: inout Self, rhs: Self) {
2121
lhs.formUnion(rhs)
2222
}
2323

24-
@inlinable static prefix func !(instance: Self) -> Bool {
24+
@inlinable @inline(__always) static prefix func !(instance: Self) -> Bool {
2525
instance.isEmpty
2626
}
2727
}

Sources/DiffableTextKitXNumber/Graphs/Graph+Integers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ where Value: _Input & FixedWidthInteger {
4141
let limit = Value(clamping: Int.max)
4242
self.precision = Int(floor(log10(Double(limit))))
4343
self.max = Value(String(repeating: "9", count: precision))!
44-
self.min = Value(clamping: -1) * max // zero when unsigned
44+
self.min = Value(clamping: -1) * self.max // unsigned: zero
4545
}
4646

4747
//=------------------------------------------------------------------------=

Tests/DiffableTextKitTests/Models/Offset.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,22 +102,22 @@ final class EncodingTests: XCTestCase {
102102
XCTAssertEqual(lhs.attribute, rhs.attribute)
103103
}
104104

105-
func AssertDistanceWorks<T>(_ encoding: T.Type, _ distances: (Offset<T>, Offset<T>, Offset<T>)) {
106-
XCTAssertEqual(T.distance(from: start, to: start, in: emojis), distances.0)
107-
XCTAssertEqual(T.distance(from: middle, to: middle, in: emojis), distances.0)
108-
XCTAssertEqual(T.distance(from: end, to: end, in: emojis), distances.0)
105+
func AssertDistancePerEmoji<T>(_ type: T.Type, _ stride: Offset<T>) {
106+
XCTAssertEqual(T.distance(from: start, to: start, in: emojis), stride * 0)
107+
XCTAssertEqual(T.distance(from: middle, to: middle, in: emojis), stride * 0)
108+
XCTAssertEqual(T.distance(from: end, to: end, in: emojis), stride * 0)
109109

110-
XCTAssertEqual(T.distance(from: start, to: middle, in: emojis), +distances.1)
111-
XCTAssertEqual(T.distance(from: middle, to: start, in: emojis), -distances.1)
110+
XCTAssertEqual(T.distance(from: start, to: middle, in: emojis), stride * 1)
111+
XCTAssertEqual(T.distance(from: middle, to: start, in: emojis), -stride * 1)
112112

113-
XCTAssertEqual(T.distance(from: middle, to: end, in: emojis), +distances.1)
114-
XCTAssertEqual(T.distance(from: end, to: middle, in: emojis), -distances.1)
113+
XCTAssertEqual(T.distance(from: middle, to: end, in: emojis), stride * 1)
114+
XCTAssertEqual(T.distance(from: end, to: middle, in: emojis), -stride * 1)
115115

116-
XCTAssertEqual(T.distance(from: start, to: end, in: emojis), +distances.2)
117-
XCTAssertEqual(T.distance(from: end, to: start, in: emojis), -distances.2)
116+
XCTAssertEqual(T.distance(from: start, to: end, in: emojis), stride * 2)
117+
XCTAssertEqual(T.distance(from: end, to: start, in: emojis), -stride * 2)
118118
}
119119

120-
func AssertIndexWorks(_ type: (some Encoding).Type) {
120+
func AssertIndexAwayFromIndexWorks(_ type: (some Encoding).Type) {
121121
let max = type.distance(from: start, to: end, in: emojis)
122122
let mid = type.distance(from: start, to: middle, in: emojis)
123123
let i = { type.index(from: $0, move: $1, in: self.emojis) }
@@ -143,8 +143,8 @@ final class EncodingTests: XCTestCase {
143143
}
144144

145145
func AssertIndexClampsToStartOfCharacter(_ type: (some Encoding).Type) {
146-
let i = { type.index(from: $0, move: $1, in: self.emojis) }
147-
let d = { type.distance(from: $0, to: $1, in: self.emojis) }
146+
let i = { type .index(from: $0, move: $1, in: self.emojis) }
147+
let d = { type.distance(from: $0, to: $1, in: self.emojis) }
148148
//=--------------------------------------=
149149
// Forwards
150150
//=--------------------------------------=
@@ -176,11 +176,11 @@ final class EncodingTests: XCTestCase {
176176
//=------------------------------------------------------------------------=
177177

178178
func testCharacterDistance() {
179-
AssertDistanceWorks(Character.self, (0, 1, 2))
179+
AssertDistancePerEmoji(Character.self, 1)
180180
}
181181

182182
func testCharacterIndex() {
183-
AssertIndexWorks(Character.self)
183+
AssertIndexAwayFromIndexWorks(Character.self)
184184
}
185185

186186
func testCharacterIndexClampsToStartOfCharacter() {
@@ -192,11 +192,11 @@ final class EncodingTests: XCTestCase {
192192
//=------------------------------------------------------------------------=
193193

194194
func testUnicodeScalarDistance() {
195-
AssertDistanceWorks(Unicode.Scalar.self, (0, 2, 4))
195+
AssertDistancePerEmoji(Unicode.Scalar.self, 2)
196196
}
197197

198198
func testUnicodeScalarIndex() {
199-
AssertIndexWorks(Unicode.Scalar.self)
199+
AssertIndexAwayFromIndexWorks(Unicode.Scalar.self)
200200
}
201201

202202
func testUnicodeScalarIndexClampsToStartOfCharacter() {
@@ -208,11 +208,11 @@ final class EncodingTests: XCTestCase {
208208
//=------------------------------------------------------------------------=
209209

210210
func testUTF16Distance() {
211-
AssertDistanceWorks(UTF16.self, (0, 4, 8))
211+
AssertDistancePerEmoji(UTF16.self, 4)
212212
}
213213

214214
func testUTF16Index() {
215-
AssertIndexWorks(UTF16.self)
215+
AssertIndexAwayFromIndexWorks(UTF16.self)
216216
}
217217

218218
func testUTF16IndexClampsToStartOfCharacter() {
@@ -224,11 +224,11 @@ final class EncodingTests: XCTestCase {
224224
//=------------------------------------------------------------------------=
225225

226226
func testUTF8Distance() {
227-
AssertDistanceWorks(UTF8.self, (0, 8, 16))
227+
AssertDistancePerEmoji(UTF8.self, 8)
228228
}
229229

230230
func testUTF8Index() {
231-
AssertIndexWorks(UTF8.self)
231+
AssertIndexAwayFromIndexWorks(UTF8.self)
232232
}
233233

234234
func testUTF8IndexClampsToStartOfCharacter() {

Tests/DiffableTextKitTests/Utilities/Mask.swift renamed to Tests/DiffableTextKitTests/Utilities/Options.swift

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
import XCTest
1515

1616
//*============================================================================*
17-
// MARK: * Mask x Tests
17+
// MARK: * Options x Tests
1818
//*============================================================================*
1919

20-
final class MaskTests: XCTestCase {
20+
final class OptionsTests: XCTestCase {
2121

2222
//=------------------------------------------------------------------------=
2323
// MARK: Tests
@@ -29,8 +29,23 @@ final class MaskTests: XCTestCase {
2929
XCTAssertEqual(max( false), [ ])
3030
}
3131

32+
func testPlusEqualsFormsUnion() {
33+
var options = Options()
34+
options += Options(rawValue: 1 << 0)
35+
options += Options(rawValue: 1 << 1)
36+
options += Options(rawValue: 1 << 1)
37+
options += Options(rawValue: 1 << 1)
38+
options += Options(rawValue: 1 << 2)
39+
XCTAssertEqual(options.rawValue, 0b111)
40+
}
41+
42+
func testNotMeansSameAsIsEmpty() {
43+
XCTAssertEqual(!Options(rawValue: 0b0), true)
44+
XCTAssertEqual(!Options(rawValue: 0b1), false)
45+
}
46+
3247
//*========================================================================*
33-
// MARK: * Elements
48+
// MARK: * Options [...]
3449
//*========================================================================*
3550

3651
struct Options: OptionSet { let rawValue: UInt8 }

0 commit comments

Comments
 (0)