In log headers, only escape information when necessary.

This commit is contained in:
Robin Sommer 2011-12-19 08:44:41 -08:00
parent a9f0b10e2e
commit c1e656d89e
4 changed files with 24 additions and 14 deletions

View file

@ -42,22 +42,32 @@
#include "Reporter.h"
/**
* Takes a string, escapes each character into its equivalent hex code (\x##), and
* Takes a string, escapes characters into equivalent hex codes (\x##), and
* returns a string containing all escaped values.
*
* @param str string to escape
* @return A std::string containing a list of escaped hex values of the form \x##
*/
std::string get_escaped_string(const std::string& str)
* @param escape_all If true, all characters are escaped. If false, only
* characters are escaped that are either whitespace or not printable in
* ASCII.
* @return A std::string containing a list of escaped hex values of the form
* \x## */
std::string get_escaped_string(const std::string& str, bool escape_all)
{
char tbuf[16];
string esc = "";
for ( size_t i = 0; i < str.length(); ++i )
{
snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]);
esc += tbuf;
}
char c = str[i];
if ( escape_all || isspace(c) || ! isascii(c) || ! isprint(c) )
{
snprintf(tbuf, sizeof(tbuf), "\\x%02x", str[i]);
esc += tbuf;
}
else
esc += c;
}
return esc;
}