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.