factoring out CPPEscape to be a standalone function

This commit is contained in:
Vern Paxson 2021-11-07 16:57:52 -08:00
parent 14abfc6831
commit bc3bf4ea6c
2 changed files with 64 additions and 0 deletions

View file

@ -75,4 +75,60 @@ void unlock_file(const string& fname, FILE* f)
}
}
string CPPEscape(const char* b, int len)
{
string res;
for ( int i = 0; i < len; ++i )
{
unsigned char c = b[i];
switch ( c )
{
case '\a':
res += "\\a";
break;
case '\b':
res += "\\b";
break;
case '\f':
res += "\\f";
break;
case '\n':
res += "\\n";
break;
case '\r':
res += "\\r";
break;
case '\t':
res += "\\t";
break;
case '\v':
res += "\\v";
break;
case '\\':
res += "\\\\";
break;
case '"':
res += "\\\"";
break;
default:
if ( isprint(c) )
res += c;
else
{
char buf[8192];
snprintf(buf, sizeof buf, "%03o", c);
res += "\\";
res += buf;
}
break;
}
}
return res;
}
} // zeek::detail

View file

@ -36,4 +36,12 @@ extern bool is_CPP_compilable(const ProfileFunc* pf, const char** reason = nullp
extern void lock_file(const std::string& fname, FILE* f);
extern void unlock_file(const std::string& fname, FILE* f);
// For the given byte array / string, returns a version expanded
// with escape sequences in order to represent it as a C++ string.
extern std::string CPPEscape(const char* b, int len);
inline std::string CPPEscape(const char* s)
{
return CPPEscape(s, strlen(s));
}
} // zeek::detail