From: Nico de Jager on
Hi

I have seen this question answered before, but just can't seem to find
it.

How do I split a string literal into multiple lines, but still end up
with a single string without line breaks?

E.g.

> "foo \
baz"
=> "foo baz"

A link to the appropriate entry in the CLHS will also be appreciated.

Thanks.
Nico
From: Zach Beane on
Nico de Jager <nicodnn(a)gmail.com> writes:

> Hi
>
> I have seen this question answered before, but just can't seem to find
> it.
>
> How do I split a string literal into multiple lines, but still end up
> with a single string without line breaks?
>
> E.g.
>
>> "foo \
> baz"
> => "foo baz"
>
> A link to the appropriate entry in the CLHS will also be appreciated.

There is no built-in syntax for this. One option is to use FORMAT:

(format nil "This is a string ~
that has no newlines ~
in it.")

You could introduce a read-macro to do something like this, too.

Zach
From: Nico de Jager on
Zach Beane <xach(a)xach.com> writes:

> Nico de Jager <nicodnn(a)gmail.com> writes:
>
>> Hi
>>
>> I have seen this question answered before, but just can't seem to find
>> it.
>>
>> How do I split a string literal into multiple lines, but still end up
>> with a single string without line breaks?
>>
>> E.g.
>>
>>> "foo \
>> baz"
>> => "foo baz"
>>
>> A link to the appropriate entry in the CLHS will also be appreciated.
>
> There is no built-in syntax for this. One option is to use FORMAT:
>
> (format nil "This is a string ~
> that has no newlines ~
> in it.")
>
> You could introduce a read-macro to do something like this, too.

Ok, thanks for taking the time to respond.

Looking at it from another angle, though, is there standard
functionality for the reader to _combine_ several string literals into
one string. E.g.

"foo " \
"baz"
=> "foo baz" ; Where \ does the magic.

This is purely for stylistic reasons. When I have a long string I want
to format it nicely within the code. Between the ~ format directive,
other concatenation functions and a read-macro I can come up with
something myself, but I could swear I have seen this handled in a
standard way before - maybe my memory is playing tricks on me.

Thanks again.
Nico
From: Tim Bradshaw on
On 2010-08-13 09:03:48 +0100, Nico de Jager said:

> This is purely for stylistic reasons. When I have a long string I want
> to format it nicely within the code. Between the ~ format directive,
> other concatenation functions and a read-macro I can come up with
> something myself, but I could swear I have seen this handled in a
> standard way before - maybe my memory is playing tricks on me.

No, there isn't a standard syntax for this I think. If I was doing it
I'd probably define an ordinary macro:

(defmacro sconc (&rest strings)
(assert (every #'stringp strings) () "need literal strings")
(apply #'concatenate 'string strings))

From: Pascal J. Bourguignon on
Nico de Jager <ndj(a)bitart.cc> writes:

> Zach Beane <xach(a)xach.com> writes:
>> You could introduce a read-macro to do something like this, too.
>
> Ok, thanks for taking the time to respond.
>
> Looking at it from another angle, though, is there standard
> functionality for the reader to _combine_ several string literals into
> one string. E.g.

We already answered. No there is not. You should write a trivial
read-macro to do something like this!

>
> "foo " \
> "baz"
> => "foo baz" ; Where \ does the magic.
>
> This is purely for stylistic reasons. When I have a long string I want
> to format it nicely within the code.


Just write the reader macro!



(defun read-my-string (stream delimiter)
(coerce
(loop
:named collect
:with state = :normal
:with buffer = '()
:for ch = (read-char stream)
:do (ecase state
((:normal)
(cond
((char= delimiter ch)
(return-from collect (nreverse buffer)))
((char= #\\ ch)
(setf state :escape))
(t
(push ch buffer))))
((:escape)
(cond
((char= #\newline ch)
#|nop|#)
(t
(push ch buffer)))
(setf state :normal))))
'string))

(set-macro-character #\" (function read-my-string))



CL-USER> "abc \"def\" ghi"
"abc \"def\" ghi"
CL-USER> "abc \\\"def\\\" ghi"
"abc \\\"def\\\" ghi"
CL-USER> "abc
ghi"
"abc
ghi"
CL-USER> "abc \
ghi"
"abc ghi"
CL-USER>


--
__Pascal Bourguignon__ http://www.informatimago.com/