-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathStyle.swift
More file actions
277 lines (250 loc) · 11.1 KB
/
Style.swift
File metadata and controls
277 lines (250 loc) · 11.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//=----------------------------------------------------------------------------=
// This source file is part of the DiffableTextViews open source project.
//
// Copyright (c) 2022 Oscar Byström Ericsson
// Licensed under Apache License, Version 2.0
//
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
//=----------------------------------------------------------------------------=
import DiffableTextKit
//*============================================================================*
// MARK: * Style
//*============================================================================*
public struct PatternTextStyle<Value>: DiffableTextStyle where Value: Equatable,
Value: RangeReplaceableCollection, Value.Element == Character {
public typealias Placeholders = PatternTextPlaceholders
//=------------------------------------------------------------------------=
// MARK: State
//=------------------------------------------------------------------------=
public var pattern: String
public var placeholders: Placeholders
public var hidden: Bool
//=------------------------------------------------------------------------=
// MARK: Initializers
//=------------------------------------------------------------------------=
@inlinable public init(_ pattern: String, placeholders: Placeholders = .init(), hidden: Bool = false) {
self.pattern = pattern; self.placeholders = placeholders; self.hidden = hidden
}
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
/// Marks a single character as the style's placeholder.
@inlinable public func placeholders(_ character: Character,
where predicate: @escaping (Character) -> Bool) -> Self {
self.placeholders((character, predicate))
}
/// Marks a single character as the style's placeholder.
@inlinable public func placeholders(_ some: (Character, (Character) -> Bool)) -> Self {
var S0 = self; S0.placeholders = Placeholders(some); return S0
}
/// Marks multiple characters as the style's placeholders.
@inlinable public func placeholders(_ many: [Character: (Character) -> Bool]) -> Self {
var S0 = self; S0.placeholders = Placeholders(many); return S0
}
//=------------------------------------------------------------------------=
// MARK: Transformations
//=------------------------------------------------------------------------=
/// Hides the pattern's suffix.
///
/// Characters after the last value, or from the first placeholder, are excluded.
///
/// ```
/// |+|1|2|_|(|3|4|5|)|_|6|7|8|-|9|~
/// |x|o|o|x|x|o|o|o|x|x|o|o|o|x|o|~ (HIDDEN)
/// ```
///
/// ```
/// |+|1|2|_|(|3|4|5|)|_|6|7|8|-|9|#|-|#|#|~
/// |x|o|o|x|x|o|o|o|x|x|o|o|o|x|o|x|x|x|x|~ (VISIBLE)
/// ```
///
@inlinable public func hidden(_ hidden: Bool = true) -> Self {
var S0 = self; S0.hidden = hidden; return S0
}
}
//=----------------------------------------------------------------------------=
// MARK: + Utilities
//=----------------------------------------------------------------------------=
extension PatternTextStyle {
//=------------------------------------------------------------------------=
// MARK: Inactive
//=------------------------------------------------------------------------=
/// - Mismatches are separated.
@inlinable public func format(_ value: Value, with cache: inout Void) -> String {
reduce(with: value, into: String()) {
characters, virtuals, nonvirtual in
characters.append(contentsOf: virtuals)
characters.append(nonvirtual)
} none: {
characters, virtuals in
characters.append(contentsOf: virtuals)
} done: {
characters, virtuals, mismatches in
//=----------------------------------=
// Pattern
//=----------------------------------=
if !hidden {
characters.append(contentsOf: virtuals)
}
//=----------------------------------=
// Mismatches
//=----------------------------------=
if !mismatches.isEmpty {
characters.append("|")
characters.append(contentsOf: mismatches)
}
}
}
//=------------------------------------------------------------------------=
// MARK: Active
//=------------------------------------------------------------------------=
/// - Mismatches are removed.
@inlinable public func interpret(_ value: Value, with cache: inout Void) -> Commit<Value> {
reduce(with: value, into: Commit()) {
commit, virtuals, nonvirtual in
commit.snapshot.append(contentsOf: virtuals, as: .phantom)
commit.snapshot.append(nonvirtual)
commit.value .append(nonvirtual)
} none: {
commit, virtuals in
commit.snapshot.append(contentsOf: virtuals, as: .phantom)
commit.select({ $0.endIndex })
} done: {
commit, virtuals, mismatches in
//=----------------------------------=
// Pattern
//=----------------------------------=
if !hidden {
commit.snapshot.append(contentsOf: virtuals, as: .phantom)
}
//=----------------------------------=
// Mismatches
//=----------------------------------=
if !mismatches.isEmpty {
Brrr.autocorrection << Info([.mark(value), "has invalid suffix \(mismatches)"])
}
}
}
//=------------------------------------------------------------------------=
// MARK: Interactive
//=------------------------------------------------------------------------=
/// - Mismatches throw an error.
@inlinable public func resolve(_ proposal: Proposal, with cache: inout Void) throws -> Commit<Value> {
try reduce(with: proposal.lazy.merged().nonvirtuals(), into: Commit()) {
commit, virtuals, nonvirtual in
commit.snapshot.append(contentsOf: virtuals, as: .phantom)
commit.snapshot.append(nonvirtual)
commit.value .append(nonvirtual)
} none: {
commit, virtuals in
commit.snapshot.append(contentsOf: virtuals, as: .phantom)
commit.select({ $0.endIndex })
} done: {
commit, virtuals, mismatches in
//=----------------------------------=
// Pattern
//=----------------------------------=
if !hidden {
commit.snapshot.append(contentsOf: virtuals, as: .phantom)
}
//=----------------------------------=
// Mismatches
//=----------------------------------=
if !mismatches.isEmpty {
//=------------------------------=
// Content <= Capacity
//=------------------------------=
if !virtuals.isEmpty {
throw Info([.mark(mismatches.first!), "is invalid"])
//=------------------------------=
// Content > Capacity
//=------------------------------=
} else {
let capacity = Info.note(commit.value.count)
let elements = Info.mark(commit.snapshot.characters + mismatches)
throw Info(["\(elements) exceeded pattern capacity \(capacity)"])
}
}
}
}
//=------------------------------------------------------------------------=
// MARK: Inactive, Active, Interactive x Reduce
//=------------------------------------------------------------------------=
@inlinable @inline(never) func reduce<Content, Result>(
with content: Content,into result: Result,
some: (inout Result, Substring, Character) -> Void,
none: (inout Result, Substring) -> Void,
done: (inout Result, Substring, Content.SubSequence) throws -> Void)
rethrows -> Result where Content: Collection<Character> {
//=--------------------------------------=
// State
//=--------------------------------------=
var result = result
var cIndex = content.startIndex
var pIndex = pattern.startIndex
var qIndex = pIndex // position
//=--------------------------------------=
// Matches
//=--------------------------------------=
matches: while qIndex != pattern.endIndex {
//=----------------------------------=
// Position == Placeholder
//=----------------------------------=
if let predicate = placeholders[pattern[qIndex]] {
guard cIndex != content.endIndex else { break matches }
let nonvirtual = content[cIndex]
guard predicate(nonvirtual) /**/ else { break matches }
//=------------------------------=
// (!) Some
//=------------------------------=
some(&result, pattern[pIndex ..< qIndex], nonvirtual)
content.formIndex(after: &cIndex)
pattern.formIndex(after: &qIndex)
pIndex = qIndex
//=----------------------------------=
// Position != Placeholder
//=----------------------------------=
} else {
pattern.formIndex(after: &qIndex)
}
}
//=--------------------------------------=
// (!) None
//=--------------------------------------=
if pIndex == pattern.startIndex {
//=----------------------------------=
// Placeholders == 0
//=----------------------------------=
if qIndex == pattern.endIndex {
none(&result, pattern[pIndex ..< pIndex])
//=----------------------------------=
// Placeholders >= 1
//=----------------------------------=
} else {
none(&result, pattern[pIndex ..< qIndex])
pIndex = qIndex
}
}
//=--------------------------------------=
// (!) Done
//=--------------------------------------=
try done(&result, pattern[pIndex...], content[cIndex...]); return result
}
}
//*============================================================================*
// MARK: * Style x Init
//*============================================================================*
extension DiffableTextStyle {
/// Creates a `PatternTextStyle` without placeholders.
///
/// ```
/// DiffableTextField(value: $number) {
/// .pattern("+## (###) ###-##-##")
/// .placeholders("#") { $0.isASCII && $0.isNumber }
/// }
/// ```
///
@inlinable public static func pattern<T>(_ pattern: String) -> Self where Self == PatternTextStyle<T> {
Self(pattern)
}
}