Search code examples
common-lispsbclunicode-normalization

Convert superscript 2 (²) symbol to string


I am trying to get a string of code from some lisp files, to create unix filenames and display it on web pages:

(let ((code "(defun ² (x) (* x x))"))
 (second (read-from-string code)))

Which evaluates to |2|. In fact, just typing evaluates to |2|, instead of |²|.

The code string is read from a file. I'm using SBCL 2.2.10.

With this scheme, the string "x²" is parsed the same as the string "x2" :

(read-from-string "x²")
(read-from-string "x2")

How can I make read-from-string correct, without modification on the code in the string ?


Solution

  • See sect. 7.1.2 of SBCL manual. By default, SBCL extends the reader to normalize all symbols. You can prevent this for current readtable by

    (setf (sb-ext:readtable-normalization *readtable*) nil)