Thread overview
verbatim string literals (feature request)
Mar 13, 2004
Chris Lajoie
Mar 13, 2004
Andy Friesen
Mar 13, 2004
Chris Lajoie
Mar 13, 2004
Andy Friesen
Mar 13, 2004
Piotr Fusik
Mar 13, 2004
C. Sauls
March 13, 2004
Hi,
There's a feature in C# that allows one to create a verbatim string in which
escape characters have no effect, and strings can span multiple lines.  The
reason I want this is because regular expressions that have backslashes in
them must be converted so they use a double backslash instead.  This also
means that regular expressions can be copied directly out of a D file
without modification.  This isn't a big deal for regexps that have two or
three backslash characters, but the kind of regular expressions I often work
with are 100 characters or more (22 backslash chars in one I just looked
at).  The spanning multiple lines I don't use, and may not be a good idea,
but IMO, verbatim strings were an awesome idea for C# and I'd like to see
them in D as well.

here's some examples straight from the C# specification:
---------------------
string a = "hello, world";                  // hello, world
string b = @"hello, world";               // hello, world
string c = "hello \t world";               // hello     world
string d = @"hello \t world";               // hello \t world
string e = "Joe said \"Hello\" to me";      // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me";   // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt";   // \\server\share\file.txt
string h = @"\\server\share\file.txt";      // \\server\share\file.txt
string i = "one\ntwo\nthree";
string j = @"one
two
three";
---------------------

Can anyone think of any disadvantages to verbatim strings?  The '@' char is also wide and unmistakable; not to mention it isn't being used (afaik) for anything else in D. Also, what are your thoughts on the line spanning? Personally, I think it could get confusing.

This seems like it would be a fairly simple thing to impliment in the compiler, although I am not familiar with the structure of the compiler so I could be wrong.

Chris



March 13, 2004
Chris Lajoie wrote:

> Hi,
> There's a feature in C# that allows one to create a verbatim string in which
> escape characters have no effect, and strings can span multiple lines.  The
> reason I want this is because regular expressions that have backslashes in
> them must be converted so they use a double backslash instead.  This also
> means that regular expressions can be copied directly out of a D file
> without modification.  This isn't a big deal for regexps that have two or
> three backslash characters, but the kind of regular expressions I often work
> with are 100 characters or more (22 backslash chars in one I just looked
> at).  The spanning multiple lines I don't use, and may not be a good idea,
> but IMO, verbatim strings were an awesome idea for C# and I'd like to see
> them in D as well.

There are two ways to do this in D.  One is r"This string is verbatim", the other is using backquotes: `Look at me!`

All D string literals can span lines, I believe.

 -- andy
March 13, 2004
> verbatim strings were an awesome idea for C# and I'd like to see
>them in D as well.

This is obviously not C#'s original idea. Personally I think that using the '@' character is a bad idea and I prefer D's 'r'.

For your information, here's how Perl does it:
q/String that
spans multiple
lines and may contain embedded \ characters/

Note that you may choose one from many available delimiter characters: ', ", /, !, *, #, |, etc. or a pair of parentheses or brackets.

If that's still not enough, you can use the so-called "here document":
<<EOF
blah blah blah
all characters are available: ' " / { }
EOF
(use any text for EOF).

>Can anyone think of any disadvantages to verbatim strings?

In C#, you still have to escape the double-quote character or you can't use it inside verbatim strings at all (I don't remember).

> The '@' char is also wide and unmistakable; not to mention it isn't being used (afaik) for anything else in D.

This is a drawback. In the future it could be used for something more useful than verbatim strings.

> Also, what are your thoughts on the line spanning?
>Personally, I think it could get confusing.

It is sometimes useful, but rarely.

Well, D's r"" and `` are enough for me.


March 13, 2004
Piotr Fusik wrote:
> It is sometimes useful, but rarely.

The one place I find myself using them very often is in printing usage/help messages.  Ie:

void usage() {
    printf(r"Usage: program [options]
	-1	Option 1
	-2	Option 2
	-N	Option N
    ");
}

-C. Sauls
-Invironz
March 13, 2004
> > Hi,
> > There's a feature in C# that allows one to create a verbatim string in
which
> > escape characters have no effect, and strings can span multiple lines.
The
> > reason I want this is because regular expressions that have backslashes
in
> > them must be converted so they use a double backslash instead.  This
also
> > means that regular expressions can be copied directly out of a D file without modification.  This isn't a big deal for regexps that have two
or
> > three backslash characters, but the kind of regular expressions I often
work
> > with are 100 characters or more (22 backslash chars in one I just looked at).  The spanning multiple lines I don't use, and may not be a good
idea,
> > but IMO, verbatim strings were an awesome idea for C# and I'd like to
see
> > them in D as well.
>
> There are two ways to do this in D.  One is r"This string is verbatim", the other is using backquotes: `Look at me!`
>
> All D string literals can span lines, I believe.

After having read the documentation I never found anything regarding verbatim strings.  Can you point me to where they are discussed?  It's certainly possible I missed something, I skimmed most of it.  Thanks!

Chris


March 13, 2004
Chris Lajoie wrote:
>>There are two ways to do this in D.  One is r"This string is verbatim",
>>the other is using backquotes: `Look at me!`
>>
>>All D string literals can span lines, I believe.
> 
> 
> After having read the documentation I never found anything regarding
> verbatim strings.  Can you point me to where they are discussed?  It's
> certainly possible I missed something, I skimmed most of it.  Thanks!

It's in the "Lexical" section.  Scroll to "String Literals"

-- andy