Translate submatches according to CRE:TVEC, fixing submatch bug

reported by Michel Schinz.
This commit is contained in:
sperber 2002-02-22 10:22:38 +00:00
parent 5f936e3a80
commit dce89b2fa8
1 changed files with 23 additions and 1 deletions

View File

@ -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.