Newsgroups: comp.lang.lisp
Path: cantaloupe.srv.cs.cmu.edu!das-news.harvard.edu!news2.near.net!MathWorks.Com!yeshua.marcam.com!charnel.ecst.csuchico.edu!csusac!csus.edu!netcom.com!barmar
From: barmar@netcom.com (Barry Margolin)
Subject: Re: Can you better write this string manipulation function?
Message-ID: <barmarCvwoIp.7vI@netcom.com>
Organization: Netcom Online Communications Services (408-241-9760 login: guest)
References: <34lfvb$jr7@anaxagoras.ils.nwu.edu> <1994Sep7.231741.22231@ptolemy-ethernet.arc.nasa.gov> <MARCOXA.94Sep8094356@mosaic.nyu.edu>
Date: Sat, 10 Sep 1994 08:49:35 GMT
Lines: 29

In article <MARCOXA.94Sep8094356@mosaic.nyu.edu> marcoxa@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                                                barmar@netcom.com

