Skip to content

Commit 1a4cde7

Browse files
committed
sink matchSeq
1 parent 670ff9b commit 1a4cde7

File tree

1 file changed

+30
-8
lines changed

1 file changed

+30
-8
lines changed

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ extension Processor {
180180
// Returns whether the advance succeeded. On failure, our
181181
// save point was restored
182182
mutating func consume(_ n: Distance) -> Bool {
183+
// TODO: need benchmark coverage
183184
guard let idx = input.index(
184185
currentPosition, offsetBy: n.rawValue, limitedBy: end
185186
) else {
@@ -192,6 +193,7 @@ extension Processor {
192193

193194
// Advances in unicode scalar view
194195
mutating func consumeScalar(_ n: Distance) -> Bool {
196+
// TODO: need benchmark coverage
195197
guard let idx = input.unicodeScalars.index(
196198
currentPosition, offsetBy: n.rawValue, limitedBy: end
197199
) else {
@@ -223,11 +225,6 @@ extension Processor {
223225
func load() -> Element? {
224226
currentPosition < end ? input[currentPosition] : nil
225227
}
226-
func load(count: Int) -> Input.SubSequence? {
227-
let slice = self.slice[currentPosition...].prefix(count)
228-
guard slice.count == count else { return nil }
229-
return slice
230-
}
231228

232229
// Match against the current input element. Returns whether
233230
// it succeeded vs signaling an error.
@@ -259,16 +256,21 @@ extension Processor {
259256
isScalarMode: Bool
260257
) -> Bool {
261258
if isScalarMode {
262-
// TODO: sink to specialized method on string, needs benchmark
259+
// TODO: needs benchmark coverage
263260
for s in seq.unicodeScalars {
264261
guard matchScalar(s, boundaryCheck: false) else { return false }
265262
}
266263
return true
267264
}
268265

269-
for e in seq {
270-
guard match(e) else { return false }
266+
guard let next = input.matchSeq(
267+
seq, at: currentPosition, limitedBy: end
268+
) else {
269+
signalFailure()
270+
return false
271271
}
272+
273+
currentPosition = next
272274
return true
273275
}
274276

@@ -327,6 +329,7 @@ extension Processor {
327329
mutating func matchBitsetScalar(
328330
_ bitset: DSLTree.CustomCharacterClass.AsciiBitset
329331
) -> Bool {
332+
// TODO: needs benchmark coverage
330333
guard let curScalar = loadScalar(),
331334
bitset.matches(scalar: curScalar),
332335
let idx = input.unicodeScalars.index(currentPosition, offsetBy: 1, limitedBy: end) else {
@@ -718,6 +721,25 @@ extension String {
718721
return idx
719722
}
720723

724+
func matchSeq(
725+
_ seq: Substring,
726+
at pos: Index,
727+
limitedBy end: Index
728+
) -> Index? {
729+
// TODO: This can be greatly sped up with string internals
730+
// TODO: This is also very much quick-check-able
731+
assert(end <= endIndex)
732+
733+
var cur = pos
734+
for e in seq {
735+
guard cur < end, self[cur] == e else { return nil }
736+
self.formIndex(after: &cur)
737+
}
738+
739+
guard cur <= end else { return nil }
740+
return cur
741+
}
742+
721743
// func consumeScalar(_ n: Distance) -> Bool {
722744

723745
// }

0 commit comments

Comments
 (0)