www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Small problem with multi-line strings

reply bearophile <bearophileHUGS lycos.com> writes:
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
Jul 21 2011
next sibling parent "Nick Sabalausky" <a a.a> writes:
"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.
Jul 21 2011
prev sibling next sibling parent reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
    writeln(q"EOS
- First item:  150
- Second item: 200
- Third item:  105
EOS");

You can replace EOS with whatever you want.
Jul 21 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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
Jul 21 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
This is the best I can do:

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

Hope OCD doesn't kick in! :p
Jul 21 2011
prev sibling next sibling parent =?ISO-8859-1?Q?Diego_Canuh=E9?= <canuhedc gmail.com> writes:
--bcaec5015f2bfbfbe204a8a0d59f
Content-Type: text/plain; charset=ISO-8859-1

if you can, use write instead of writeln

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

--bcaec5015f2bfbfbe204a8a0d59f
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

if you can, use write instead of writeln<br><br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0=A0=A0 write(q&quot;EOS<br>
<div class=3D"im">- First item: =A0150<br>
- Second item: 200<br>
- Third item: =A0105<br>
</div>EOS&quot;);<br><br>

--bcaec5015f2bfbfbe204a8a0d59f--
Jul 21 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
Followed by stdout.flush(); to be sure.
Jul 21 2011
prev sibling next sibling parent =?ISO-8859-1?Q?Diego_Canuh=E9?= <canuhedc gmail.com> writes:
--20cf307ac3d3c7611804a8a0dc79
Content-Type: text/plain; charset=ISO-8859-1

got one extra line, I meant

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

--20cf307ac3d3c7611804a8a0dc79
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

got one extra line, I meant<br><br>=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=A0=
=A0 write(q&quot;EOS<br>
<div class=3D"im"><div>- First item: =A0150<br>
- Second item: 200<br>
- Third item: =A0105<br>
</div></div>EOS&quot;);<br>

--20cf307ac3d3c7611804a8a0dc79--
Jul 21 2011
prev sibling parent reply Dmitry Olshansky <dmitry.olsh gmail.com> writes:
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

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
Jul 22 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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
Jul 22 2011