mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-18 02:00:20 +02:00
(regexp-quote): [ and | must be quoted. Quote ( ) { + ?
using char class [(] etc since \( in fact makes them become special in regexp/basic.
This commit is contained in:
parent
95bcfff75f
commit
2b28ce5b0c
1 changed files with 29 additions and 3 deletions
|
@ -43,6 +43,11 @@
|
||||||
string-match regexp-substitute fold-matches list-matches
|
string-match regexp-substitute fold-matches list-matches
|
||||||
regexp-substitute/global))
|
regexp-substitute/global))
|
||||||
|
|
||||||
|
;; References:
|
||||||
|
;;
|
||||||
|
;; POSIX spec:
|
||||||
|
;; http://www.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap09.html
|
||||||
|
|
||||||
;;; FIXME:
|
;;; FIXME:
|
||||||
;;; It is not clear what should happen if a `match' function
|
;;; It is not clear what should happen if a `match' function
|
||||||
;;; is passed a `match number' which is out of bounds for the
|
;;; is passed a `match number' which is out of bounds for the
|
||||||
|
@ -78,6 +83,21 @@
|
||||||
(loop (+ 1 i)))
|
(loop (+ 1 i)))
|
||||||
(else #f)))))
|
(else #f)))))
|
||||||
|
|
||||||
|
;; * . \ ^ $ and [ are special in both regexp/basic and regexp/extended and
|
||||||
|
;; can be backslash escaped.
|
||||||
|
;;
|
||||||
|
;; ( ) + ? { } and | are special in regexp/extended so must be quoted. But
|
||||||
|
;; that can't be done with a backslash since in regexp/basic where they're
|
||||||
|
;; not special, adding a backslash makes them become special. Character
|
||||||
|
;; class forms [(] etc are used instead.
|
||||||
|
;;
|
||||||
|
;; ) is not special when not preceded by a (, and * and ? are not special at
|
||||||
|
;; the start of a string, but we quote all of these always, so the result
|
||||||
|
;; can be concatenated or merged into some larger regexp.
|
||||||
|
;;
|
||||||
|
;; ] is not special outside a [ ] character class, so doesn't need to be
|
||||||
|
;; quoted.
|
||||||
|
;;
|
||||||
(define (regexp-quote string)
|
(define (regexp-quote string)
|
||||||
(call-with-output-string
|
(call-with-output-string
|
||||||
(lambda (p)
|
(lambda (p)
|
||||||
|
@ -85,9 +105,15 @@
|
||||||
(and (< i (string-length string))
|
(and (< i (string-length string))
|
||||||
(begin
|
(begin
|
||||||
(case (string-ref string i)
|
(case (string-ref string i)
|
||||||
((#\* #\. #\( #\) #\+ #\? #\\ #\^ #\$ #\{ #\})
|
((#\* #\. #\\ #\^ #\$ #\[)
|
||||||
(write-char #\\ p)))
|
(write-char #\\ p)
|
||||||
(write-char (string-ref string i) p)
|
(write-char (string-ref string i) p))
|
||||||
|
((#\( #\) #\+ #\? #\{ #\} #\|)
|
||||||
|
(write-char #\[ p)
|
||||||
|
(write-char (string-ref string i) p)
|
||||||
|
(write-char #\] p))
|
||||||
|
(else
|
||||||
|
(write-char (string-ref string i) p)))
|
||||||
(loop (1+ i))))))))
|
(loop (1+ i))))))))
|
||||||
|
|
||||||
(define (match:start match . args)
|
(define (match:start match . args)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue