Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to escape a line-end?

39 views
Skip to first unread message

jara...@skynet.be

unread,
Mar 26, 2015, 4:10:17 AM3/26/15
to
Hi,
given a multi-line string

"abc
def"

I'd like to "align" the lines. In other programming languages one can escape
the line end with a backslash like

"\
abc
def"

Is there a replacement in Common Lisp?

Many thanks
Helmut

Nicolas Hafner

unread,
Mar 26, 2015, 5:24:07 AM3/26/15
to
A bit ugly, but you can do something like

#.(format NIL "foo~
bar~
baz")

resulting in

"foobarbaz"

You could go a step further and define a dispatch macro character to aid
in that:

(set-dispatch-macro-character
#\# #\~ (lambda (stream a b)
(declare (ignore a b))
(format NIL (read stream))))

#~"foo~
bar~
baz"

However, adding reader extensions is discouraged as it can clash with
other libraries or even standard ones (See section 2.4.8). Make sure
that it either does not clash at all, or use something like
named-readtables if you're planning on distributing your code as a library.

--
http://everything.shinmera.com

Pascal J. Bourguignon

unread,
Mar 26, 2015, 5:28:40 AM3/26/15
to
The standard reader macro for #\" doesn't do that. Any non " or \
character is take literally, and \ escapes any character.

You can write your own reader macro to read strings, with all the
escapes you want.

http://paste.lisp.org/display/137262

Strings are often used with FORMAT. FORMAT implements some useful
features with ~ newline:

http://www.lispworks.com/documentation/HyperSpec/Body/22_cic.htm


--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk

jara...@skynet.be

unread,
Mar 26, 2015, 8:44:24 AM3/26/15
to
Many thanks to both of you,
Helmut

Kaz Kylheku

unread,
Mar 26, 2015, 10:39:03 AM3/26/15
to
On 2015-03-26, Nicolas Hafner <shin...@tymoon.eu> wrote:
> On 26/03/15 09:10, jara...@skynet.be wrote:
>> Hi,
>> given a multi-line string
>>
>> "abc
>> def"
>>
>> I'd like to "align" the lines. In other programming languages one can escape
>> the line end with a backslash like
>>
>> "\
>> abc
>> def"
>>
>> Is there a replacement in Common Lisp?
>>
>> Many thanks
>> Helmut
>>
>
> A bit ugly, but you can do something like
>
> #.(format NIL "foo~
> bar~
> baz")
>
> resulting in
>
> "foobarbaz"

In TXR, I experimented in this area.

Firstly, literals do not span lines (possibly a design mistake, but
one that could be fixed later):

$ txr -p '"abc
def"'
txr: (string:2): syntax error
txr: (string:2): newline in string literal
txr: unhandled exception of type syntax-error:
txr: message: read: syntax error

Escaped newline deletes all surrounding unescaped whitespace, from
current line and following line:

$ txr -p '"abc \
def"'
"abcdef"

Escaped space is preseved:

$ txr -p '"abc\ \
def"'
"abc def"

... on either side of the line continuation:

$ txr -p '"abc\
\ def"'
"abc def"

C-like escapes encode special characters, so \n for newline.
We achieve a multi-line string with a desired indentation:

$ txr -P '"abc\x6f22\x5b57\n\
\ def\n\
\ ghi\n"
abc漢字
def
ghi

Largely a no-brainer.

WJ

unread,
Mar 26, 2015, 4:59:46 PM3/26/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Pascal J. Bourguignon wrote:

> jara...@skynet.be writes:
>
> > Hi,
> > given a multi-line string
> >
> > "abc
> > def"
> >
> > I'd like to "align" the lines. In other programming languages one can escape
> > the line end with a backslash like
> >
> > "\
> > abc
> > def"
> >
> > Is there a replacement in Common Lisp?
>
> The standard reader macro for #\" doesn't do that. Any non " or \
> character is take literally, and \ escapes any character.
>
> You can write your own reader macro to read strings, with all the
> escapes you want.
>
> http://paste.lisp.org/display/137262
>
> Strings are often used with FORMAT. FORMAT implements some useful
> features with ~ newline:
>
> http://www.lispworks.com/documentation/HyperSpec/Body/22_cic.htm

Gauche Scheme:

gosh> (use srfi-13)
#<undef>
gosh> (string-trim-both "
foo
bar
who
")
"foo\r\nbar\r\nwho"


tar...@google.com

unread,
Mar 26, 2015, 5:57:54 PM3/26/15
to
Wrong answer.
The correct result is "foobarwho" without the CR-LF characters.

Kaz Kylheku

unread,
Mar 27, 2015, 12:32:08 AM3/27/15
to
On 2015-03-26, WJ <w_a_...@yahoo.com> wrote:
> gosh> (string-trim-both "
> foo
> bar
> who
> ")
> "foo\r\nbar\r\nwho"

Retarded garbage.
0 new messages