From d7870d672e3b28154ba5c231739b185ad41a57f7 Mon Sep 17 00:00:00 2001 From: Thomas Geymayer Date: Mon, 10 Jun 2013 21:35:27 +0200 Subject: [PATCH] strutils: move unescape and simplify starts_with/ends_with --- simgear/misc/strutils.cxx | 68 +++++++++++++++++++++++++++++++--- simgear/misc/strutils.hxx | 13 +++++++ simgear/misc/strutils_test.cxx | 4 +- 3 files changed, 79 insertions(+), 6 deletions(-) diff --git a/simgear/misc/strutils.cxx b/simgear/misc/strutils.cxx index d064ed6b..ff16e64c 100644 --- a/simgear/misc/strutils.cxx +++ b/simgear/misc/strutils.cxx @@ -204,15 +204,18 @@ namespace simgear { bool starts_with( const string & s, const string & substr ) - { - return s.find( substr ) == 0; + { + return s.compare(0, substr.length(), substr) == 0; } bool ends_with( const string & s, const string & substr ) - { - size_t n = s.rfind( substr ); - return (n != string::npos) && (n == s.length() - substr.length()); + { + if( substr.length() > s.length() ) + return false; + return s.compare( s.length() - substr.length(), + substr.length(), + substr ) == 0; } string simplify(const string& s) @@ -426,6 +429,61 @@ std::string encodeHex(const unsigned char* rawBytes, unsigned int length) return hex; } +//------------------------------------------------------------------------------ +std::string unescape(const char* s) +{ + std::string r; + while( *s ) + { + if( *s != '\\' ) + { + r += *s++; + continue; + } + + if( !*++s ) + break; + + if (*s == '\\') { + r += '\\'; + } else if (*s == 'n') { + r += '\n'; + } else if (*s == 'r') { + r += '\r'; + } else if (*s == 't') { + r += '\t'; + } else if (*s == 'v') { + r += '\v'; + } else if (*s == 'f') { + r += '\f'; + } else if (*s == 'a') { + r += '\a'; + } else if (*s == 'b') { + r += '\b'; + } else if (*s == 'x') { + if (!*++s) + break; + int v = 0; + for (int i = 0; i < 2 && isxdigit(*s); i++, s++) + v = v * 16 + (isdigit(*s) ? *s - '0' : 10 + tolower(*s) - 'a'); + r += v; + continue; + + } else if (*s >= '0' && *s <= '7') { + int v = *s++ - '0'; + for (int i = 0; i < 3 && *s >= '0' && *s <= '7'; i++, s++) + v = v * 8 + *s - '0'; + r += v; + continue; + + } else { + r += *s; + } + s++; + } + return r; +} + } // end namespace strutils } // end namespace simgear diff --git a/simgear/misc/strutils.hxx b/simgear/misc/strutils.hxx index d2c8a3e8..73e24fc4 100644 --- a/simgear/misc/strutils.hxx +++ b/simgear/misc/strutils.hxx @@ -168,6 +168,19 @@ namespace simgear { std::string encodeHex(const std::string& bytes); std::string encodeHex(const unsigned char* rawBytes, unsigned int length); + + /** + * Unescape string. + * + * @param str String possibly containing escaped characters. + * @return string with escaped characters replaced by single character + * values. + */ + std::string unescape(const char* str); + + inline std::string unescape(const std::string& str) + { return unescape(str.c_str()); } + } // end namespace strutils } // end namespace simgear diff --git a/simgear/misc/strutils_test.cxx b/simgear/misc/strutils_test.cxx index 6387b47c..baba43ca 100644 --- a/simgear/misc/strutils_test.cxx +++ b/simgear/misc/strutils_test.cxx @@ -76,7 +76,9 @@ int main (int ac, char ** av) std::string j = join(la, "&"); COMPARE(j, "zero&one&two&three&four&five"); - + + COMPARE(unescape("\\ \\n\\t\\x41\\117a"), " \n\tAOa"); + cout << "all tests passed successfully!" << endl; return 0; } -- 2.39.5