Raw Strings in C++

 

Sometimes Strings in C++  must be pampered and gently persuaded to display correctly.  Consider a string representing a small HTML block :

<h1 style="color: #5e9ca0;"><strong>welcome!</strong></h1>
<h4 style="color: #5e9ca0;">these are the available options:</h4>
<ol>
	<li>Reset Sensor</li>
	<li>Disengage power coupling</li>
</ol>

generating this output :

welcome!

these are the available options:

  1. Reset Sensor
  2. Disengage power coupling

if I  want to keep the HTML block in a string  literal,  I  can’t just copy and paste it from  a text editor (where I presumably created and tested it) . It will have to escaped it first, and then either split  to adjoining static strings (or have a very long one-line string):

std::string htmlMessage =  "<h1 style=\"color: #5e9ca0;\"><strong>welcome!</strong></h1>\n"
"<h4 style=\"color: #5e9ca0;\">these are the available options:</h4>\n"
"<ol>\n"
"<li>Reset Sensor</li>\n"
"<li>Disengage power coupling</li>\n"
"</ol>"

Fortunately, with C++ 11  and up this  kind of manual pre-processing can be a thing of the past:

 std::string htmlMessage = R"(<h1 style="color: #5e9ca0;"><strong>welcome!</strong></h1>
  <h4 style="color: #5e9ca0;">these are the available options:</h4>
  <ol>
	  <li>Reset Sensor</li>
	  <li>Disengage power coupling</li>
  </ol>)";

The unusual form R”(<actual string>)”  is the raw string modifier. When encountering  a raw string, the compiler will treat all characters in the string literal as simple  plain characters including normally escaped characters  . For example,  It will not attempt to translate ‘\t‘  into a tab character,  instead, the string literal will include the actual sequence “\t

Note how the left and right parenthesis inside the quotes are part of the raw string literal.

By having this slightly more complex definition  the raw string can include the ‘ ‘    character itself inside the string without confusing the compiler into believing  the string has terminated.

I am sure at least one of you ask: What if I want to include the  literal  )”  in my string ?     That standard has  you covered by having an optional delimiter string  at both ends of the string

So you can define the string as :

std::string rowString = R”C++Island(look ma “( and “) in a raw string!)C++Island!”

here the string  C++Island  comes between  the ‘“‘ and ‘(‘ or ‘)’   indicating to  the compiler to be out on the lookout for this specific  sequence signaling  the literal end.

The delimiter string can be anything you want as long as its 16 characters at most.

C++ raw string’s power become’s its  weakness when trying to express special character such as newline ‘\n‘ or tab ‘\t‘. However, you can always fall back on the old familiar  none-raw strings   you can also contact the two literals types together :

std::string str = R"(The '\n' character can easily be shown)" "\n"  "and used";
std::cout << str;


The ‘\n’ character can easily be shown
and used
[/color-box color]

Conclusions:

While not being a game changer in C++ it’s nice having the language  do the boring technical stuff  for me instead of the other way around.  I mean isn’t  that what software development is all about ?   plus all the other kids languages have  this feature, it about time we get to play with it as well.

 

Leave a Reply

Your email address will not be published. Required fields are marked *