digitalmars.D.learn - Small problem with multi-line strings
- bearophile (23/23) Jul 21 2011 Multi-line strings are handy, but I have a small problem.
- Nick Sabalausky (60/85) Jul 21 2011 It's even worse with indentation:
- Andrej Mitrovic (6/6) Jul 21 2011 writeln(q"EOS
- bearophile (4/11) Jul 21 2011 It seems to add a newline at the end :-(
- Andrej Mitrovic (6/6) Jul 21 2011 This is the best I can do:
- =?ISO-8859-1?Q?Diego_Canuh=E9?= (6/6) Jul 21 2011 if you can, use write instead of writeln
- Andrej Mitrovic (1/1) Jul 21 2011 Followed by stdout.flush(); to be sure.
- =?ISO-8859-1?Q?Diego_Canuh=E9?= (6/6) Jul 21 2011 got one extra line, I meant
- Dmitry Olshansky (15/38) Jul 22 2011 How about:
- bearophile (8/20) Jul 22 2011 They are frequently needed in little programs, like script-like programs...
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
"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
writeln(q"EOS
- First item: 150
- Second item: 200
- Third item: 105
EOS");
You can replace EOS with whatever you want.
Jul 21 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
Jul 21 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
Jul 21 2011
if you can, use write instead of writeln
write(q"EOS
- First item: 150
- Second item: 200
- Third item: 105
EOS");
Jul 21 2011
Followed by stdout.flush(); to be sure.
Jul 21 2011
got one extra line, I meant
write(q"EOS
- First item: 150
- Second item: 200
- Third item: 105
EOS");
Jul 21 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
Jul 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
Jul 22 2011









"Nick Sabalausky" <a a.a> 