From b8dd71c5772870a6293226a09a9edb9b4569c367 Mon Sep 17 00:00:00 2001 From: "Albert ARIBAUD (3ADEV)" Date: Mon, 7 Dec 2015 18:11:13 +0100 Subject: [PATCH] Factorize toxml() into a single member function lib/symboldatabase.cpp and lib/tokenize.cpp both define a static toxml() function. Make it a single static ErrorLogger::toxml() member function. --- lib/errorlogger.cpp | 33 +++++++++++++++++++++++++++++++++ lib/errorlogger.h | 7 +++++++ lib/symboldatabase.cpp | 30 ++---------------------------- lib/tokenize.cpp | 37 ++----------------------------------- 4 files changed, 44 insertions(+), 63 deletions(-) diff --git a/lib/errorlogger.cpp b/lib/errorlogger.cpp index c18736d1a..7d8ddd08c 100644 --- a/lib/errorlogger.cpp +++ b/lib/errorlogger.cpp @@ -421,3 +421,36 @@ std::string ErrorLogger::ErrorMessage::FileLocation::stringify() const oss << ']'; return oss.str(); } + +std::string ErrorLogger::toxml(const std::string &str) +{ + std::ostringstream xml; + const bool isstring(str[0] == '\"'); + for (std::size_t i = 0U; i < str.length(); i++) { + char c = str[i]; + switch (c) { + case '<': + xml << "<"; + break; + case '>': + xml << ">"; + break; + case '&': + xml << "&"; + break; + case '\"': + xml << """; + break; + case '\0': + xml << "\\0"; + break; + default: + if (!isstring || (c >= ' ' && c <= 'z')) + xml << c; + else + xml << 'x'; + break; + } + } + return xml.str(); +} diff --git a/lib/errorlogger.h b/lib/errorlogger.h index 3b1061870..f8eedc7d0 100644 --- a/lib/errorlogger.h +++ b/lib/errorlogger.h @@ -311,6 +311,13 @@ public: void reportUnmatchedSuppressions(const std::list &unmatched); static std::string callStackToString(const std::list &callStack); + + /** + * Convert XML-sensitive characters into XML entities + * @param str The input string containing XML-sensitive characters + * @return The ouput string containing XML entities + */ + static std::string toxml(const std::string &str); }; /// @} diff --git a/lib/symboldatabase.cpp b/lib/symboldatabase.cpp index 9e08eea77..82449db09 100644 --- a/lib/symboldatabase.cpp +++ b/lib/symboldatabase.cpp @@ -2452,32 +2452,6 @@ void SymbolDatabase::printOut(const char *title) const std::cout << std::resetiosflags(std::ios::boolalpha); } -static std::string toxml(const std::string &str) -{ - std::ostringstream xml; - for (std::size_t i = 0U; i < str.length(); i++) { - char c = str[i]; - switch (c) { - case '<': - xml << "<"; - break; - case '>': - xml << ">"; - break; - case '&': - xml << "&"; - break; - case '\"': - xml << """; - break; - default: - xml << c; - break; - } - } - return xml.str(); -} - void SymbolDatabase::printXml(std::ostream &out) const { out << std::setiosflags(std::ios::boolalpha); @@ -2488,7 +2462,7 @@ void SymbolDatabase::printXml(std::ostream &out) const out << " id=\"" << &*scope << "\""; out << " type=\"" << scope->type << "\""; if (!scope->className.empty()) - out << " className=\"" << toxml(scope->className) << "\""; + out << " className=\"" << ErrorLogger::toxml(scope->className) << "\""; if (scope->classStart) out << " classStart=\"" << scope->classStart << '\"'; if (scope->classEnd) @@ -2504,7 +2478,7 @@ void SymbolDatabase::printXml(std::ostream &out) const if (!scope->functionList.empty()) { out << " " << std::endl; for (std::list::const_iterator function = scope->functionList.begin(); function != scope->functionList.end(); ++function) { - out << " tokenDef << "\" name=\"" << toxml(function->name()) << '\"'; + out << " tokenDef << "\" name=\"" << ErrorLogger::toxml(function->name()) << '\"'; if (function->argCount() == 0U) out << "/>" << std::endl; else { diff --git a/lib/tokenize.cpp b/lib/tokenize.cpp index 31c513cec..861ea0f4d 100644 --- a/lib/tokenize.cpp +++ b/lib/tokenize.cpp @@ -3911,39 +3911,6 @@ void Tokenizer::printDebugOutput(unsigned int simplification) const } } -static std::string toxml(const std::string &str) -{ - std::ostringstream xml; - const bool isstring(str[0] == '\"'); - for (std::size_t i = 0U; i < str.length(); i++) { - char c = str[i]; - switch (c) { - case '<': - xml << "<"; - break; - case '>': - xml << ">"; - break; - case '&': - xml << "&"; - break; - case '\"': - xml << """; - break; - case '\0': - xml << "\\0"; - break; - default: - if (!isstring || (c >= ' ' && c <= 'z')) - xml << c; - else - xml << 'x'; - break; - } - } - return xml.str(); -} - void Tokenizer::dump(std::ostream &out) const { // Create a xml data dump. @@ -3953,8 +3920,8 @@ void Tokenizer::dump(std::ostream &out) const // tokens.. out << " " << std::endl; for (const Token *tok = list.front(); tok; tok = tok->next()) { - out << " linenr() << '\"'; - out << " str=\"" << toxml(tok->str()) << '\"'; + out << " linenr() << '\"'; + out << " str=\"" << ErrorLogger::toxml(tok->str()) << '\"'; out << " scope=\"" << tok->scope() << '\"'; if (tok->isName()) out << " type=\"name\"";