On Jan 17, 2009, at 2:37 AM, Tom Faulhaber wrote:

Question: How do I read non-printing characters in the reader?

From reading src/jvm/clojure/lang/LispReader.java and experimenting, I see that there are at least 3 ways to read such an arbitrary character code with the reader:

- Octal character codes from 0 to 255 (octal 0 to 377) can be entered using a literal of the form "\oddd" (that's a lower case oh followed by up to 3 digits). You can represent the ^C character as \o3 in Clojure source.

- Unicode character codes can be entered using a literal of the form "\uDDDD" (that's a lowercase u followed by exactly 4 digits). You can represent the ^C character as \u0003 in Clojure source.

- If you can get the character into the input stream with a leading backslash, the reader does read it properly. This doesn't work for ^C (for me, in an emacs *shell* buffer) because it's interpreted as "quit" before Clojure sees it. (I would imagine it's possible to put the terminal into some kind of "raw" mode so that doesn't happen.) It does work for less consequential characters like ^G.

I don't see these documented on clojure.org/reader which is where I would expect to find the info.

It does appear that Clojure can properly read the characters it prints:

        user=> (with-in-str (pr-str \u0003) (read))
        \^C
        user=> (int (with-in-str (pr-str \u0003) (read)))
        3
        user=>

(which I did in emacs, which shows a literal ascii 3 as ^C)

--Steve

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to