From: Andrew Philpot
Subject: Re: Can you better write this string manipulation function?
Date: 
Message-ID: <1994Sep7.231741.22231@ptolemy-ethernet.arc.nasa.gov>
In article <··········@anaxagoras.ils.nwu.edu>, ····@aristotle.ils.nwu.edu (William Fitzgerald) writes:
|> (defun remove-curly-brace-comments (string)
|>   (let ((collecting t))
|>     (coerce
|>      (loop for ch across string 
|>            when (char= ch #\{)
|>            do (setf collecting nil)
|>            when collecting
|>            collect ch
|>            when (char= ch #\})
|>            do (setf collecting t))
|>      'string)))
|> 

Shamelessly appropriating from the above,

(defun remove-curly-brace-comments (in)
  (let ((collecting t))
    (with-output-to-string (out)
      (loop for ch across in
	  when (char= ch #\{)
	  do (setq collecting nil)
	  when collecting
	  do (write-char ch out)
	  when (char= ch #\})
	  do (setq collecting t)))))

Perhaps a little more efficient, as it doesn't produce and then discard a list.

But what about embedded {} comments?


-- 
Andrew Philpot	 		
Recom Tech/NASA Ames 		
·······@ptolemy.arc.nasa.gov	
			        
From: Barry Margolin
Subject: Re: Can you better write this string manipulation function?
Date: 
Message-ID: <barmarCvwoIp.7vI@netcom.com>
In article <····················@mosaic.nyu.edu> ·······@mosaic.nyu.edu (Marco Antoniotti) writes:
>My version is not efficient and conses a lot (at least you can argue
>that it does). Pure Lisp Style :) But it is really elegant!!!!
>
>;;; This code assumes you do not have "nested" comments. You need a
>;;; parser for that

It also assumes you have exactly one comment in the string.  Here's a
modified version that fixes that bug.

(defun remove-curly-ied-comments (string)
  (let* ((lcurl-pos (position #\{ string))
         (rcurl-pos (and lcurl-pos (position #\} string :start lcurl-pos)))
         (head (subseq string 0 lcurl-pos))
         (tail (and rcurl-pos
                    ;; Recurse to remove additional comments
		    (remove-curly-ied-comments
                      (subseq string (1+ rculr-pos))))))
   (values (concatenate 'string head tail)
           (and lcurl-pos (not rcurl-pos)))))

It also returns a second value that's true if the string ended in an
unclosed comment.  Of course, for this to be useful, the function should
also take an argument indicating whether the line begins in a comment,
although this can be faked by calling (remove-curly-ied-comments
(concatenate 'string "{" string)).
-- 
Barry Margolin                                                ······@netcom.com