- if (scan->flags) {
- char * const s = HOPBACKc(locinput, scan->flags);
- if (!s) {
- /* trivial fail */
- if (logical) {
- logical = 0;
- sw = 1 - cBOOL(ST.wanted);
- }
- else if (ST.wanted)
- sayNO;
- next = scan + ARG(scan);
- if (next == scan)
- next = NULL;
- break;
- }
- newstart = s;
+ ST.count = scan->next_off + 1; /* next_off repurposed to be
+ lookbehind count, requires
+ non-zero flags */
+ if (! scan->flags) { /* 'flags' zero means lookahed */
+
+ /* Lookahead starts here and ends at the normal place */
+ ST.start = locinput;
+ ST.end = loceol;
+ }
+ else {
+ PERL_UINT_FAST8_T back_count = scan->flags;
+ char * s;
+
+ /* Lookbehind can look beyond the current position */
+ ST.end = loceol;
+
+ /* ... and starts at the first place in the input that is in
+ * the range of the possible start positions */
+ for (; ST.count > 0; ST.count--, back_count--) {
+ s = HOPBACKc(locinput, back_count);
+ if (s) {
+ ST.start = s;
+ goto do_ifmatch;
+ }
+ }
+
+ /* If the lookbehind doesn't start in the actual string, is a
+ * trivial match failure */
+ if (logical) {
+ logical = 0;
+ sw = 1 - cBOOL(ST.wanted);
+ }
+ else if (ST.wanted)
+ sayNO;
+
+ /* Here, we didn't want it to match, so is actually success */
+ next = scan + ARG(scan);
+ if (next == scan)
+ next = NULL;
+ break;