Re: string substitution?



Pascal Bourguignon schrieb:
André Thieme <address.good.until.2006.dec.22@xxxxxxxxxxx> writes:

georgeryoung@xxxxxxxxx schrieb:
[mostly newbie, sbcl, linux]
I recently tried to do a seemingly simple thing and could not see how.
Given a string, I wanted to substitute a CR-LF for each LF, or if you
prefer, insert a CR before each LF. I'm sure it can be done with some
crufty characater by character loop, but it seems like there should be
an elegant solution.

Either the case of a small string, allowing exploding it into large
data structures, or a constant-memory streaming algorithm would be
interesting.
Any suggestions?
[I ended up doing it in python which was very simple, but my intent is
to learn practical lisp]
You could do it with cl-ppcre:

(cl-ppcre:regex-replace-all
(string #\Newline)
target-string
(concatenate 'string (string #\Newline) (string #\Return)))

This is incorrect. #\Newline has no relation with CR or LF.

(cl-ppcre:regex-replace-all
(string #\Linefeed)
target-string
(concatenate 'string (string #\Return) (string #\Linefeed)))



At least on sbcl there seems to be a relation:
CL-USER> #\Linefeed
#\Newline

I was following this table:
http://www.robelle.com/smugbook/ascii.htmlb

And after checking that (code-char 10) -> #\Newline
I put it in the code.


André
--
.



Relevant Pages

  • Re: string substitution?
    ... Given a string, I wanted to substitute a CR-LF for each LF, or if you ... crufty characater by character loop, but it seems like there should be ...
    (comp.lang.lisp)
  • Re: string substitution?
    ... Given a string, I wanted to substitute a CR-LF for each LF, or if you ... crufty characater by character loop, but it seems like there should be ... to learn practical lisp] ...
    (comp.lang.lisp)
  • string substitution?
    ... Given a string, I wanted to substitute a CR-LF for each LF, or if you ... crufty characater by character loop, but it seems like there should be ...
    (comp.lang.lisp)
  • Re: string substitution?
    ... Given a string, I wanted to substitute a CR-LF for each LF, or if you ... crufty characater by character loop, but it seems like there should be ...
    (comp.lang.lisp)