From 8756dd45ef6a245f30eb23b678ca6d4f6ddab917 Mon Sep 17 00:00:00 2001 From: Jens Heuschkel Date: Thu, 17 Nov 2016 17:53:06 +0100 Subject: Add convenient macro escape function --- src/uscxml/util/String.cpp | 27 +++++++++++++++++++++++++++ src/uscxml/util/String.h | 1 + 2 files changed, 28 insertions(+) diff --git a/src/uscxml/util/String.cpp b/src/uscxml/util/String.cpp index 6d43301..894148d 100644 --- a/src/uscxml/util/String.cpp +++ b/src/uscxml/util/String.cpp @@ -25,6 +25,33 @@ namespace uscxml { #define ISWHITESPACE(char) (isspace(char)) +std::string macro_escaped(std::string const& s) { + std::string returnValue=""; + for (std::string::const_iterator iter = s.begin(), end = s.end(); iter != end; ++iter) { + char c = *iter; + if (' ' <= c and c <= '~' and c != '\\' and c != '"') { + returnValue += c; + } + else { + returnValue += '__'; + switch(c) { + case '"': returnValue += 'COLON'; break; + case '\\': returnValue += 'BACHSLASH'; break; + case '\t': returnValue += 'TAB'; break; + case '\r': returnValue += 'RETURN'; break; + case '\n': returnValue += 'NEWLINE'; break; + default: + char const* const hexdig = "0123456789ABCDEF"; + returnValue += 'x'; + returnValue += hexdig[c >> 4]; + returnValue += hexdig[c & 0xF]; + } + returnValue += '__'; + } + } + return returnValue; +} + std::list tokenize(const std::string& line, const char sep, bool trimWhiteSpace) { std::list tokens; diff --git a/src/uscxml/util/String.h b/src/uscxml/util/String.h index 5344245..b0832c8 100644 --- a/src/uscxml/util/String.h +++ b/src/uscxml/util/String.h @@ -25,6 +25,7 @@ namespace uscxml { +std::string macro_escaped(std::string const& s); std::list tokenize(const std::string& line, const char seperator = ' ', bool trimWhiteSpace = true); std::string spaceNormalize(const std::string& text); bool nameMatch(const std::string& eventDescs, const std::string& event); -- cgit v0.12