avoiding the first newline in a C++11 raw string literal?
You can get a pointer to the 2nd character - skipping the leading newline - by adding 1 to the const char*
to which the string literal is automatically converted:
some_code();
std::string text = 1 + R"(
This is the first line.
This is the second line.
This is the third line.
)";
more_code();
IMHO, the above is flawed in breaking with the indentation of the surrounding code. Some languages provide a built-in or library function that does something like:
- removes an empty leading line, and
- looks at the indentation of the second line and removes the same amount of indentation from all further lines
That allows usage like:
some_code();
std::string text = unindent(R"(
This is the first line.
This is the second line.
This is the third line.
)");
more_code();
Writing such a function is relatively simple...
std::string unindent(const char* p)
{
std::string result;
if (*p == '\n') ++p;
const char* p_leading = p;
while (std::isspace(*p) && *p != '\n')
++p;
size_t leading_len = p - p_leading;
while (*p)
{
result += *p;
if (*p++ == '\n')
{
for (size_t i = 0; i < leading_len; ++i)
if (p[i] != p_leading[i])
goto dont_skip_leading;
p += leading_len;
}
dont_skip_leading: ;
}
return result;
}
(The slightly weird p_leading[i]
approach is intended to make life for people who use tabs and spaces no harder than they make it for themselves ;-P, as long as the lines start with the same sequence.)
This is probably not what you want, but just in case, you should be aware of automatic string literal concatenation:
std::string text =
"This is the first line.\n"
"This is the second line.\n"
"This is the third line.\n";
I recommend @Brian's answer, especially if you only need to have few lines of text, or that which you can handle with your text editor-fu. I have an alternative if that isn't the case.
std::string text =
"\
This is the first line." R"(
This is the second line.
This is the third line.)";
Live example
Raw string literals can still concatenate with "normal" string literals, as shown in the code. The "\
at the start is meant to "eliminate" the "
character from the first line, putting it in a line of its own instead.
Still, if I were to decide, I would put such lotsa-text into a separate file and load it at runtime. No pressure to you though :-).
Also, that is one of the uglier code I've written these days.