From: Christopher J. Vogt
Subject: Re: how to efficiently concatenate strings?
Date: 
Message-ID: <35B7449E.189E8D4C@computer.org>
kp gores wrote:
> 
> hello,
> in a loop reading a file char-by-char into current-char i do :
>        (setf current-token (concatenate 'string
>                                         current-token
>                                         (string current-char)))
> 
> to collect the characters into  a string current-token.
> 
> this is lacks efficiency, because concatenate seems to be non-destructive.
> 
> how do i efficienmtly code the above??

Maybe something like this?

(defun get-file-string (file)
   (with-open-file (stream file)
      (let* ((length (file-length stream))  ; length of file may actually be
smaller
             (string (make-string length))) ;    but gaurantees string is long
enough
        (loop for i from 0
              for char = (read-char stream nil nil)
              while char do
              (setf (aref string i) char)))))

If you need the *true* length of the string (because file-length may
be bigger than the actual contents of the file) you can return multiple
values including i.

Note the recent discussion on "problem with large arrays" might be of interest
to you should you be reading large files.

-- 
Christopher J. Vogt - Computer Consultant - Lisp, AI, Graphics, etc.
http://members.home.com/vogt/