Added function to unquote strings.
This commit is contained in:
parent
8066394538
commit
fd0647db85
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
@ -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);
|
||||
};
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user