Prev: need easy benchmarks
Next: Lisp sucks!
From: milanj on 12 Mar 2010 03:11 On Mar 12, 9:10 am, "mil...(a)gmail.com" <mil...(a)gmail.com> wrote: > what about using something like this: > > (let ((string (make-array (file-stream stream) :element-type > 'character))) > (read-sequence string stream) > (string)) of course this should be: (let ((string (make-array (file-stream stream) :element-type 'character))) (read-sequence string stream) string)
From: refun on 12 Mar 2010 03:44 In article <732c910e-a482-4510-a9d3-0f55ec7a27cd(a)q15g2000yqj.googlegroups.com>, milanj(a)gmail.com says... > > On Mar 12, 9:10 am, "mil...(a)gmail.com" <mil...(a)gmail.com> wrote: > > what about using something like this: > > > > (let ((string (make-array (file-stream stream) :element-type > > 'character))) > > (read-sequence string stream) > > (string)) > > of course this should be: > > (let ((string (make-array (file-stream stream) :element-type > 'character))) > (read-sequence string stream) > string) Using READ-SEQUENCE looks nicer, but there is one problem: you need to know how many characters(not bytes!) you'll have in your file for it to work. It would cons a larger array than needed if the file's encoding is something other than latin1 (or similar), such as if the file was opened with an EXTERNAL-FORMAT which specifies some encoding like UTF8/16 or some other multibyte format.
From: Vinay on 12 Mar 2010 04:01 On 2010-03-11 20:12:22 -0800, Vassil Nikolov said: > On Fri, 12 Mar 2010 03:58:53 +0000 (UTC), refun <refun(a)nospam.gmx.com> said: > >> Here's another way using a fill-pointer: >> (defun read-stream-to-string (stream) >> (let ((string (make-array (file-length stream) >> :element-type 'character >> :initial-element #\Space >> :fill-pointer 0))) >> (loop for char = (read-char stream nil 'done) >> until (eql char 'done) >> do (vector-push char string) >> finally (return string)))) > > Change to ``(OR (FILE-LENGTH STREAM) 0)'', add ``:ADJUSTABLE T'', > and use VECTOR-PUSH-EXTEND to degrade gracefully when the input > stream's length cannot be determined or when the file grows while > being read. > > ---Vassil. thanks. i fing the with-output-to-string to be a cleaner solution. --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: milanj on 12 Mar 2010 05:24 On Mar 12, 9:44 am, refun <re...(a)nospam.gmx.com> wrote: > In article <732c910e-a482-4510-a9d3-0f55ec7a2...(a)q15g2000yqj.googlegroups..com>, > mil...(a)gmail.com says... > > > > > On Mar 12, 9:10 am, "mil...(a)gmail.com" <mil...(a)gmail.com> wrote: > > > what about using something like this: > > > > (let ((string (make-array (file-stream stream) :element-type > > > 'character))) > > > (read-sequence string stream) > > > (string)) > > > of course this should be: > > > (let ((string (make-array (file-stream stream) :element-type > > 'character))) > > (read-sequence string stream) > > string) > > Using READ-SEQUENCE looks nicer, but there is one problem: you need to know how > many characters(not bytes!) you'll have in your file for it to work. > It would cons a larger array than needed if the file's encoding is something > other than latin1 (or similar), such as if the file was opened with an > EXTERNAL-FORMAT which specifies some encoding like UTF8/16 or some other > multibyte format. yes, that is a problem and in my code (file-stream stream) should be (file-length stream) of course : )
From: Tim Bradshaw on 12 Mar 2010 05:55
On 2010-03-12 03:58:53 +0000, refun said: > It makes an array of file-length size, but keeps a fill-pointer of 0, then > pushes each character into the array. This is not safe, because the number of characters in the file may not be the same as the file's length, depending on the encoding of the file. This is a classic "the world is Unix and all characters are exactly 8 bits long" mistake. |