From a97181f024e4d66c6795a9b35c6cdc3ae67e8043 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Wed, 22 May 2013 13:19:06 -0400 Subject: [PATCH] Protect strings from regexps. Fixes PR13768. --- collects/racket/string.rkt | 7 ++++--- collects/tests/racket/string.rktl | 6 +++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/collects/racket/string.rkt b/collects/racket/string.rkt index 1035725859..7df9f8e839 100644 --- a/collects/racket/string.rkt +++ b/collects/racket/string.rkt @@ -35,8 +35,8 @@ (apply string-append r))) ;; Utility for the functions below: get a string or a regexp and return a list -;; of the regexp (strings are converted using `regexp-quote'), the and versions -;; that matches at the beginning/end. +;; of the regexp (strings are converted using `regexp-quote'), and versions +;; that match at the beginning/end. (define get-rxs (let ([t (make-weak-hasheq)] [t+ (make-weak-hasheq)]) (let ([spaces '(#px"\\s+" #px"^\\s+" #px"\\s+$")]) @@ -45,7 +45,8 @@ (λ (who rx +?) (hash-ref! (if +? t+ t) rx (λ () (let* ([s (cond [(string? rx) (regexp-quote rx)] - [(regexp? rx) (object-name rx)] + [(regexp? rx) (string-append + "(?:" (object-name rx) ")")] [else (raise-argument-error who "(or/c string? regexp?)" rx)])] [s (if +? (string-append "(?:" s ")+") s)] diff --git a/collects/tests/racket/string.rktl b/collects/tests/racket/string.rktl index 8234801aec..686e0095ae 100644 --- a/collects/tests/racket/string.rktl +++ b/collects/tests/racket/string.rktl @@ -437,6 +437,9 @@ (test " x" string-trim " x " #:left? #f) (test "x " string-trim " x " #:right? #f) (test " x " string-trim " x " #:left? #f #:right? #f) + (test "x" string-trim " x\t" #px" |\t") + (test "x" string-trim "\tx " #px" |\t") + (test "\t x \t" string-trim " \t x \t " #px" |\t") (for* ([i+e '(["" "" ""] ["a" "a" "a"] ["aa" "" ""] @@ -467,7 +470,8 @@ (for ([s (in-list '(" " " " "\n\t\r"))]) (test '() string-split s)) (test '("x" "y" "z") string-split "axayaza" "a") - (test '("" "x" "y" "z" "") string-split "axayaza" "a" #:trim? #f)) + (test '("" "x" "y" "z" "") string-split "axayaza" "a" #:trim? #f) + (test '("foo" "bar" "baz") string-split "foo,bar;baz" #rx",|;")) ;; ---------- string-replace/* ---------- (let ()