Skip to content

Commit 216eee0

Browse files
committed
Improve lower bound handling for reverse matches
1 parent f211b6d commit 216eee0

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

Sources/_StringProcessing/Engine/Processor.swift

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,15 +301,24 @@ extension Processor {
301301
mutating func reverseMatch(
302302
_ e: Element, isCaseInsensitive: Bool
303303
) -> Bool {
304-
guard let previous = input.reverseMatch(
304+
let previous = input.reverseMatch(
305305
e,
306306
at: currentPosition,
307307
limitedBy: start,
308308
isCaseInsensitive: isCaseInsensitive
309-
) else {
310-
signalFailure()
311-
return false
309+
)
310+
311+
guard let previous else {
312+
guard currentPosition == start else {
313+
// If there's no previous character, and we're not
314+
// at the start of the string, the match has failed
315+
signalFailure()
316+
return false
317+
}
318+
319+
return true
312320
}
321+
313322
currentPosition = previous
314323
return true
315324
}
@@ -358,16 +367,23 @@ extension Processor {
358367
boundaryCheck: Bool,
359368
isCaseInsensitive: Bool
360369
) -> Bool {
361-
guard let previous = input.reverseMatchScalar(
370+
let previous = input.reverseMatchScalar(
362371
s,
363372
at: currentPosition,
364373
limitedBy: start,
365374
boundaryCheck: boundaryCheck,
366375
isCaseInsensitive: isCaseInsensitive
367-
) else {
368-
signalFailure()
369-
return false
376+
)
377+
378+
guard let previous else {
379+
guard currentPosition == start else {
380+
signalFailure()
381+
return false
382+
}
383+
384+
return true
370385
}
386+
371387
currentPosition = previous
372388
return true
373389
}

0 commit comments

Comments
 (0)