From dce89b2fa8885c4613763b3f765fc061028798ba Mon Sep 17 00:00:00 2001 From: sperber Date: Fri, 22 Feb 2002 10:22:38 +0000 Subject: [PATCH] Translate submatches according to CRE:TVEC, fixing submatch bug reported by Michel Schinz. --- scsh/rx/re-low.scm | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/scsh/rx/re-low.scm b/scsh/rx/re-low.scm index aa33ac2..a138ae7 100644 --- a/scsh/rx/re-low.scm +++ b/scsh/rx/re-low.scm @@ -83,7 +83,29 @@ (if (not ret) #f (make-regexp-match str - (list->vector ret)))))))) + (translate-submatches ret + (cre:tvec cre) + start-vec)))))))) + +(define (translate-submatches matches trans-vec match-vec) + (let ((n-virtual-submatches (vector-length trans-vec))) + (let loop ((virtual-index 0) + (match-index 0) + (matches matches)) + (cond + ((> virtual-index n-virtual-submatches) + match-vec) + ((if (zero? virtual-index) + 0 + (vector-ref trans-vec (- virtual-index 1))) + => (lambda (actual-index) + (if (= match-index actual-index) + (begin + (vector-set! match-vec virtual-index (car matches)) + (loop (+ 1 virtual-index) (+ 1 match-index) (cdr matches))) + (loop virtual-index (+ 1 match-index) (cdr matches))))) + (else + (loop (+ 1 virtual-index) match-index matches)))))) (define (cre-search? cre str start) (let ((re-str (cre:string cre))) ;; RE-STR = #F => empty match.