To avoid a newline in multi-line strings

Nick Sabalausky a at a.a
Sat Jul 23 17:40:31 PDT 2011


"Andrei Alexandrescu" <SeeWebsiteForEmail at erdani.org> wrote in message 
news:4E2B5C3E.4090404 at erdani.org...
> On 7/23/11 6:22 PM, Walter Bright wrote:
>> On 7/23/2011 1:23 PM, Nick Sabalausky wrote:
>>> I'd be happy to work it into something appropriate for Phobos if
>>> people are
>>> intersted.
>>
>> I think you should propose it as a pull request.

Will do.

>
> std.string.outdent
>

It would make sense to name the "unindent" function "outdent", but the 
"normalize" does more than that: It also removes all whitespace-only lines 
from the top and bottom ("stripTopBottom") and (though this part is maybe 
far less important) it does a stripr on each remaining line.

The idea is you can call just one CTFEable function on an embedded multiline 
string, and be able to format the string however looks right and not have to 
worry about the resulting string having werid whitespace. As a trivial 
example, suppose you had a program that output the source for a basic hello 
world:

// example.d
import std.stdio;
void main()
{
    enum helloWorld = q{
        import std.stdio;

        void main()
        {
            writeln("Hello World");
        }
    };
}

$ dmd example.d
$ example > hello.d

As it is, making it actually fit nicely into example.d's source results in 
not just excess indentation in hello.d, but also an excess leading/trailing 
blank line. So using one function cleans up everything:

// example2.d
import std.stdio;
void main()
{
    enum helloWorld = q{
        import std.stdio;

        void main()
        {
            writeln("Hello World");
        }
    }.normalize(); // Bye-bye indents *and* excess leading/trailing lines
}





More information about the Digitalmars-d mailing list