50inline std::string
StrEscape(
const char* data,
size_t size,
bool use_octal_escape =
false,
51 bool escape_whitespace_special_chars =
true) {
52 std::ostringstream stream;
53 for (
size_t i = 0; i < size; ++i) {
54 unsigned char c = data[i];
55 if (c >=
' ' && c <=
'~' && c !=
'\\' && c !=
'"') {
60 stream <<
'\\' <<
'"';
63 stream <<
'\\' <<
'\\';
66 if (escape_whitespace_special_chars) {
67 stream <<
'\\' <<
't';
73 if (escape_whitespace_special_chars) {
74 stream <<
'\\' <<
'r';
80 if (escape_whitespace_special_chars) {
81 stream <<
'\\' <<
'n';
87 if (use_octal_escape) {
88 stream << '\\' << static_cast<unsigned char>(
'0' + ((c >> 6) & 0x03))
89 <<
static_cast<unsigned char>(
'0' + ((c >> 3) & 0x07))
90 <<
static_cast<unsigned char>(
'0' + (c & 0x07));
92 const char* hex_digits =
"0123456789ABCDEF";
93 stream <<
'\\' <<
'x' << hex_digits[c >> 4] << hex_digits[c & 0xf];
std::string StrEscape(const char *data, size_t size, bool use_octal_escape=false, bool escape_whitespace_special_chars=true)
Create a stream with escape.
Definition str_escape.h:50