Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Raw string literals

Use case

Avoid escape hell in byte patterns.

Explanation

R"()" is a raw string (no escape sequences processed). Essential for regex patterns, YARA rules and byte signatures.

Code

#include <iostream>
#include <string>

int main() {
  std::string pattern1 = "\\xFD\\x7B\\xBF\\xA9";
  std::string pattern2 = R"(\xFD\x7B\xBF\xA9)";

  std::cout << "Escaped: " << pattern1 << "\n";
  std::cout << "Raw:     " << pattern2 << "\n";

  std::string rule = R"(
rule MachO {
    strings: $magic = { CE FA ED FE }
}
    )";

  std::cout << rule << "\n";

  return 0;
}

View on GitHub.

Output

$ ./src/c++11/build/raw-string-literals
Escaped: \xFD\x7B\xBF\xA9
Raw:     \xFD\x7B\xBF\xA9

rule MachO {
    strings: $magic = { CE FA ED FE }
}