string - C++ escape control characters -
is there c++ function escape control characters in string? example, if input "first\r\nsecond" output should "first\\0x0d\\0x0asecond".
i haven't heard should relatively easy 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; (auto c: s) { if (replacementmap.find(c) != replacementmap.end()) { ss << replacementmap[c]; } else { ss << c; } } return ss.str(); }
Comments
Post a Comment