[Openmcl-devel] encoding/decoding clozure strings to/from UTF-8

Mark Klein m_klein at mit.edu
Tue Dec 30 12:53:20 PST 2014


>>> I need to change a bunch of cl-http web page generation methods so that they output UTF-8. I wrote a little macro that I think will do the job. Am I on the right track, can it be made substantially faster or reliable somehow? 
>>> 
>>> (defmacro c2utf8 (&rest body)
>>>  "Takes everything body sends to stream, and converts it to UTF-8"
>>>  `(let ((list (encode-string-to-octets (with-output-to-string (stream) , at body) :external-format :utf-8)))
>>>     (loop for c across list do (write-char (code-char c) stream))))
>>> 
>> You’re working way too hard.  Character streams automatically do work for you.  All character streams in CCL have a parameter that sets their character encoding, and the default is UTF-8.  So unless you want a different encoding, you should just be able to output characters to a stream and the Right Thing will happen.  If it doesn’t, then either your default encoding has changed somehow, or you’ve found a bug in CCL.
> 
> Well, this is about HTTP, isn't it? Unfortunately, in HTTP you either need to use a byte stream or 
> neet to use a stream which can switch encoding while open (an HTTP header always needs to be RFC 822
> conformant (7bit ASCII) while the content part can have a custom encoding.
> Mark, before you reinvent the wheel: have a look at flexi-streams (http://weitz.de/flexi-streams/), a library
> written to solve exactly this problem (or you might want to base your server on hunchentoot which does all the
> ugly encoding for you …)

I'm trying to do this in with the cl-http web server, so my own code doesn't create the stream, that's done in the background somewhere by cl-http. Anyone know how to make it work in that context?
   
    Thanks,

	Mark

-------------------------------
Mark Klein
http://cci.mit.edu/klein

Principal Research Scientist
Center for Collective Intelligence
Massachusetts Institute of Technology

Visiting Researcher
Dynamic and Distributed Information Systems Group
University of Zurich

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 1842 bytes
Desc: not available
URL: <https://lists.clozure.com/pipermail/openmcl-devel/attachments/20141230/84e96486/attachment.bin>


More information about the Openmcl-devel mailing list