From 6e835338bdd2411ff31fb90a6d6675ae23b15dc5 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Tue, 12 Feb 2008 18:04:11 +0000 Subject: Quote all non-printable ASCII characters (not just control characters) git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@620889 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/qpid/log/Statement.cpp | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'cpp/src/qpid/log/Statement.cpp') diff --git a/cpp/src/qpid/log/Statement.cpp b/cpp/src/qpid/log/Statement.cpp index 2935de9071..9b6fb7feaf 100644 --- a/cpp/src/qpid/log/Statement.cpp +++ b/cpp/src/qpid/log/Statement.cpp @@ -22,6 +22,7 @@ #include #include #include +#include namespace qpid { namespace log { @@ -29,21 +30,21 @@ namespace log { namespace { using namespace std; -struct IsControl { bool operator()(unsigned char c) { return c < 32; } }; +struct NonPrint { bool operator()(unsigned char c) { return !isprint(c); } }; -bool isClean(const std::string& str) { - return std::find_if(str.begin(), str.end(), IsControl()) == str.end(); -} +char hex[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; std::string quote(const std::string& str) { - IsControl isControl; - size_t n = std::count_if(str.begin(), str.end(), isControl); + NonPrint nonPrint; + size_t n = std::count_if(str.begin(), str.end(), nonPrint); + if (n==0) return str; std::string ret; - ret.reserve(str.size()+n); // Avoid extra allocations. + ret.reserve(str.size()+2*n); // Avoid extra allocations. for (string::const_iterator i = str.begin(); i != str.end(); ++i) { - if (isControl(*i)) { - ret.push_back('^'); - ret.push_back((*i)+64); + if (nonPrint(*i)) { + ret.push_back('\\'); + ret.push_back(hex[((*i) >> 4)&0xf]); + ret.push_back(hex[(*i) & 0xf]); } else ret.push_back(*i); } @@ -53,7 +54,7 @@ std::string quote(const std::string& str) { } void Statement::log(const std::string& message) { - Logger::instance().log(*this, isClean(message) ? message : quote(message)); + Logger::instance().log(*this, quote(message)); } Statement::Initializer::Initializer(Statement& s) : statement(s) { -- cgit v1.2.1