Skip to content

Commit a71cf1e

Browse files
committed
Cleanup + small prefix(_:) optimization.
1 parent 02b08c3 commit a71cf1e

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

Sources/DiffableTextKit/Styles/Prefix.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
6363
text = prefix + text
6464
}
6565

66+
/// This transformation assumes that the base style
67+
/// provides a manual selection when all attributes
68+
/// are passtrough, to avoid duplicate computations.
6669
@inlinable func label(_ snapshot: inout Snapshot) {
6770
//=--------------------------------------=
6871
// None
@@ -86,10 +89,12 @@ public struct PrefixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
8689
// Base x Some x Selection
8790
//=--------------------------------------=
8891
guard let selection = base.selection else { return }
89-
let offsets = selection.map({ size + $0.attribute })
90-
let lower = snapshot.index(snapshot.startIndex, offsetBy: offsets.lower)
91-
let upper = snapshot.index(lower, offsetBy: offsets.upper - offsets.lower)
92-
snapshot.select(Range(uncheckedBounds: (lower, upper)))
92+
let min = selection.lower.attribute + size // offset
93+
let max = selection.upper.attribute + size // offset
94+
95+
let lower = snapshot.index(snapshot.startIndex, offsetBy: min)
96+
let upper = snapshot.index(lower, /*---*/ offsetBy: max - min)
97+
snapshot.select(Range(uncheckedBounds: (lower, upper))) /*--*/
9398
}
9499
}
95100

Sources/DiffableTextKit/Styles/Suffix.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ public struct SuffixTextStyle<Base: DiffableTextStyle>: WrapperTextStyle {
5959
text.append(contentsOf: suffix)
6060
}
6161

62+
/// This transformation assumes that the base style
63+
/// provides a manual selection when all attributes
64+
/// are passtrough, to avoid duplicate computations.
6265
@inlinable func label(_ snapshot: inout Snapshot) {
6366
//=--------------------------------------=
6467
// Base x None

Sources/DiffableTextKit/Utilities/Count.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
public extension Sequence {
1515

1616
@inlinable func count(where predicate: (Element) throws -> Bool) rethrows -> Int {
17-
var S0 = 0; for x in self { if try predicate(x) { S0 += 1 } }; return S0
17+
var S0 = 0; for S1 in self { if try predicate(S1) { S0 += 1 } }; return S0
1818
}
1919

2020
@inlinable func count(while predicate: (Element) throws -> Bool) rethrows -> Int {
21-
var S0 = 0; for x in self { if try predicate(x) { S0 += 1 } else { break } }; return S0
21+
var S0 = 0; for S1 in self { if try predicate(S1) { S0 += 1 } else { break } }; return S0
2222
}
2323
}

0 commit comments

Comments
 (0)