diff --git a/src/utility/stringUtils.cpp b/src/utility/stringUtils.cpp index 2455cfff..09c7f8f4 100644 --- a/src/utility/stringUtils.cpp +++ b/src/utility/stringUtils.cpp @@ -151,5 +151,41 @@ const string::size_type stringUtils::countASCIIchars } +const string stringUtils::unquote(const string& str) +{ + if (str.length() < 2) + return str; + + if (str[0] != '"' || str[str.length() - 1] != '"') + return str; + + string res; + res.reserve(str.length()); + + bool escaped = false; + + for (string::const_iterator it = str.begin() + 1, end = str.end() - 1 ; it != end ; ++it) + { + const string::value_type c = *it; + + if (escaped) + { + res += c; + escaped = false; + } + else if (!escaped && c == '\\') + { + escaped = true; + } + else + { + res += c; + } + } + + return res; +} + + } // utility } // vmime diff --git a/tests/utility/stringUtilsTest.cpp b/tests/utility/stringUtilsTest.cpp index a75b8ad7..8b60112d 100644 --- a/tests/utility/stringUtilsTest.cpp +++ b/tests/utility/stringUtilsTest.cpp @@ -42,6 +42,8 @@ VMIME_TEST_SUITE_BEGIN VMIME_TEST(testTrim) VMIME_TEST(testCountASCIIChars) + + VMIME_TEST(testUnquote) VMIME_TEST_LIST_END @@ -119,5 +121,13 @@ VMIME_TEST_SUITE_BEGIN stringUtils::countASCIIchars(str4.begin(), str4.end())); } + void testUnquote() + { + VASSERT_EQ("1", "quoted", stringUtils::unquote("\"quoted\"")); // "quoted" + VASSERT_EQ("2", "\"not quoted", stringUtils::unquote("\"not quoted")); // "not quoted + VASSERT_EQ("3", "not quoted\"", stringUtils::unquote("not quoted\"")); // not quoted" + VASSERT_EQ("4", "quoted with \"escape\"", stringUtils::unquote("\"quoted with \\\"escape\\\"\"")); // "quoted with \"escape\"" + } + VMIME_TEST_SUITE_END diff --git a/vmime/utility/stringUtils.hpp b/vmime/utility/stringUtils.hpp index 59db56d6..29e57139 100644 --- a/vmime/utility/stringUtils.hpp +++ b/vmime/utility/stringUtils.hpp @@ -138,6 +138,13 @@ public: return (ret); } + + /** Unquote the specified string and transform escaped characters. + * + * @param string from which to remove quotes + * @return unquoted string + */ + static const string unquote(const string& str); };