www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What are delimited string, heredoc and D token strings?

reply Pierre Rouleau <prouleau001 gmail.com> writes:
Hi all,

The D2.0 lexical page describes delimited string and token string 
literals.  Is there any example of how these are used and why, somewhere?

Thanks

-- Pierre
Jun 27 2010
parent reply "Simen kjaeraas" <simen.kjaras gmail.com> writes:
Pierre Rouleau <prouleau001 gmail.com> wrote:

 Hi all,

 The D2.0 lexical page describes delimited string and token string  =
 literals.  Is there any example of how these are used and why, somewhe=
re? Token strings are added for the specific use case of string mixins[1]. They must contain valid D code. mixin( q{ a =3D b; } ); // Works. mixin( q{ this is nonsense, I tell you! } ); // Does not work. Editors may syntax-highlight token strings as if it were normal code. They often do, by virtue of having no idea that it is indeed a string. Delimited strings and heredoc strings exist for simplicity of adding lon= g strings to the source. q"EOS Heredoc strings allow you to add long string literals to your code. They also let you embed "quotes" 'of' `various` =C2=B4kinds=C2=B4 withou= t escaping them. EOS" q"(Delimited strings are much the same as "heredoc" strings, but somewha= t more succinct, by not using a whole identifier for terminators. They are= thus not as suited for long texts.)" [1]: http://digitalmars.com/d/2.0/statement.html#MixinStatement also, http://digitalmars.com/d/2.0/expression.html#MixinExpression http://digitalmars.com/d/2.0/module.html#MixinDeclaration -- = Simen
Jun 27 2010
next sibling parent reply Pierre Rouleau <prouleau001 gmail.com> writes:
On 27/06/10 9:52 AM, Simen kjaeraas wrote:
 Pierre Rouleau <prouleau001 gmail.com> wrote:

 Hi all,

 The D2.0 lexical page describes delimited string and token string
 literals. Is there any example of how these are used and why, somewhere?
Token strings are added for the specific use case of string mixins[1]. They must contain valid D code. mixin( q{ a = b; } ); // Works. mixin( q{ this is nonsense, I tell you! } ); // Does not work. Editors may syntax-highlight token strings as if it were normal code. They often do, by virtue of having no idea that it is indeed a string. Delimited strings and heredoc strings exist for simplicity of adding long strings to the source. q"EOS Heredoc strings allow you to add long string literals to your code. They also let you embed "quotes" 'of' `various` ´kinds´ without escaping them. EOS" q"(Delimited strings are much the same as "heredoc" strings, but somewhat more succinct, by not using a whole identifier for terminators. They are thus not as suited for long texts.)" [1]: http://digitalmars.com/d/2.0/statement.html#MixinStatement also, http://digitalmars.com/d/2.0/expression.html#MixinExpression http://digitalmars.com/d/2.0/module.html#MixinDeclaration
Thanks for this clear explanation, Simen. I was wondering if D had the equivalnt of Python's triple quote string literals and it does: the delimited string serves the same purpose. Great! -- Pierre
Jun 27 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Pierre Rouleau:
 I was wondering if D had the equivalnt of Python's triple quote string 
 literals and it does: the delimited string serves the same purpose.  Great!
But keep in mind that normal D string literals can span more than one line :-) Bye, bearophile
Jun 27 2010
parent reply Pierre Rouleau <prouleau001 gmail.com> writes:
On 27/06/10 11:36 AM, bearophile wrote:

 But keep in mind that normal D string literals can span more than one line :-)
In what sense? In the sense that adjacent strings are concatenated? If I want to create a string literal that embeds new line without explicitly placing a '\n' inside the string, I did not see any other way than using the delimited string (at least inside http://www.digitalmars.com/d/2.0/lex.html). What am I missing? -- Pierre
Jun 27 2010
parent reply bearophile <bearophileHUGS lycos.com> writes:
Pierre Rouleau:
 In what sense?
This is valid D1 code: import std.stdio; void main() { string s = "this is just a test"; writefln(s); } Bye, bearophile
Jun 27 2010
parent Pierre Rouleau <prouleau001 gmail.com> writes:
On 27/06/10 1:03 PM, bearophile wrote:
 Pierre Rouleau:
 In what sense?
This is valid D1 code: import std.stdio; void main() { string s = "this is just a test"; writefln(s); } Bye, bearophile
Thanks! -- Pierre
Jun 28 2010
prev sibling parent Pierre Rouleau <prouleau001 gmail.com> writes:
On 27/06/10 9:52 AM, Simen kjaeraas wrote:
 Pierre Rouleau <prouleau001 gmail.com> wrote:

 Hi all,

 The D2.0 lexical page describes delimited string and token string
 literals. Is there any example of how these are used and why, somewhere?
Token strings are added for the specific use case of string mixins[1]. They must contain valid D code. mixin( q{ a = b; } ); // Works. mixin( q{ this is nonsense, I tell you! } ); // Does not work. Editors may syntax-highlight token strings as if it were normal code. They often do, by virtue of having no idea that it is indeed a string. Delimited strings and heredoc strings exist for simplicity of adding long strings to the source. q"EOS Heredoc strings allow you to add long string literals to your code. They also let you embed "quotes" 'of' `various` ´kinds´ without escaping them. EOS" q"(Delimited strings are much the same as "heredoc" strings, but somewhat more succinct, by not using a whole identifier for terminators. They are thus not as suited for long texts.)" [1]: http://digitalmars.com/d/2.0/statement.html#MixinStatement also, http://digitalmars.com/d/2.0/expression.html#MixinExpression http://digitalmars.com/d/2.0/module.html#MixinDeclaration
Thanks for this clear explanation, Simen. I was wondering if D had the equivalent of Python's triple quote string literals and it does: the delimited string serves the same purpose. Great! -- Pierre
Jun 27 2010