Skip to content
| Marketplace
Sign in
Visual Studio>Tools>Refactor stringS to raw string
Refactor stringS to raw string

Refactor stringS to raw string

Loïc Joly

|
1,116 installs
| (0) | Free
This extension helps you refactor your C++ code that contains many classical string literals to leverage C++11 raw string literals. In visual studio 2015, there is already a tool to perform this kind of transformation, but it only handles one string at a time. This version wi...
Download

Description

Raw string literals

With C++11, the C++ standard allows a new way to write string literals, named Raw string literals. The content of these literals will not be interpreted, which removes the need to escape some characters, suck as double quotes, backslashes, tabultations or line breaks.

These raw string literals are especially useful when writing strings that contains many special characters, such as file paths, regex, or multiline text.

One common usage pattern for multiline text was to use the fact that two consecutive strings were merged into one to introduce line breaks into the source code to make the multiline text easier to read. This pattern is especially common in unit tests.

To start writing a raw string litteral, write R"xxx(, where xxx can be anything (even nothing). You can then write anything you want, and end your literal with )xxx" (the same xxx that was used to open it).

This extension

This extension allows to refactor classical string literals into raw string literals.

A similar feature is provided by default in Visual C++2015, but it only works one string at a time. This makes it really useful to refactor regex definitions, for instance, but it does not work well for multiline text, where we would like to merge many string literals into one big raw string literal.

This is exactly what this extension allows us to do. For instance, the following text:

C++
Edit|Remove
cplusplus
auto replacements =  "<regexPatterns>\n" "\t<pattern mode = \"Perl\">\n" "\t\t<searched text = \"C\\+\\+98\" />\n" "\t\t<replace text = \"C\\+\\+11\" />\n" "\t</pattern>\n" "</regexPatterns>\n";
auto replacements =   "<regexPatterns>\n"  "\t<pattern mode = \"Perl\">\n"  "\t\t<searched text = \"C\\+\\+98\" />\n"  "\t\t<replace text = \"C\\+\\+11\" />\n"  "\t</pattern>\n"  "</regexPatterns>\n";

Will be changed into:

C++
Modifier le script|Remove
cplusplus
auto replacements =  R"(<regexPatterns> 	<pattern mode = "Perl"> 		<searched text = "C\+\+98" /> 		<replace text = "C\+\+11" /> 	</pattern> </regexPatterns> )";
auto replacements =   R"(<regexPatterns>      <pattern mode = "Perl">          <searched text = "C\+\+98" />          <replace text = "C\+\+11" />      </pattern>  </regexPatterns>  )";
To run this refactoring tool, put the cursor or the selection on a string literal, use the light bulb (Ctrl-.) and select the refactoring. If you want to revert the modification, just undo your changes.
Here is a video of how it works:

Limitations

  • The goal of this extension is to work in the editor, not to perform batch operations on a file or a list of files. While it might be useful sometime, I believe not all strings should be converted to raw string literals, and I wanted to provide a way to make it really fast, not so do it indiscriminately.
  • As a consequence, if your selection contains several blocks of strings, only the first one will be converted.
  • This extension only works with the release version of Visual Studio. It will not work with release candidate versions.
  • I have not yet released the source code of this extension, but I plan to do it soon.
  • The refactoring provided by Visual Studio is still active, which means you might sometimes select the wrong one, since the names are similar and the Visual Studio version is first in the list. If anyone knows of a way to change that, I'd be happy to do it.
  • If you encounter any issue with this extension, please let me know!

Release notes

2016 08 21 Version 1.0.2

  • Correct infinite loop that appeared in some cases to detect start of string
  • Do not propose refactoring if the string is already a raw string literal

2012 08 10 Version 1.0.1

  • Correct a bug in the detection of the start of a list of strings Improve the technical documentation

2012 08 09 Version 1.0

  • Initial release
  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft