emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [External] : [PATCH] Interpret #r"..." as a raw string


From: Stefan Monnier
Subject: Re: [External] : [PATCH] Interpret #r"..." as a raw string
Date: Fri, 26 Feb 2021 14:48:02 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>     (ert-deftest lread-raw-string-usage-2 ()
>       (should (equal
>                (let ((str "(def\\macro leaf () nil)"))
>                  (string-match "(\\(def\\\\macro\\) \\([^ ]+\\)" str)
>                  (list (match-string 1 str) (match-string 2 str)))
>                '("def\\macro" "leaf")))
>     
>       (should (equal
>                (let ((str "(def\\macro leaf () nil)"))
>                  (string-match #r"(\(def\macro\) \([^ ]+\)" str)
>                  (list (match-string 1 str) (match-string 2 str)))
>                '("def\\macro" "leaf"))))

[ Note the above has a bug: the raw-string regexp equivalent to
  "(\\(def\\\\macro\\) \\([^ ]+\\)" is #r"(\(def\\macro\) \([^ ]+\)"
  notice the double backslash between "def" and "macro".  ]

For regexps, this has been discussed to death already, but a better
option is arguably to introduce a macro that converts from the
non-backslashed regexp style to the backslashed regexp style.
That would require even fewer backslashes in most cases (tho not in
this example because of the presence of a literal \ in the regexp).

    (should (equal
             (let ((str "(def\\macro leaf () nil)"))
               (string-match (re "\\((def\\\\macro) ([^ ]+)" str)
               (list (match-string 1 str) (match-string 2 str)))
             '("def\\macro" "leaf"))))

IOW, I think regexps are a poor motivation to introduce raw strings.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]