June 26, 2023 [Issue 24015] New: C#-style indented delimited strings | ||||
---|---|---|---|---|
| ||||
https://issues.dlang.org/show_bug.cgi?id=24015 Issue ID: 24015 Summary: C#-style indented delimited strings Product: D Version: D2 Hardware: All OS: All Status: NEW Severity: enhancement Priority: P1 Component: dmd Assignee: nobody@puremagic.com Reporter: qs.il.paperinik@gmail.com https://dlang.org/spec/lex.html#delimited_strings allows identifier-delimited strings like ``` q"EOS This is a multi-line heredoc string EOS" ``` However, that “The closing identifier must be placed on its own line at the leftmost column” is annoying and ugly in code w.r.t. indentation. For example: ```d void f() { string str = q"EOS This is a multi-line heredoc string EOS"; } ``` C# has triple-quote strings that are similar to D’s identifier-delimited strings as if `""` were the identifier, however, C# allows the string to be in one line and if it is not, the whitespace between the last newline and the closing """ is not considered part of the string – and, of course, must be present at the beginning of every line of the string. For the sake of readability, D should have something similar: If the sequence `q"` *end-of-string-identifier* *newline* is followed by whitespace (a sequence of whitespace characters), the whitespace is repeated on every line up until a line consists of the whitespace and the *end-of-string-identifier* `"` string terminator, that is considered a string literal. The line-initial whitespace is not part of the literal. E.g.: ```d void f() { string str = q"EOS This is a multi-line heredoc string EOS"; assert(str == "This\nis a multi-line\nheredoc string\n"); } ``` Theoretically, this is a breaking change, but it will be very unlikely that the string termination sequence will be anywhere in the string, let alone after whitespace that is repeated on every line before. Alternatively, D can go “full C#” and just implement `"""` delimited strings with the exact same semantics as C#: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/raw-string -- |
Copyright © 1999-2021 by the D Language Foundation