Skip to content

Commit 630ca64

Browse files
committed
more refactoring
1 parent eb1f505 commit 630ca64

File tree

2 files changed

+21
-73
lines changed

2 files changed

+21
-73
lines changed

Sources/_StringProcessing/Engine/Backtracking.swift

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ extension Processor {
1515
var pos: Position?
1616

1717
// Quantifiers may store a range of positions to restore to
18-
var rangeStart: Position?
19-
var rangeEnd: Position?
18+
var quantifiedRange: Range<Position>?
2019

2120
// FIXME: refactor, for now this field is only used for quantifier save
2221
// points. We should try to separate out the concerns better.
@@ -53,43 +52,28 @@ extension Processor {
5352
// Whether this save point is quantified, meaning it has a range of
5453
// possible positions to explore.
5554
var isQuantified: Bool {
56-
if rangeEnd == nil {
57-
assert(rangeStart == nil)
58-
return false
59-
}
60-
assert(rangeStart != nil)
61-
return true
62-
}
63-
64-
mutating func updateRange(newEnd: Input.Index) {
65-
if rangeStart == nil {
66-
assert(rangeEnd == nil)
67-
rangeStart = newEnd
68-
}
69-
rangeEnd = newEnd
55+
quantifiedRange != nil
7056
}
7157

7258
/// Move the next range position into pos, and removing it from the range
7359
mutating func takePositionFromRange(_ input: Input) {
7460
assert(isQuantified)
75-
pos = rangeEnd!
76-
shrinkRange(input)
77-
}
61+
let range = quantifiedRange!
62+
pos = range.upperBound
63+
if range.isEmpty {
64+
// Becomes a normal save point
65+
quantifiedRange = nil
66+
return
67+
}
7868

79-
/// Shrink the range of the save point by one index, essentially dropping the last index
80-
mutating func shrinkRange(_ input: Input) {
81-
assert(isQuantified)
82-
if rangeEnd == rangeStart {
83-
// The range is now empty
84-
rangeStart = nil
85-
rangeEnd = nil
69+
// Shrink the range
70+
let newUpper: Position
71+
if isScalarSemantics {
72+
newUpper = input.unicodeScalars.index(before: range.upperBound)
8673
} else {
87-
if isScalarSemantics {
88-
input.unicodeScalars.formIndex(before: &rangeEnd!)
89-
} else {
90-
input.formIndex(before: &rangeEnd!)
91-
}
74+
newUpper = input.index(before: range.upperBound)
9275
}
76+
quantifiedRange = range.lowerBound..<newUpper
9377
}
9478
}
9579

@@ -99,8 +83,7 @@ extension Processor {
9983
SavePoint(
10084
pc: pc,
10185
pos: currentPosition,
102-
rangeStart: nil,
103-
rangeEnd: nil,
86+
quantifiedRange: nil,
10487
isScalarSemantics: false,
10588
stackEnd: .init(callStack.count),
10689
captureEnds: storedCaptures,
@@ -114,8 +97,7 @@ extension Processor {
11497
SavePoint(
11598
pc: pc,
11699
pos: nil,
117-
rangeStart: nil,
118-
rangeEnd: nil,
100+
quantifiedRange: nil,
119101
isScalarSemantics: false,
120102
stackEnd: .init(callStack.count),
121103
captureEnds: storedCaptures,
@@ -130,42 +112,7 @@ extension Processor {
130112
SavePoint(
131113
pc: controller.pc + 1,
132114
pos: nil,
133-
rangeStart: range.lowerBound,
134-
rangeEnd: range.upperBound,
135-
isScalarSemantics: isScalarSemantics,
136-
stackEnd: .init(callStack.count),
137-
captureEnds: storedCaptures,
138-
intRegisters: registers.ints,
139-
posRegisters: registers.positions)
140-
}
141-
//
142-
// func makeSavePoint(
143-
// resumeAt pc: InstructionAddress? = nil,
144-
// quantifiedRange: Range<Position>? = nil,
145-
// addressOnly: Bool = false,
146-
// isScalarSemantics: Bool = false
147-
// ) -> SavePoint {
148-
// SavePoint(
149-
// pc: pc ?? controller.pc + 1,
150-
// pos: addressOnly ? nil : currentPosition,
151-
// rangeStart: quantifiedRange?.lowerBound,
152-
// rangeEnd: quantifiedRange?.lowerBound,
153-
// isScalarSemantics: false, // FIXME: refactor away
154-
// stackEnd: .init(callStack.count),
155-
// captureEnds: storedCaptures,
156-
// intRegisters: registers.ints,
157-
// posRegisters: registers.positions)
158-
// }
159-
160-
func startQuantifierSavePoint(
161-
isScalarSemantics: Bool
162-
) -> SavePoint {
163-
// Restores to the instruction AFTER the current quantifier instruction
164-
SavePoint(
165-
pc: controller.pc + 1,
166-
pos: nil,
167-
rangeStart: nil,
168-
rangeEnd: nil,
115+
quantifiedRange: range,
169116
isScalarSemantics: isScalarSemantics,
170117
stackEnd: .init(callStack.count),
171118
captureEnds: storedCaptures,

Sources/_StringProcessing/Engine/Tracing.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,9 @@ extension Processor.SavePoint {
121121
if !isQuantified {
122122
posStr = "<none>"
123123
} else {
124-
let startStr = "\(input.distance(from: input.startIndex, to: rangeStart!))"
125-
let endStr = "\(input.distance(from: input.startIndex, to: rangeEnd!))"
124+
let range = quantifiedRange!
125+
let startStr = "\(input.distance(from: input.startIndex, to: range.lowerBound))"
126+
let endStr = "\(input.distance(from: input.startIndex, to: range.upperBound))"
126127
posStr = "\(startStr)...\(endStr)"
127128
}
128129
}

0 commit comments

Comments
 (0)