Skip to content

Commit 3c87596

Browse files
committed
sink match onto string
1 parent 62a9fbe commit 3c87596

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,11 +232,13 @@ extension Processor {
232232
// Match against the current input element. Returns whether
233233
// it succeeded vs signaling an error.
234234
mutating func match(_ e: Element) -> Bool {
235-
guard let cur = load(), cur == e else {
235+
guard let next = input.match(
236+
e, at: currentPosition, limitedBy: end
237+
) else {
236238
signalFailure()
237239
return false
238240
}
239-
_uncheckedForcedConsumeOne()
241+
currentPosition = next
240242
return true
241243
}
242244

@@ -256,6 +258,7 @@ extension Processor {
256258
isScalarMode: Bool
257259
) -> Bool {
258260
if isScalarMode {
261+
// TODO: sink to specialized method on string, needs benchmark
259262
for s in seq.unicodeScalars {
260263
guard matchScalar(s, boundaryCheck: false) else { return false }
261264
}
@@ -702,6 +705,23 @@ func blackHole<T>(_ t: T) { _ = t }
702705

703706
extension String {
704707

708+
func match(
709+
_ char: Character,
710+
at pos: Index,
711+
limitedBy end: String.Index
712+
) -> Index? {
713+
// TODO: This can be greatly sped up with string internals
714+
// TODO: This is also very much quick-check-able
715+
assert(end <= endIndex)
716+
717+
guard pos < end, self[pos] == char else { return nil }
718+
719+
let idx = index(after: pos)
720+
guard idx <= end else { return nil }
721+
722+
return idx
723+
}
724+
705725
// func consumeScalar(_ n: Distance) -> Bool {
706726

707727
// }

0 commit comments

Comments
 (0)