1

In python you can write this r"c:\data"

How do I the same in lisp? ie I want a reader macro that has the same functionality so I no longer need to escape backslashes in windows paths. I do not want to use forward slashes as plenty of commands in windows dont understand them.

f l
  • 1,684
  • 2
  • 10
  • 11
  • Actually, `#"` isn't in use yet, so you could use `#"c:\data"` – Barmar Jul 08 '22 at 21:45
  • 1
    Example libraries defining reader macro for strings: [pythonic-string-reader](https://github.com/smithzvk/pythonic-string-reader), [cl-heredoc](https://github.com/outergod/cl-heredoc), [mstrings](https://git.sr.ht/~shunter/mstrings). – Ehvince Jul 09 '22 at 09:31

1 Answers1

2

For instance

(defun read-literal-string (stream delimiter arg)
  (declare (ignore arg))
  (loop for char = (read-char stream nil stream)
        when (eq char stream)
          do (error "hit end of stream")
        until (char= char delimiter)
        collect char into chars
        finally (return (coerce chars 'string))))

(set-dispatch-macro-character #\# #\" #'read-literal-string)

And now

> #"fo\o"
"fo\\o"

> #"foo\"
"foo\\"

obviously a real version would be more carefully written

ignis volens
  • 7,040
  • 2
  • 12