WYSIWYG string still limited

Kirk McDonald kirklin.mcdonald at gmail.com
Sat Dec 15 14:41:24 PST 2007


Swer wrote:
> "Kirk McDonald" <kirklin.mcdonald at gmail.com> Swer wrote:
> 
>>>The D 2.x series has already added a few new forms of string literals,
>>
>>including delimited strings. They look like this:
>>
>>q"(foo(xxx))"   // "foo(xxx)"
>>q"[foo{]"       // "foo{"
>>q"/foo"bar/"    // "foo\"bar"
> 
> 
> hmm thats still not enough, and complex for compiler to implement. I 
> modified my poposal as below:
> 

The compiler already has implemented them. Also, I've written the D 
lexer in Pygments (which supports them) and implemented highlighting for 
these new string literals in vim. They are not particularly hard to 
implement.

> const char [] code_demo = {{anystr{
>  q"(foo(xxx))"   // "foo(xxx)"
>  q"[foo{]"       // "foo{"
>  q"/foo"bar/"    // "foo\"bar"
> }anystr}}
> 
> or
> 
> const char [] code_demo = {{{
>  q"(foo(xxx))"   // "foo(xxx)"
>  q"[foo{]"       // "foo{"
>  q"/foo"bar/"    // "foo\"bar"
> }}}
> 
> if there is no }}} in text.
> 
> 'anystr' can be replaced to any string you like. '{{'  '}}'  '{{{'  '}}}' 
> are easier for compiler to detect.
> 
> 

If you'd bothered looking at the docs, you'd have seen that there are 
other forms of string literals which I did not mention. There are token 
strings:

q{
  q"(foo(xxx))"   // "foo(xxx)"
  q"[foo{]"       // "foo{"
  q"/foo"bar/"    // "foo\"bar"
}

These have the caveat that they can only contain valid D tokens.

There are also heredoc strings, which are really just a kind of 
delimited string:

q"IDENT
This is a string. It can contain anything.
IDENT"

IDENT may be any identifier. The newlines after the opening identifier 
and before the closing one are required.

-- 
Kirk McDonald
http://kirkmcdonald.blogspot.com
Pyd: Connecting D and Python
http://pyd.dsource.org



More information about the Digitalmars-d mailing list