mirror of
https://github.com/zeek/zeek.git
synced 2025-10-05 08:08:19 +00:00
factoring out CPPEscape to be a standalone function
This commit is contained in:
parent
14abfc6831
commit
bc3bf4ea6c
2 changed files with 64 additions and 0 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue