Skip to content

Commit fdae56b

Browse files
committed
Cleanup: Snapshot. Added: a more efficient Snapshot init method.
1 parent 0dae563 commit fdae56b

File tree

2 files changed

+81
-57
lines changed

2 files changed

+81
-57
lines changed

Sources/DiffableTextKit/Models/Snapshot.swift

Lines changed: 79 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,42 @@ public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
4747
}
4848

4949
//=------------------------------------------------------------------------=
50-
// MARK: Initializers
50+
// MARK: Accessors
51+
//=------------------------------------------------------------------------=
52+
53+
@inlinable public var characters: String {
54+
_characters
55+
}
56+
57+
@inlinable public var attributes: [Attribute] {
58+
_attributes
59+
}
60+
61+
@inlinable public var anchor: Index? {
62+
_anchor
63+
}
64+
65+
//=------------------------------------------------------------------------=
66+
// MARK: Transformations
67+
//=------------------------------------------------------------------------=
68+
69+
@inlinable public mutating func anchorAtEndIndex() {
70+
self._anchor = endIndex
71+
}
72+
73+
@inlinable public mutating func anchor(at index: Index?) {
74+
self._anchor = index
75+
}
76+
}
77+
78+
//=----------------------------------------------------------------------------=
79+
// MARK: + Initializers
80+
//=----------------------------------------------------------------------------=
81+
82+
extension Snapshot {
83+
84+
//=------------------------------------------------------------------------=
85+
// MARK: Characters
5186
//=------------------------------------------------------------------------=
5287

5388
@inlinable public init(_ characters: String, as attribute: Attribute = .content) {
@@ -70,39 +105,24 @@ public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
70105
}
71106

72107
//=------------------------------------------------------------------------=
73-
// MARK: Accessors
108+
// MARK: Characters
74109
//=------------------------------------------------------------------------=
75110

76-
@inlinable public var characters: String {
77-
_characters
78-
}
79-
80-
@inlinable public var attributes: [Attribute] {
81-
_attributes
82-
}
83-
84-
@inlinable public var anchor: Index? {
85-
_anchor
86-
}
87-
88-
//=------------------------------------------------------------------------=
89-
// MARK: Count
90-
//=------------------------------------------------------------------------=
91-
92-
/// - Complexity: O(1).
93-
@inlinable public var isEmpty: Bool {
94-
_attributes.isEmpty
95-
}
96-
97-
/// - Complexity: O(1).
98-
@inlinable public var count: Int {
99-
_attributes.count
100-
}
101-
102-
/// - Complexity: O(1).
103-
@inlinable public var underestimatedCount: Int {
104-
_attributes.underestimatedCount
111+
@inlinable public init(_ characters: String, as attribute: (Character) -> Attribute) {
112+
self._characters = characters
113+
self._attributes = []
114+
115+
for character in characters {
116+
self._attributes.append(attribute(character))
117+
}
105118
}
119+
}
120+
121+
//=----------------------------------------------------------------------------=
122+
// MARK: + Accessors
123+
//=----------------------------------------------------------------------------=
124+
125+
extension Snapshot {
106126

107127
//=------------------------------------------------------------------------=
108128
// MARK: Element
@@ -140,39 +160,46 @@ public struct Snapshot: BidirectionalCollection, RangeReplaceableCollection {
140160
Index(_characters .index(before: index.character),
141161
as: _attributes .index(before: index.attribute))
142162
}
143-
}
144-
145-
//=----------------------------------------------------------------------------=
146-
// MARK: + Transformations
147-
//=----------------------------------------------------------------------------=
148-
149-
public extension Snapshot {
150163

151164
//=------------------------------------------------------------------------=
152-
// MARK: Anchor
165+
// MARK: Count
153166
//=------------------------------------------------------------------------=
154167

155-
@inlinable mutating func anchorAtEndIndex() {
156-
self._anchor = endIndex
168+
/// - Complexity: O(1).
169+
@inlinable public var isEmpty: Bool {
170+
_attributes.isEmpty
157171
}
158172

159-
@inlinable mutating func anchor(at index: Index?) {
160-
self._anchor = index
173+
/// - Complexity: O(1).
174+
@inlinable public var count: Int {
175+
_attributes.count
161176
}
162177

178+
/// - Complexity: O(1).
179+
@inlinable public var underestimatedCount: Int {
180+
_attributes.underestimatedCount
181+
}
182+
}
183+
184+
//=----------------------------------------------------------------------------=
185+
// MARK: + Transformations
186+
//=----------------------------------------------------------------------------=
187+
188+
extension Snapshot {
189+
163190
//=------------------------------------------------------------------------=
164191
// MARK: Update
165192
//=------------------------------------------------------------------------=
166193

167-
@inlinable mutating func transform(attributes index: Index,
194+
@inlinable public mutating func transform(attributes index: Index,
168195
with transform: (inout Attribute) -> Void) {
169196
//=--------------------------------------=
170197
// Index
171198
//=--------------------------------------=
172199
transform(&_attributes[index.attribute])
173200
}
174201

175-
@inlinable mutating func transform<S>(attributes indices: S,
202+
@inlinable public mutating func transform<S>(attributes indices: S,
176203
with transform: (inout Attribute) -> Void)
177204
where S: Sequence, S.Element == Index {
178205
//=--------------------------------------=
@@ -183,7 +210,7 @@ public extension Snapshot {
183210
}
184211
}
185212

186-
@inlinable mutating func transform<R>(attributes indices: R,
213+
@inlinable public mutating func transform<R>(attributes indices: R,
187214
with transform: (inout Attribute) -> Void)
188215
where R: RangeExpression, R.Bound == Index {
189216
//=--------------------------------------=
@@ -198,7 +225,7 @@ public extension Snapshot {
198225
// MARK: Replace
199226
//=------------------------------------------------------------------------=
200227

201-
@inlinable mutating func replaceSubrange<C>(_ indices: Range<Index>,
228+
@inlinable public mutating func replaceSubrange<C>(_ indices: Range<Index>,
202229
with elements: C) where C: Collection, C.Element == Symbol {
203230
//=--------------------------------------=
204231
// Characters
@@ -216,29 +243,29 @@ public extension Snapshot {
216243
with: elements.lazy.map(\.attribute))
217244
}
218245

219-
@inlinable mutating func append(_ element: Symbol) {
246+
@inlinable public mutating func append(_ element: Symbol) {
220247
_characters.append(element.character)
221248
_attributes.append(element.attribute)
222249
}
223250

224-
@inlinable mutating func append<S: Sequence>(
251+
@inlinable public mutating func append<S: Sequence>(
225252
contentsOf elements: S) where S.Element == Symbol {
226253
_characters.append(contentsOf: elements.lazy.map(\.character))
227254
_attributes.append(contentsOf: elements.lazy.map(\.attribute))
228255
}
229256

230-
@inlinable mutating func insert(_ element: Element, at index: Index) {
257+
@inlinable public mutating func insert(_ element: Element, at index: Index) {
231258
_characters.insert(element.character, at: index.character)
232259
_attributes.insert(element.attribute, at: index.attribute)
233260
}
234261

235-
@inlinable mutating func insert<C: Collection>(
262+
@inlinable public mutating func insert<C: Collection>(
236263
contentsOf elements: C, at index: Index) where C.Element == Symbol {
237264
_characters.insert(contentsOf: elements.lazy.map(\.character), at: index.character)
238265
_attributes.insert(contentsOf: elements.lazy.map(\.attribute), at: index.attribute)
239266
}
240267

241-
@inlinable @discardableResult mutating func remove(at index: Index) -> Symbol {
268+
@inlinable @discardableResult public mutating func remove(at index: Index) -> Symbol {
242269
Symbol(_characters.remove(at: index.character),
243270
as: _attributes.remove(at: index.attribute))
244271
}

Sources/DiffableTextKitXNumber/Style.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,13 +143,10 @@ extension _NumberTextStyle {
143143

144144
/// Assumes characters contain at least one content character.
145145
@inlinable func snapshot(_ characters: String) -> Snapshot {
146-
var snapshot = Snapshot()
147146
//=--------------------------------------=
148-
// Characters
147+
// Characters, Attributes
149148
//=--------------------------------------=
150-
for character in characters {
151-
snapshot.append(Symbol(character, as: reader.attributes[character]))
152-
}
149+
var snapshot = Snapshot(characters) { reader.attributes[$0] }
153150
//=--------------------------------------=
154151
// Autocorrect
155152
//=--------------------------------------=

0 commit comments

Comments
 (0)