~ chicken-core (chicken-5) 6ca6044dc6fca14b471203a16fe728abba71397d


commit 6ca6044dc6fca14b471203a16fe728abba71397d
Author:     Peter Bex <peter.bex@xs4all.nl>
AuthorDate: Sun Nov 17 11:37:56 2013 +0100
Commit:     Evan Hanson <evhan@foldling.org>
CommitDate: Tue Nov 19 09:57:00 2013 +1300

    Irregex: Fix #1066: submatches in negative look-behind work with chunked strings
    
    This includes upstream changesets 60eb93968828, 13211f6af649 and a3abbe85fd00.
    
    Signed-off-by: Evan Hanson <evhan@foldling.org>

diff --git a/irregex-core.scm b/irregex-core.scm
index c0886655..310810a0 100644
--- a/irregex-core.scm
+++ b/irregex-core.scm
@@ -397,6 +397,17 @@
                   #t
                   (chunk-before? cnk next b))))))
 
+;; For look-behind searches, wrap an existing chunker such that it
+;; returns the same results but ends at a given point.
+(define (wrap-end-chunker cnk src i)
+  (make-irregex-chunker
+   (lambda (x) (and (not (eq? x src)) ((chunker-get-next cnk) x)))
+   (chunker-get-str cnk)
+   (chunker-get-start cnk)
+   (lambda (x) (if (eq? x src) i ((chunker-get-end cnk) x)))
+   (chunker-get-substring cnk)
+   (chunker-get-subchunk cnk)))
+
 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ;;;; String Utilities
 
@@ -3267,16 +3278,13 @@
                         flags
                         (lambda (cnk init src str i end matches fail) i))))
                (lambda (cnk init src str i end matches fail)
-                 (let* ((prev ((chunker-get-substring cnk)
-                               (car init)
-                               (cdr init)
-                               src
-                               i))
-                        (len (string-length prev))
-                        (src2 (list prev 0 len)))
+                 (let* ((cnk* (wrap-end-chunker cnk src i))
+                        (str* ((chunker-get-str cnk*) (car init)))
+                        (i* (cdr init))
+                        (end* ((chunker-get-end cnk*) (car init))))
                    (if ((if (eq? (car sre) 'look-behind) (lambda (x) x) not)
-                        (check irregex-basic-string-chunker
-                               (cons src2 0) src2 prev 0 len matches (lambda () #f)))
+                        (check cnk* init (car init) str* i* end* matches
+                               (lambda () #f)))
                        (next cnk init src str i end matches fail)
                        (fail))))))
             ((atomic)
diff --git a/tests/re-tests.txt b/tests/re-tests.txt
index 7b233572..37e951a7 100644
--- a/tests/re-tests.txt
+++ b/tests/re-tests.txt
@@ -97,6 +97,22 @@ ab*	xayabbbz	y	&	a
 (a|b)c*d	abcd	y	&-\1	bcd-b
 (ab|ab*)bc	abc	y	&-\1	abc-a
 (?:(a)b|ac)	ac	y	&-\1	ac-
+a(?=(b|c))	ab	y	&-\1	a-b
+a(?=(b|c))	a	n	-	-
+a(?=(b|c))	ax	n	-	-
+a(?=(b|c))bc	abc	y	&-\1	abc-b
+a(?!(b|c))	ax	y	&-\1	a-
+a(?!(b|c))	a	y	&-\1	a-
+a(?!(b|c))	ab	n	-	-
+a(?!(b|c))xc	axc	y	&-\1	axc-
+(a|b)(?<=(a))c	ac	y	&-\1-\2	ac-a-a
+(a|b)(?<=(a))c	bc	n	-	-
+(?<=(a))bc	bc	n	-	-
+.(?<=(a))bc	abc	y	&-\1	abc-a
+(a|b)(?<!(a))c	ac	n	-	-
+(a|b)(?<!(a))c	bc	y	&-\1-\2	bc-b-
+.(?<!(a))bc	abc	n	-	-
+(?<!(a))bc	bc	y	&-\1	bc-
 a([bc]*)c*	abc	y	&-\1	abc-bc
 a([bc]*)(c*d)	abcd	y	&-\1-\2	abcd-bc-d
 a([bc]+)(c*d)	abcd	y	&-\1-\2	abcd-bc-d
Trap