Skip to content

Commit 76799df

Browse files
committed
Cleanup, DEBUG messages, fixed bugs introduced by v4.0.0.
1 parent c15861e commit 76799df

24 files changed

+148
-91
lines changed

Sources/DiffableTextKit/Models/Update.swift renamed to Sources/DiffableTextKit/Models/Changes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//*============================================================================*
1313

1414
/// Used to store state comparison results in order to avoid multiple comparisons.
15-
public struct Update: OptionSet {
15+
public struct Changes: OptionSet {
1616

1717
//=------------------------------------------------------------------------=
1818
// MARK: Instances

Sources/DiffableTextKit/Models/Context.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,19 @@ public extension Context {
192192
//=--------------------------------------=
193193
// Update
194194
//=--------------------------------------=
195-
let (result, update) = self.status + status
195+
var next = self.status
196+
let changes = next.merge(status)
196197
//=--------------------------------------=
197198
// At Least One Must Be Different
198199
//=--------------------------------------=
199-
return (!update.isEmpty).on(true) {
200-
self.merge(Self.init(result))
200+
let update = !changes.isEmpty
201+
if update {
202+
self.merge(Self.init(next))
201203
}
204+
//=--------------------------------------=
205+
// Return
206+
//=--------------------------------------=
207+
return update
202208
}
203209

204210
//=------------------------------------------------------------------------=

Sources/DiffableTextKit/Models/Status.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public struct Status<Style: DiffableTextStyle> {
3737
// MARK: Transformations
3838
//=------------------------------------------------------------------------=
3939

40-
@inlinable public mutating func merge(_ other: Self) -> Update {
40+
@inlinable public mutating func merge(_ other: Self) -> Changes {
4141
let update = (self .!= other)
4242
//=--------------------------------------=
4343
// Update
@@ -50,13 +50,13 @@ public struct Status<Style: DiffableTextStyle> {
5050
//=--------------------------------------=
5151
return update
5252
}
53-
53+
5454
//=------------------------------------------------------------------------=
5555
// MARK: Utilities
5656
//=------------------------------------------------------------------------=
5757

58-
@inlinable static func .!= (lhs: Self, rhs: Self) -> Update {
59-
var update = Update()
58+
@inlinable static func .!= (lhs: Self, rhs: Self) -> Changes {
59+
var update = Changes()
6060
//=--------------------------------------=
6161
// Compare
6262
//=--------------------------------------=
@@ -68,8 +68,4 @@ public struct Status<Style: DiffableTextStyle> {
6868
//=--------------------------------------=
6969
return update
7070
}
71-
72-
@inlinable static func + (lhs: Self, rhs: Self) -> (Self, Update) {
73-
var result = lhs; let update = result.merge(rhs); return (result, update)
74-
}
7571
}

Sources/DiffableTextKit/Support/Bool.swift

Lines changed: 0 additions & 24 deletions
This file was deleted.

Sources/DiffableTextStylesXNumber/Models/Bounds.swift

Lines changed: 101 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,69 @@ public struct NumberTextBounds<Value: NumberTextValue>: Equatable {
1919
// MARK: State
2020
//=------------------------------------------------------------------------=
2121

22-
@usableFromInline let min: Value
23-
@usableFromInline let max: Value
24-
22+
@usableFromInline var bounds: ClosedRange<Value>
23+
2524
//=------------------------------------------------------------------------=
2625
// MARK: Initializers
2726
//=------------------------------------------------------------------------=
2827

29-
@inlinable init(unchecked: (min: Value, max: Value)) {
30-
(self.min, self.max) = unchecked; precondition(min <= max)
28+
@inlinable init(unchecked: (lower: Value, upper: Value)) {
29+
self.bounds = ClosedRange(uncheckedBounds: unchecked)
30+
}
31+
32+
//=------------------------------------------------------------------------=
33+
// MARK: Accessors
34+
//=------------------------------------------------------------------------=
35+
36+
@inlinable var min: Value {
37+
bounds.lowerBound
38+
}
39+
40+
@inlinable var max: Value {
41+
bounds.upperBound
42+
}
43+
44+
//=------------------------------------------------------------------------=
45+
// MARK: Utilities
46+
//=------------------------------------------------------------------------=
47+
48+
@inlinable func location(of value: Value) -> Location? {
49+
//=--------------------------------------=
50+
// Value Is Not Maxed Out
51+
//=--------------------------------------=
52+
if min < value && value < max { return .body }
53+
//=--------------------------------------=
54+
// Value == Max
55+
//=--------------------------------------=
56+
if value == max { return Location(edge: value > .zero || min == max) }
57+
//=--------------------------------------=
58+
// Value == Min
59+
//=--------------------------------------=
60+
if value == min { return Location(edge: value < .zero) }
61+
//=--------------------------------------=
62+
// Value Is Out Of Bounds
63+
//=--------------------------------------=
64+
return nil
65+
}
66+
67+
//*========================================================================*
68+
// MARK: Declaration
69+
//*========================================================================*
70+
71+
@usableFromInline enum Location {
72+
73+
//=--------------------------------------------------------------------=
74+
// MARK: Instances
75+
//=--------------------------------------------------------------------=
76+
77+
case body
78+
case edge
79+
80+
//=--------------------------------------------------------------------=
81+
// MARK: Initializers
82+
//=--------------------------------------------------------------------=
83+
84+
@inlinable init(edge: Bool) { self = edge ? .edge : .body }
3185
}
3286
}
3387

@@ -99,21 +153,28 @@ extension NumberTextBounds {
99153
//=------------------------------------------------------------------------=
100154

101155
@inlinable func autocorrect(_ value: inout Value) {
102-
value = Swift.min(Swift.max(min, value), max)
156+
//=--------------------------------------=
157+
// Lower Bound
158+
//=--------------------------------------=
159+
if value < min {
160+
Info.print(autocorrection: [.mark(value), "<", .note(min)])
161+
value = min; return
162+
}
163+
//=--------------------------------------=
164+
// Upper Bound
165+
//=--------------------------------------=
166+
if value > max {
167+
Info.print(autocorrection: [.mark(value), ">", .note(max)])
168+
value = max; return
169+
}
103170
}
104171

105172
//=------------------------------------------------------------------------=
106173
// MARK: Number
107174
//=------------------------------------------------------------------------=
108175

109-
@inlinable func autocorrect(_ number: inout Number) {
110-
autocorrect(&number.sign)
111-
}
112-
113-
/// Toggles the sign if the opposite sign is the only sign allowed.
114-
@inlinable func autocorrect(_ sign: inout Sign) {
115-
if (sign == .positive && max <= .zero && min != .zero )
116-
|| (sign == .negative && min >= .zero) { sign.toggle() }
176+
@inlinable func autocorrect(_ number: inout Number) {
177+
autocorrectSignOnUnambigiousBounds(&number.sign)
117178
}
118179
}
119180

@@ -128,40 +189,42 @@ extension NumberTextBounds {
128189
//=------------------------------------------------------------------------=
129190

130191
@inlinable func autovalidate(_ number: inout Number) throws {
131-
try autovalidate(&number.sign)
132-
}
133-
134-
@inlinable func autovalidate(_ sign: inout Sign) throws {
135-
let autocorrected = sign.transformed(autocorrect)
136-
if autocorrected != sign {
137-
Info.print(autocorrection: [.mark(sign), "is not in", .mark(self)])
138-
sign = autocorrected
139-
}
192+
autocorrectSignOnUnambigiousBounds(&number.sign)
140193
}
141194

142195
//=------------------------------------------------------------------------=
143196
// MARK: Value
144197
//=------------------------------------------------------------------------=
145198

146199
@inlinable func autovalidate(_ value: Value, _ number: inout Number) throws {
147-
if try edge(value), number.removeSeparatorAsSuffix() {
148-
Info.print(autocorrection: [.mark(number), "does not fit a fraction separator"])
149-
}
150-
}
151-
152-
@inlinable func edge(_ value: Value) throws -> Bool {
153-
if min < value && value < max { return false }
154-
//=--------------------------------------=
155-
// Value == Max
156200
//=--------------------------------------=
157-
if value == max { return value > .zero || min == max }
201+
// Location
158202
//=--------------------------------------=
159-
// Value == Min
160-
//=--------------------------------------=
161-
if value == min { return value < .zero }
203+
guard let location = location(of: value) else {
204+
throw Info([.mark(value), "is not in", .note(self)])
205+
}
162206
//=--------------------------------------=
163-
// Value Is Out Of Bounds
207+
// Remove Separator On Value Is Maxed Out
164208
//=--------------------------------------=
165-
throw Info([.mark(value), "is not in", .mark(self)])
209+
if location == .edge, number.removeSeparatorAsSuffix() {
210+
Info.print(autocorrection: [.mark(number), "does not fit a fraction separator"])
211+
}
212+
}
213+
}
214+
215+
//=----------------------------------------------------------------------------=
216+
// MARK: Upstream, Downstream
217+
//=----------------------------------------------------------------------------=
218+
219+
extension NumberTextBounds {
220+
221+
//=------------------------------------------------------------------------=
222+
// MARK: Number
223+
//=------------------------------------------------------------------------=
224+
225+
@inlinable func autocorrectSignOnUnambigiousBounds(_ sign: inout Sign) {
226+
guard let correct = Sign(of: bounds), sign != correct else { return }
227+
Info.print(autocorrection: [.mark(sign), "is not in", .note(self)])
228+
sign = correct
166229
}
167230
}

Sources/DiffableTextStylesXNumber/Models/Precision.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ extension NumberTextPrecision {
170170
// Validate Each Component
171171
//=--------------------------------------=
172172
if let component = capacity.first(where: (.<, 0)) {
173-
throw Info([.mark(component), "digits exceed max precision", .mark(upper[component])])
173+
throw Info([.mark(component), "digits exceed max precision", .note(upper[component])])
174174
}
175175
//=--------------------------------------=
176176
// Return

Sources/DiffableTextStylesXNumber/Number/Glyphs.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
//*============================================================================*
1313

1414
/// An object representing multiple UTF-8 encoded ASCII characters.
15-
@usableFromInline protocol Glyphs {
15+
@usableFromInline protocol Glyphs: CustomStringConvertible {
1616

1717
//=------------------------------------------------------------------------=
1818
// MARK: State

Sources/DiffableTextStylesXNumber/Number/Number.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ extension Number {
9393
//=------------------------------------------------------------------------=
9494

9595
/// To use this method, all formatting characters must be marked as virtual.
96-
@inlinable init?<T>(parse snapshot: Snapshot, with lexicon: Lexicon, as kind: T.Type)
97-
throws where T: NumberTextKind {
96+
@inlinable init?<T>(parse snapshot: Snapshot,
97+
with lexicon: Lexicon, as kind: T.Type) throws where T: NumberTextKind {
9898
let sequence = snapshot.lazy.filter(\.nonvirtual).map(\.character)
9999
try self.init(unformatted: sequence,
100100
optional: kind.isOptional,
@@ -184,6 +184,6 @@ extension Number {
184184
//=--------------------------------------=
185185
// Finalize
186186
//=--------------------------------------=
187-
self.integer.removeZerosAsPrefix(); self.integer.makeAtLeastZero()
187+
self.integer.removeZerosAsPrefix(); self.integer.makeAtLeastZero()
188188
}
189189
}

Sources/DiffableTextStylesXNumber/Number/Sign.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ import Foundation
3636
self.rawValue = enumeration.rawValue
3737
}
3838

39+
//=------------------------------------------------------------------------=
40+
// MARK: Initializers
41+
//=------------------------------------------------------------------------=
42+
43+
@inlinable init?<T>(of bounds: ClosedRange<T>) where T: NumberTextValue {
44+
if bounds.lowerBound >= .zero { self = .positive }
45+
else if bounds.upperBound <= .zero && bounds.lowerBound != .zero { self = .negative }
46+
else { return nil }
47+
}
48+
3949
//=------------------------------------------------------------------------=
4050
// MARK: Transformations
4151
//=------------------------------------------------------------------------=
@@ -50,7 +60,7 @@ import Foundation
5060
case .negative: return .positive
5161
}
5262
}
53-
63+
5464
//=------------------------------------------------------------------------=
5565
// MARK: Localization
5666
//=------------------------------------------------------------------------=

Sources/DiffableTextStylesXPattern/Style.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ extension PatternTextStyle {
144144
// Capacity
145145
//=--------------------------------------=
146146
guard contents.next() == nil else {
147-
throw Info([.mark(proposal.characters), "exceeded pattern capacity", .mark(value.count)])
147+
throw Info([.mark(proposal.characters), "exceeded pattern capacity", .note(value.count)])
148148
}
149149
//=--------------------------------------=
150150
// Value -> Commit

0 commit comments

Comments
 (0)