Added function to unquote strings.

This commit is contained in:
Vincent Richard 2007-08-30 21:38:22 +00:00
parent 8066394538
commit fd0647db85
3 changed files with 53 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -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);
};