0

Is there a simple way to escape all occurrences of \ in a string? I start with the following string:

#include <string>
#include <iostream>

std::string escapeSlashes(std::string str) {
    // I have no idea what to do here
    return str;
}

int main () {
    std::string str = "a\b\c\d";

    std::cout << escapeSlashes(str) << "\n";
    // Desired output:
    // a\\b\\c\\d

    return 0;
}

Basically, I am looking for the inverse to this question. The problem is that I cannot search for \ in the string, because C++ already treats it as an escape sequence.

NOTE: I am not able to change the string str in the first place. It is parsed from a LaTeX file. Thus, this answers to a similar question does not apply. Edit: The parsing failed due to an unrelated problem, the question here is about string literals.

Edit: There are nice solutions to find and replace known escape sequences, such as this answer. Another option is to use boost::regex("\p{cntrl}"). However, I haven't found one that works for unknown (erroneous) escape sequences.

Cœur
  • 32,421
  • 21
  • 173
  • 232
Julian Helfferich
  • 960
  • 2
  • 12
  • 27
  • 2
    How about you show a sample of the file and your parsing code? – spectras Mar 22 '18 at 10:38
  • Add another \ before every slash and try – Rajesh Mar 22 '18 at 10:39
  • To search for special characters put a `\ ` in front of them, eg `\\ `. You don't need to modify the *source* data. – Panagiotis Kanavos Mar 22 '18 at 10:40
  • BTW you don't need to "escape" the source data either. It's just fine as it is. If you try to inspect the characters in the debugger you'll see that the `\ ` character is displayed as `\\ `. Escaping is needed only for string literals in the source code. If you used `cout << str` you'd see the original string without modifications – Panagiotis Kanavos Mar 22 '18 at 10:42
  • I would prefer not to touch the parsing code. A related question would be: Is there a way to search for *any* escape character in a string? – Julian Helfferich Mar 22 '18 at 10:50
  • What do you mean by *"It is parsed from a LaTeX file"*? Have you got a script that extracts strings from your LaTeX sources and produces C++ source as output? (In which case, fix the script.) Or is your C++ code reading LaTeX as input? (In which case, you'll already have literal `\ ` characters in your string, and your example code is not representative of what you're doing.) – Toby Speight Mar 22 '18 at 13:16
  • I found the problem with the parser and it was not related to this question. When trying to understand the problem by using string literals instead of the parsed string, I only made it worse. I will remove the note as it is misleading. – Julian Helfferich Mar 22 '18 at 13:38

2 Answers2

8

You can use raw string literal. See http://en.cppreference.com/w/cpp/language/string_literal

#include <string>
#include <iostream>

int main() {
    std::string str = R"(a\b\c\d)";

    std::cout << str << "\n";
    return 0;
}

Output:

a\b\c\d
Saleem
  • 7,965
  • 2
  • 16
  • 31
1

It is not possible to convert the string literal a\b\c\d to a\\b\\c\\d, i.e. escaping the backslashes.

Why? Because the compiler converts \c and \d directly to c and d, respectively, giving you a warning about Unknown escape sequence \c and Unknown escape sequence \d (\b is fine as it is a valid escape sequence). This happens directly to the string literal before you have any chance to work with it.

To see this, you can compile to assembler

gcc -S main.cpp

and you will find the following line somewhere in your assembler code:

.string "a\bcd"

Thus, your problem is either in your parsing function or you use string literals for experimenting and you should use raw strings R"(a\b\c\d)" instead.

Julian Helfferich
  • 960
  • 2
  • 12
  • 27