Skip to content

Commit 61eca00

Browse files
committed
Added: Snapshot/index(_:offsetBy:limitedBy). Cleanup n' stuff
1 parent 657122b commit 61eca00

File tree

2 files changed

+31
-35
lines changed

2 files changed

+31
-35
lines changed

Sources/DiffableTextKit/Models/Snapshot.swift

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,11 @@ extension Snapshot {
110110
// MARK: Sequence
111111
//=------------------------------------------------------------------------=
112112

113-
@inlinable public init(
114-
_ characters: some Sequence<Character>,
115-
as attribute: Attribute = .content) {
113+
@inlinable public init(_ characters: some Sequence<Character>, as attribute: Attribute = .content) {
116114
self.init(characters, as: { _ in attribute })
117115
}
118116

119-
@inlinable public init(
120-
_ characters: some Sequence<Character>,
121-
as attribute: (Character) -> Attribute) {
117+
@inlinable public init(_ characters: some Sequence<Character>, as attribute: (Character) -> Attribute) {
122118
self.init(); self.append(contentsOf: characters, as: attribute)
123119
}
124120
}
@@ -168,6 +164,14 @@ extension Snapshot {
168164
return Index(character, as: attribute)
169165
}
170166

167+
@inlinable public func index(_ position: Index, offsetBy distance: Int, limitedBy limit: Index) -> Index? {
168+
guard let attribute = _attributes.index(position.attribute/*----*/,
169+
offsetBy: distance, limitedBy: limit.attribute) else { return nil }
170+
guard let character = _characters.index(position.character/*----*/,
171+
offsetBy: distance, limitedBy: limit.character) else { return nil }
172+
return Index(character, as: attribute)
173+
}
174+
171175
//=------------------------------------------------------------------------=
172176
// MARK: Distance, Count
173177
//=------------------------------------------------------------------------=
@@ -246,10 +250,9 @@ extension Snapshot {
246250
// MARK: Insert
247251
//=------------------------------------------------------------------------=
248252

249-
@inlinable public mutating func insert(
250-
_ symbol: Symbol, at position: Index) {
251-
self._characters.insert(symbol.character,at: position.character)
252-
self._attributes.insert(symbol.attribute,at: position.attribute)
253+
@inlinable public mutating func insert(_ symbol: Symbol, at position: Index) {
254+
self._characters.insert(symbol.character, at: position.character)
255+
self._attributes.insert(symbol.attribute, at: position.attribute)
253256
}
254257

255258
@inlinable public mutating func insert(

Sources/DiffableTextKitXPattern/Placeholders.swift

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,22 @@ public struct PatternTextPlaceholders: Equatable, ExpressibleByDictionaryLiteral
3030
}
3131

3232
@inlinable public init(_ character: Character, where predicate: @escaping (Character) -> Bool) {
33-
self.option = .some(Some((character, predicate)))
33+
self.option = .some(Some(base: (character, predicate)))
3434
}
3535

3636
@inlinable public init(_ some: (Character, (Character) -> Bool)) {
37-
self.option = .some(Some(some))
37+
self.option = .some(Some(base: some))
3838
}
3939

4040
@inlinable public init(_ many: [Character: (Character) -> Bool]) {
41-
self.option = .many(Many(many))
41+
self.option = .many(Many(base: many))
4242
}
4343

4444
@inlinable public init(dictionaryLiteral elements: (Character, (Character) -> Bool)...) {
45-
switch elements.count /* O(1) */ {
46-
case 0: self.init()
47-
case 1: self.init(elements[0])
48-
default: self.init(Dictionary(elements, uniquingKeysWith: { $1 })) }
45+
switch elements.count {
46+
case 1: self.init(elements[0])
47+
case 2...: self.init(Dictionary(elements, uniquingKeysWith: { $1 }))
48+
default: self.init() }
4949
}
5050

5151
//=------------------------------------------------------------------------=
@@ -68,36 +68,29 @@ public struct PatternTextPlaceholders: Equatable, ExpressibleByDictionaryLiteral
6868
case some(Some)
6969
case many(Many)
7070
}
71-
}
72-
73-
//=----------------------------------------------------------------------------=
74-
// MARK: + Option(s)
75-
//=----------------------------------------------------------------------------=
76-
77-
extension PatternTextPlaceholders {
7871

7972
//*========================================================================*
8073
// MARK: * Some [...]
8174
//*========================================================================*
8275

8376
@usableFromInline struct Some: Equatable {
84-
77+
8578
//=--------------------------------------------------------------------=
8679

87-
@usableFromInline let elements: (Character, Predicate)
80+
@usableFromInline let base: (Character, Predicate)
8881

8982
//=--------------------------------------------------------------------=
9083

91-
@inlinable init( _ elements: (Character, Predicate)) {
92-
self.elements = elements
84+
@inlinable init(base: (Character, Predicate)) {
85+
self.base = base
9386
}
9487

9588
@inlinable subscript(character: Character) -> Predicate? {
96-
elements.0 == character ? elements.1 : nil
89+
base.0 == character ? base.1 : nil
9790
}
9891

9992
@inlinable static func == (lhs: Self, rhs: Self) -> Bool {
100-
lhs.elements.0 == rhs.elements.0
93+
lhs.base.0 == rhs.base.0
10194
}
10295
}
10396

@@ -109,20 +102,20 @@ extension PatternTextPlaceholders {
109102

110103
//=--------------------------------------------------------------------=
111104

112-
@usableFromInline let elements: [Character: Predicate]
105+
@usableFromInline let base: [Character: Predicate]
113106

114107
//=--------------------------------------------------------------------=
115108

116-
@inlinable init( _ elements: [Character: Predicate]) {
117-
self.elements = elements
109+
@inlinable init(base: [Character: Predicate]) {
110+
self.base = base
118111
}
119112

120113
@inlinable subscript(character: Character) -> Predicate? {
121-
elements[character]
114+
base[character]
122115
}
123116

124117
@inlinable static func == (lhs: Self, rhs: Self) -> Bool {
125-
lhs.elements.keys == rhs.elements.keys
118+
lhs.base.keys == rhs.base.keys
126119
}
127120
}
128121
}

0 commit comments

Comments
 (0)