Reputation: 2534
Is there a C++ function to escape the control characters in a string? For example, if the input is "First\r\nSecond"
the output should be "First\\0x0D\\0x0ASecond"
.
Upvotes: 0
Views: 651
Reputation: 495
I haven't heard of using raw ANSI escape codes as codes in C or C++, but i do know that some like
/n
work for creating newlines, so perhaps use
#define ActualEscapeCode AsciiEscapeCode
to substitute?
Upvotes: 0
Reputation: 13988
I haven't heard about any but it should be relatively easy to implement one:
unordered_map<char, string> replacementmap;
void initreplecementmap() {
replacementmap['\''] = "\\0x27";
replacementmap['\"'] = "\\0x22";
replacementmap['\?'] = "\\0x3f";
replacementmap['\\'] = "\\\\";
replacementmap['\a'] = "\\0x07";
replacementmap['\b'] = "\\0x08";
replacementmap['\f'] = "\\0x0c";
replacementmap['\n'] = "\\0x0a";
replacementmap['\r'] = "\\0x0d";
replacementmap['\t'] = "\\0x09";
replacementmap['\v'] = "\\0x0b";
}
string replace_escape(string s) {
stringstream ss;
for (auto c: s) {
if (replacementmap.find(c) != replacementmap.end()) {
ss << replacementmap[c];
} else {
ss << c;
}
}
return ss.str();
}
Upvotes: 2
Reputation: 206567
Is there a C++ function to escape the control characters in a string?
By that, if you mean "Is there a standard library function that you can use to accomplish that?", the answer is no.
Upvotes: 1