diff options
Diffstat (limited to 'src/uscxml/util')
-rw-r--r-- | src/uscxml/util/String.cpp | 49 | ||||
-rw-r--r-- | src/uscxml/util/String.h | 11 |
2 files changed, 47 insertions, 13 deletions
diff --git a/src/uscxml/util/String.cpp b/src/uscxml/util/String.cpp index 6a80756..95d69e9 100644 --- a/src/uscxml/util/String.cpp +++ b/src/uscxml/util/String.cpp @@ -27,8 +27,8 @@ namespace uscxml { std::string escapeMacro(std::string const &s) { // inspired by http://stackoverflow.com/questions/2417588/escaping-a-c-string - std::string returnValue=""; - std::string specialChars=""; + std::string returnValue = ""; + std::string specialChars = ""; for (std::string::const_iterator iter = s.begin(), end = s.end(); iter != end; ++iter) { char c = *iter; if (('0' <= c && c <= '9') || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || c == '_') { @@ -47,7 +47,36 @@ std::string escapeMacro(std::string const &s) { return returnValue; } -std::list<std::string> tokenize(const std::string& line, const char sep, bool trimWhiteSpace) { +std::string toBinStr(size_t val, size_t margin) { + // inspired by http://www.cplusplus.com/forum/general/65862/ + + // mask has only the leftmost bit set. + size_t mask = 1u << (std::numeric_limits<unsigned>::digits - 1); + + // skip leading bits that are not set. + while (0 == (val & mask) && mask != 0) + mask >>= 1; // shift all bits to the right by 1 + + std::string binStr; + binStr.reserve(std::numeric_limits<unsigned>::digits + 1); + + do { + // add a '1' or '0' depending the current bit. + binStr += static_cast<char>(val & mask) + '0'; + + } while ((mask >>= 1) != 0); // next bit, when mask is 0 we've processed all bits + + // fill with leading 0 + if (binStr.size() < margin) { + for (size_t i = 0; i < (margin - binStr.size()); i++) { + binStr = "0" + binStr; + } + } + + return binStr; +} + +std::list<std::string> tokenize(const std::string &line, const char sep, bool trimWhiteSpace) { std::list<std::string> tokens; // appr. 3x faster than stringstream @@ -57,7 +86,7 @@ std::list<std::string> tokenize(const std::string& line, const char sep, bool tr if (i > 0 && start < i) { tokens.push_back(line.substr(start, i - start)); } - while(line[i] == sep || (trimWhiteSpace && ISWHITESPACE(line[i]))) { + while (line[i] == sep || (trimWhiteSpace && ISWHITESPACE(line[i]))) { i++; // skip multiple occurences of seperator and whitespaces } start = i; @@ -69,7 +98,7 @@ std::list<std::string> tokenize(const std::string& line, const char sep, bool tr return tokens; } -std::string spaceNormalize(const std::string& text) { +std::string spaceNormalize(const std::string &text) { std::stringstream content; #if 1 @@ -83,7 +112,7 @@ std::string spaceNormalize(const std::string& text) { content << seperator << text.substr(start, i - start); seperator = " "; } - while(isspace(text[++i])); // skip whitespaces + while (isspace(text[++i])); // skip whitespaces start = i; } else if (i + 1 == text.size()) { content << seperator << text.substr(start, i + 1 - start); @@ -93,7 +122,7 @@ std::string spaceNormalize(const std::string& text) { #else -// 291ms with test-performance-events.scml + // 291ms with test-performance-events.scml std::istringstream iss(text); std::string seperator; do { @@ -110,9 +139,9 @@ std::string spaceNormalize(const std::string& text) { } // see: http://www.w3.org/TR/scxml/#EventDescriptors -bool nameMatch(const std::string& eventDescs, const std::string& eventName) { +bool nameMatch(const std::string &eventDescs, const std::string &eventName) { #if 1 - if(eventDescs.length() == 0 || eventName.length() == 0) + if (eventDescs.length() == 0 || eventName.length() == 0) return false; // naive case of single descriptor and exact match @@ -126,7 +155,7 @@ bool nameMatch(const std::string& eventDescs, const std::string& eventName) { if (i > 0 && start < i - 1) { eventDesc = eventDescs.substr(start, i - start); } - while(isspace(eventDescs[++i])); // skip whitespaces + while (isspace(eventDescs[++i])); // skip whitespaces start = i; } else if (i + 1 == eventDescs.size()) { eventDesc = eventDescs.substr(start, i + 1 - start); diff --git a/src/uscxml/util/String.h b/src/uscxml/util/String.h index 8d9c9ef..974db3b 100644 --- a/src/uscxml/util/String.h +++ b/src/uscxml/util/String.h @@ -26,9 +26,14 @@ namespace uscxml { std::string escapeMacro(std::string const &s); -std::list<std::string> 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); + +std::string toBinStr(size_t val, size_t margin); + +std::list<std::string> 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); } |