Thread overview
Small problem with multi-line strings
Jul 21, 2011
bearophile
Jul 21, 2011
Nick Sabalausky
Jul 21, 2011
Andrej Mitrovic
Jul 22, 2011
bearophile
Jul 22, 2011
Andrej Mitrovic
Jul 22, 2011
Diego Canuhé
Jul 22, 2011
Andrej Mitrovic
Jul 22, 2011
Diego Canuhé
Jul 22, 2011
Dmitry Olshansky
Jul 22, 2011
bearophile
July 21, 2011
Multi-line strings are handy, but I have a small problem.

This is an example, it has a problem, there is an unwanted newline at the beginning:

writeln("
- First item:  150
- Second item: 200
- Third item:  105");


To avoid it you can write this, but both break the alignment in the source code, and it's not nice looking:

writeln("- First item:  150
- Second item: 200
- Third item:  105");


writeln(
"- First item:  150
- Second item: 200
- Third item:  105");


To solve this problem in Python you are allowed to write (in Python you need tree " or tree ' to denote a multi-line string):

print """\
- First item:  150
- Second item: 200
- Third item:  105"""


The extra slash at the beginning avoids the start newline.

Is this currently possible in D too? If this isn't possible, is it worth a very little enhancement request for the support of that syntax?

Bye,
bearophile
July 21, 2011
"bearophile" <bearophileHUGS@lycos.com> wrote in message news:j0ac3m$29hj$1@digitalmars.com...
> Multi-line strings are handy, but I have a small problem.
>
> This is an example, it has a problem, there is an unwanted newline at the beginning:
>
> writeln("
> - First item:  150
> - Second item: 200
> - Third item:  105");
>
>
> To avoid it you can write this, but both break the alignment in the source code, and it's not nice looking:
>
> writeln("- First item:  150
> - Second item: 200
> - Third item:  105");
>
>
> writeln(
> "- First item:  150
> - Second item: 200
> - Third item:  105");
>
>
> To solve this problem in Python you are allowed to write (in Python you need tree " or tree ' to denote a multi-line string):
>
> print """\
> - First item:  150
> - Second item: 200
> - Third item:  105"""
>
>
> The extra slash at the beginning avoids the start newline.
>
> Is this currently possible in D too? If this isn't possible, is it worth a very little enhancement request for the support of that syntax?
>

It's even worse with indentation:

void foo()
{
    if(blah)
    {
        writeln("- First item:  150
            - Second item: 200
                -- Subitem 1
                -- Subitem 2
            - Third item:  105");
    }
}

That's why I created a (CTFEable) fucntion normalize() (maybe could use a better name?):

http://www.dsource.org/projects/semitwist/browser/trunk/src/semitwist/util/text.d#L630

void foo()
{
    if(blah)
    {
        // Works properly:
        writeln("- First item:  150
            - Second item: 200
                -- Subitem 1
                -- Subitem 2
            - Third item:  105".normalize());
    }
}

Another example:

Do this:
--------------------
void foo()
{
    enum codeStr = q{
        // Written in the D Programming Langauge
        // by John Doe

        int main()
        {
            return 0;
        }
    }.normalize();
}
--------------------

Instead of this:
--------------------
void foo()
{
enum codeStr =
q{// Written in the D Programming Langauge
// by John Doe

int main()
{
    return 0;
}};
}
--------------------

The resulting string is exactly the same.

I'd be happy to work it into something appropriate for Phobos if people are intersted.




July 21, 2011
    writeln(q"EOS
- First item:  150
- Second item: 200
- Third item:  105
EOS");

You can replace EOS with whatever you want.
July 22, 2011
Andrej Mitrovic:

>     writeln(q"EOS
> - First item:  150
> - Second item: 200
> - Third item:  105
> EOS");
> 
> You can replace EOS with whatever you want.

It seems to add a newline at the end :-(

Bye,
bearophile
July 22, 2011
This is the best I can do:

writeln(
`- First item:  150
- Second item: 200
- Third item:  105`);

Hope OCD doesn't kick in! :p
July 22, 2011
if you can, use write instead of writeln

             write(q"EOS
- First item:  150
- Second item: 200
- Third item:  105
EOS");


July 22, 2011
Followed by stdout.flush(); to be sure.
July 22, 2011
got one extra line, I meant

               write(q"EOS
- First item:  150
- Second item: 200
- Third item:  105
EOS");


July 22, 2011
On 22.07.2011 3:18, bearophile wrote:
> Multi-line strings are handy, but I have a small problem.
>
> This is an example, it has a problem, there is an unwanted newline at the beginning:
>
> writeln("
> - First item:  150
> - Second item: 200
> - Third item:  105");
>
>
> To avoid it you can write this, but both break the alignment in the source code, and it's not nice looking:
>
> writeln("- First item:  150
> - Second item: 200
> - Third item:  105");
>
>
> writeln(
> "- First item:  150
> - Second item: 200
> - Third item:  105");
>
>
> To solve this problem in Python you are allowed to write (in Python you need tree " or tree ' to denote a multi-line string):
>
> print """\
> - First item:  150
> - Second item: 200
> - Third item:  105"""
>
>
> The extra slash at the beginning avoids the start newline.
>
> Is this currently possible in D too? If this isn't possible, is it worth a very little enhancement request for the support of that syntax?
>
> Bye,
> bearophile
How about:

writeln(
"- First item:  150\n"
"- Second item: 200\n"
"- Third item:  105");

Yeah, I know implicit concatenation is bad and I would agree once ~ concatenate complie-time string for 0 overhead.
After all, hardcoded strings are not exactly good thing in the first place.
For big formatted texts just use string import :
     writeln(import("usage.txt"));
work like a charm.
So IMHO it's a non-issue.

-- 
Dmitry Olshansky

July 22, 2011
Dmitry Olshansky:

> writeln(
> "- First item:  150\n"
> "- Second item: 200\n"
> "- Third item:  105");
> 
> Yeah, I know implicit concatenation is bad and I would agree once ~ concatenate complie-time string for 0 overhead.

This works, but it's noisy. Multi-line strings are present in D right to avoid this.


> After all, hardcoded strings are not exactly good thing in the first place.

They are frequently needed in little programs, like script-like programs. I expect D to be good enough for such kind of programs too.


> For big formatted texts just use string import :
>       writeln(import("usage.txt"));
> work like a charm.
> So IMHO it's a non-issue.

I agree it's not a necessary thing, just like multi-line strings are not necessary. But it's handy, and none of the solutions shown in this tread is very good. So I've added an enhancement request:
http://d.puremagic.com/issues/show_bug.cgi?id=6361

Thank you for the answers and comments,
bye,
bearophile