1 ;;;; Copyright (C) 1997, 1999 Free Software Foundation, Inc.
3 ;;;; This program is free software; you can redistribute it and/or modify
4 ;;;; it under the terms of the GNU General Public License as published by
5 ;;;; the Free Software Foundation; either version 2, or (at your option)
6 ;;;; any later version.
8 ;;;; This program is distributed in the hope that it will be useful,
9 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 ;;;; GNU General Public License for more details.
13 ;;;; You should have received a copy of the GNU General Public License
14 ;;;; along with this software; see the file COPYING. If not, write to
15 ;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
16 ;;;; Boston, MA 02111-1307 USA
19 ;;;; POSIX regex support functions.
21 (define-module (ice-9 regex))
24 ;;; It is not clear what should happen if a `match' function
25 ;;; is passed a `match number' which is out of bounds for the
26 ;;; regexp match: return #f, or throw an error? These routines
27 ;;; throw an out-of-range error.
29 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
30 ;;;; These procedures are not defined in SCSH, but I found them useful.
32 (define-public (match:count match)
33 (- (vector-length match) 1))
35 (define-public (match:string match)
38 (define-public (match:prefix match)
39 (substring (match:string match) 0 (match:start match 0)))
41 (define-public (match:suffix match)
42 (substring (match:string match) (match:end match 0)))
44 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
45 ;;;; SCSH compatibility routines.
47 (define-public (regexp-match? match)
49 (string? (vector-ref match 0))
51 (cond ((>= i (vector-length match)) #t)
52 ((and (pair? (vector-ref match i))
53 (integer? (car (vector-ref match i)))
54 (integer? (cdr (vector-ref match i))))
58 (define-public (regexp-quote regexp)
59 (call-with-output-string
62 (and (< i (string-length regexp))
64 (case (string-ref regexp i)
65 ((#\* #\. #\( #\) #\+ #\? #\\ #\^ #\$ #\{ #\})
67 (write-char (string-ref regexp i) p)
70 (define-public (match:start match . args)
71 (let* ((matchnum (if (pair? args)
74 (start (car (vector-ref match matchnum))))
75 (if (= start -1) #f start)))
77 (define-public (match:end match . args)
78 (let* ((matchnum (if (pair? args)
81 (end (cdr (vector-ref match matchnum))))
82 (if (= end -1) #f end)))
84 (define-public (match:substring match . args)
85 (let* ((matchnum (if (pair? args)
88 (start (match:start match matchnum))
89 (end (match:end match matchnum)))
90 (and start end (substring (match:string match) start end))))
92 (define-public (string-match pattern str . args)
93 (let ((rx (make-regexp pattern))
94 (start (if (pair? args) (car args) 0)))
95 (regexp-exec rx str start)))
97 (define-public (regexp-substitute port match . items)
98 ;; If `port' is #f, send output to a string.
100 (call-with-output-string
102 (apply regexp-substitute p match items)))
104 ;; Otherwise, process each substitution argument in `items'.
105 (for-each (lambda (obj)
106 (cond ((string? obj) (display obj port))
107 ((integer? obj) (display (match:substring match obj) port))
108 ((eq? 'pre obj) (display (match:prefix match) port))
109 ((eq? 'post obj) (display (match:suffix match) port))
110 (else (error 'wrong-type-arg obj))))
113 ;;; If we call fold-matches, below, with a regexp that can match the
114 ;;; empty string, it's not obvious what "all the matches" means. How
115 ;;; many empty strings are there in the string "a"? Our answer:
117 ;;; This function applies PROC to every non-overlapping, maximal
118 ;;; match of REGEXP in STRING.
120 ;;; "non-overlapping": There are two non-overlapping matches of "" in
121 ;;; "a" --- one before the `a', and one after. There are three
122 ;;; non-overlapping matches of "q|x*" in "aqb": the empty strings
123 ;;; before `a' and after `b', and `q'. The two empty strings before
124 ;;; and after `q' don't count, because they overlap with the match of
127 ;;; "maximal": There are three distinct maximal matches of "x*" in
128 ;;; "axxxb": one before the `a', one covering `xxx', and one after the
129 ;;; `b'. Around or within `xxx', only the match covering all three
130 ;;; x's counts, because the rest are not maximal.
132 (define-public (fold-matches regexp string init proc . flags)
133 (let ((regexp (if (regexp? regexp) regexp (make-regexp regexp)))
134 (flags (if (null? flags) 0 flags)))
137 (abuts #f)) ; True if start abuts a previous match.
138 (let ((m (if (> start (string-length string)) #f
139 (regexp-exec regexp string start flags))))
142 ((and (= (match:start m) (match:end m)) abuts)
143 ;; We matched an empty string, but that would overlap the
144 ;; match immediately before. Try again at a position
145 ;; further to the right.
146 (loop (+ start 1) value #f))
148 (loop (match:end m) (proc m value) #t)))))))
150 (define-public (list-matches regexp string . flags)
151 (reverse! (apply fold-matches regexp string '() cons flags)))
153 (define-public (regexp-substitute/global port regexp string . items)
155 ;; If `port' is #f, send output to a string.
157 (call-with-output-string
159 (apply regexp-substitute/global p regexp string items)))
161 ;; Walk the set of non-overlapping, maximal matches.
162 (let next-match ((matches (list-matches regexp string))
165 (display (substring string start) port)
166 (let ((m (car matches)))
168 ;; Process all of the items for this match. Don't use
169 ;; for-each, because we need to make sure 'post at the
170 ;; end of the item list is a tail call.
171 (let next-item ((items items))
173 (define (do-item item)
175 ((string? item) (display item port))
176 ((integer? item) (display (match:substring m item) port))
177 ((procedure? item) (display (item m) port))
180 (substring string start (match:start m))
183 (next-match (cdr matches) (match:end m)))
184 (else (error 'wrong-type-arg item))))
187 (if (null? (cdr items))
188 (do-item (car items)) ; This is a tail call.
190 (do-item (car items)) ; This is not.
191 (next-item (cdr items)))))))))))