17

I want to put this:

<script src = "Script2.js" type = "text/javascript"> < / script>

in a std::string so I append a (\) symbol before every double quotes (") to give it a literal meaning of ", instead of a string demarcation in C++ like this:

std::string jsFilesImport = "<script src = \"Script2.js\" type = \"text/javascript\"> < / script>\""

If I have a big string with many ("), adding (\) for every (") becomes difficult. Is there a simple way to achieve this in C++?

Bob
  • 15,441
  • 3
  • 26
  • 42
codeLover
  • 3,720
  • 10
  • 65
  • 121

1 Answers1

31

The easiest way is to use a raw string literal:

std::string s = R"x(<script src = "Script2.js" type = "text/javascript"> < / script>)x";
             // ^^^^                                                                ^^^

You just need to take care that the x( )x delimiters aren't used in the text provided inside. All other characters appearing within these delimiters are rendered as is (including newlines). x is arbitrarily chosen, could be something(, )somethingas well.


To prevent further questions regarding this:

No, it's not possible to do1 something like

std::string s = R"resource(
#include "MyRawTextResource.txt"
)resource";

The preprocessor won't recognize the #include "MyRawTextResource.txt" statement, because it's enclosed within a pair of double quotes (").

For such case consider to introduce your custom pre-build step doing the text replacement before compilation.


1)At least I wasn't able to find a workaround. Appreciate if someone proves me wrong about that.

user0042
  • 7,917
  • 3
  • 24
  • 39
  • @DanielH: Actually, you're fine having a closing parenthesis, as long as its not immediately followed by a `"`. – Benjamin Lindley Aug 17 '17 at 17:23
  • @BenjaminLindley Ah, you’re right. And `something_unlikely_to_occur` is too long according to the standard, but I wouldn’t be surprised if there were some compilers that accepted it anyway. – Daniel H Aug 17 '17 at 17:25
  • 1
    @Daniel Oh, _"and at most 16 characters long"_ yes you're right. – user0042 Aug 17 '17 at 17:26
  • Which is more than enough until we get 128-bit `size_t`; if you tried to craft a string which used every delimiter, you’d need [about 2¹⁰⁷](https://www.wolframalpha.com/input/?i=log2(sum+from+i+%3D+0+to+16+of+((i%2B2)+*+85%5Ei)) "WolframAlpha calculation") characters. – Daniel H Aug 17 '17 at 17:31
  • what is "x" here. Can it be simply R"string";. ? – codeLover Aug 17 '17 at 19:54
  • @codeLover `x` + the parenthesis (`(`) is the delimiter you can choose arbitrarily, how was I unclear about that (or the linked documentation)? _"Can it be simply `R"string"`"_ Nope. – user0042 Aug 17 '17 at 19:56
  • 3
    For XML, `]]><>` would be a reasonable delimiter, since that sequence of characters is guaranteed not to occur in well-formed XML. – Sneftel Aug 17 '17 at 20:04
  • @Sneftel They are arbitrary, though that's useful information specifically for XML formats. – user0042 Aug 17 '17 at 20:09
  • @Sneftel My point is that (aside OP asks for XML format specifically) this can be used quite universally (e.g. raw json text, where escaping the double quotes would be a PITA). – user0042 Aug 17 '17 at 20:12
  • 1
    Yeah, for JSON you'd want something like `a"a"`. It's just a matter of finding lacunas in the grammar. (You could also look over the string to find a character sequence that happened not to show up, but relying on the format is a more robust and maintainable approach.) – Sneftel Aug 17 '17 at 20:17
  • @Sneftel _"but relying on the format is a more robust and maintainable approach"_ Sure, that's a good idea (I mentioned that before). Maybe worth another additional answer, how to find appropriate delimiters according given grammars? – user0042 Aug 17 '17 at 20:19
  • 2
    "I wasn't able to find a workaround." Assuming a plain char array is fine and you don't care about MSVC: `extern char example[]; __asm__(".globl example; example: .incbin \"example.txt\"; .byte 0");` – Alcaro Aug 17 '17 at 21:12
  • @Alcaro OK, via inline assembly. That's implementation specific though, I'd prefer standard c++. – user0042 Aug 17 '17 at 21:15
  • @Sneftel Even in a string? – Deduplicator Aug 17 '17 at 21:53
  • @Deduplicator I believe so, yeah. CDATA delimiters are potent even in attribute values, and cannot nest. – Sneftel Aug 17 '17 at 21:57