Skip to content

Commit a016073

Browse files
committed
Cleanup, NumbericTextStyles: merged (Bounds, Precision)+[branch] files and moved Scheme files up a level.
1 parent 70e40fa commit a016073

File tree

9 files changed

+145
-195
lines changed

9 files changed

+145
-195
lines changed

Sources/DiffableTextStylesXNumeric/Models/Bounds+Autocorrect.swift

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

Sources/DiffableTextStylesXNumeric/Models/Bounds+Autovalidate.swift

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

Sources/DiffableTextStylesXNumeric/Models/Bounds.swift

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// See http://www.apache.org/licenses/LICENSE-2.0 for license information.
88
//=----------------------------------------------------------------------------=
99

10+
import DiffableTextKit
11+
1012
//*============================================================================*
1113
// MARK: * Bounds
1214
//*============================================================================*
@@ -85,3 +87,92 @@ extension Bounds: CustomStringConvertible {
8587
"\(min) to \(max)"
8688
}
8789
}
90+
91+
//=----------------------------------------------------------------------------=
92+
// MARK: + Upstream - Autocorrect
93+
//=----------------------------------------------------------------------------=
94+
95+
extension Bounds {
96+
97+
//=------------------------------------------------------------------------=
98+
// MARK: Value
99+
//=------------------------------------------------------------------------=
100+
101+
@inlinable func autocorrect(_ value: inout Value) {
102+
value = Swift.min(Swift.max(min, value), max)
103+
}
104+
105+
//=------------------------------------------------------------------------=
106+
// MARK: Number
107+
//=------------------------------------------------------------------------=
108+
109+
@inlinable func autocorrect(_ number: inout Number) {
110+
autocorrect(&number.sign)
111+
}
112+
113+
//=------------------------------------------------------------------------=
114+
// MARK: Number - Helpers
115+
//=------------------------------------------------------------------------=
116+
117+
@inlinable func autocorrect(_ sign: inout Sign) {
118+
switch sign {
119+
case .positive: if max <= .zero, min != .zero { sign.toggle() }
120+
case .negative: if min >= .zero { sign.toggle() }
121+
}
122+
}
123+
}
124+
125+
//=----------------------------------------------------------------------------=
126+
// MARK: + Downstream - Autovalidate
127+
//=----------------------------------------------------------------------------=
128+
129+
extension Bounds {
130+
131+
//=------------------------------------------------------------------------=
132+
// MARK: Number
133+
//=------------------------------------------------------------------------=
134+
135+
@inlinable func autovalidate(_ number: Number) throws {
136+
try autovalidate(number.sign)
137+
}
138+
139+
//=------------------------------------------------------------------------=
140+
// MARK: Number - Helpers
141+
//=------------------------------------------------------------------------=
142+
143+
@inlinable func autovalidate(_ sign: Sign) throws {
144+
guard sign == sign.transformed(autocorrect) else {
145+
throw Info([.mark(sign), "is not in", .mark(self)])
146+
}
147+
}
148+
149+
//=------------------------------------------------------------------------=
150+
// MARK: Value
151+
//=------------------------------------------------------------------------=
152+
153+
@inlinable func autovalidate(_ value: Value, _ number: inout Number) throws {
154+
if try edge(value), number.removeSeparatorAsSuffix() {
155+
Info.print([.autocorrection, .mark(number), "does not fit a fraction separator"])
156+
}
157+
}
158+
159+
//=------------------------------------------------------------------------=
160+
// MARK: Value - Helpers
161+
//=------------------------------------------------------------------------=
162+
163+
@inlinable func edge(_ value: Value) throws -> Bool {
164+
if min < value && value < max { return false }
165+
//=--------------------------------------=
166+
// MARK: Value == Max
167+
//=--------------------------------------=
168+
if value == max { return value > .zero || min == max }
169+
//=--------------------------------------=
170+
// MARK: Value == Min
171+
//=--------------------------------------=
172+
if value == min { return value < .zero }
173+
//=--------------------------------------=
174+
// MARK: Value Is Out Of Bounds
175+
//=--------------------------------------=
176+
throw Info([.mark(value), "is not in", .mark(self)])
177+
}
178+
}

Sources/DiffableTextStylesXNumeric/Models/Precision+Autocorrect.swift

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

Sources/DiffableTextStylesXNumeric/Models/Precision+Autovalidate.swift

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

Sources/DiffableTextStylesXNumeric/Models/Precision.swift

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,60 @@ extension Precision: Brrr {
137137
}
138138
}
139139

140+
//=----------------------------------------------------------------------------=
141+
// MARK: + Upstream - Autocorrect
142+
//=----------------------------------------------------------------------------=
143+
144+
extension Precision {
145+
146+
//=------------------------------------------------------------------------=
147+
// MARK: Number
148+
//=------------------------------------------------------------------------=
149+
150+
@inlinable func autocorrect(_ number: inout Number) {
151+
number.trim(max: upper)
152+
}
153+
}
154+
155+
//=----------------------------------------------------------------------------=
156+
// MARK: + Downstream - Autovalidate
157+
//=----------------------------------------------------------------------------=
158+
159+
extension Precision {
160+
161+
//=------------------------------------------------------------------------=
162+
// MARK: Number
163+
//=------------------------------------------------------------------------=
164+
165+
@inlinable func autovalidate(_ number: inout Number, _ count: Count) throws {
166+
let capacity = try capacity(count)
167+
//=--------------------------------------=
168+
// MARK: Autocorrect
169+
//=--------------------------------------=
170+
if capacity.fraction <= 0 || capacity.value <= 0, number.removeSeparatorAsSuffix() {
171+
Info.print([.autocorrection, .mark(number), "does not fit a fraction separator"])
172+
}
173+
}
174+
175+
//=------------------------------------------------------------------------=
176+
// MARK: Number - Helpers
177+
//=------------------------------------------------------------------------=
178+
179+
@inlinable func capacity(_ count: Count) throws -> Count {
180+
let capacity = upper.map((&-, count))
181+
//=--------------------------------------=
182+
// MARK: Validate Each Component
183+
//=--------------------------------------=
184+
if let component = capacity.first(where: (.<, 0)) {
185+
throw Info([.mark(component), "digits exceed max precision", .mark(upper[component])])
186+
}
187+
//=--------------------------------------=
188+
// MARK: Done
189+
//=--------------------------------------=
190+
return capacity
191+
}
192+
}
193+
140194
//*============================================================================*
141195
// MARK: * Precision x Namespace
142196
//*============================================================================*

Sources/DiffableTextStylesXNumeric/Models/Scheme+Currency.swift renamed to Sources/DiffableTextStylesXNumeric/Scheme+Currency.swift

File renamed without changes.

Sources/DiffableTextStylesXNumeric/Models/Scheme+Standard.swift renamed to Sources/DiffableTextStylesXNumeric/Scheme+Standard.swift

File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)