Path to/from string conversion.
This commit is contained in:
parent
5ec4ea4aa2
commit
58bad6e488
@ -261,5 +261,52 @@ path::component& path::getComponentAt(const size_t pos)
|
||||
}
|
||||
|
||||
|
||||
// static
|
||||
path path::fromString(const string& str, const string& sep, const charset& cset)
|
||||
{
|
||||
path p;
|
||||
|
||||
size_t start = 0;
|
||||
size_t end = 0;
|
||||
|
||||
do
|
||||
{
|
||||
end = str.find(sep, start);
|
||||
|
||||
string comp;
|
||||
|
||||
if (end == string::npos)
|
||||
comp = str.substr(start);
|
||||
else
|
||||
comp = str.substr(start, end - start);
|
||||
|
||||
// Skip leading or trailing separators
|
||||
if (comp.length())
|
||||
p.appendComponent(component(comp, cset));
|
||||
|
||||
start = end + 1;
|
||||
}
|
||||
while (end != string::npos);
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
|
||||
const string path::toString(const string& sep, const charset& cset)
|
||||
{
|
||||
string str;
|
||||
|
||||
for (size_t i = 0 ; i < m_list.size() ; ++i)
|
||||
{
|
||||
if (i != 0)
|
||||
str += sep;
|
||||
|
||||
str += m_list[i].getConvertedText(cset);
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
} // utility
|
||||
} // vmime
|
||||
|
@ -158,6 +158,25 @@ public:
|
||||
*/
|
||||
void renameParent(const path& oldPath, const path& newPath);
|
||||
|
||||
/** Construct a new path from a string.
|
||||
*
|
||||
* @param str string representation of the path
|
||||
* @param sep separator string (eg: "/")
|
||||
* @param cset charset in which the path is encoded (use the value returned by
|
||||
* vmime::charset::getLocalCharset() to use the default charset of your system)
|
||||
* @return a new path corresponding to the specified string
|
||||
*/
|
||||
static path fromString(const string& str, const string& sep, const charset& cset);
|
||||
|
||||
/** Returns a string representation of this path.
|
||||
*
|
||||
* @param sep separator string (eg: "/")
|
||||
* @param cset charset in which to encode the components (use the value returned by
|
||||
* vmime::charset::getLocalCharset() to use the default charset of your system)
|
||||
* @return a string representing this path
|
||||
*/
|
||||
const string toString(const string& sep, const charset& cset);
|
||||
|
||||
private:
|
||||
|
||||
list m_list;
|
||||
|
@ -53,6 +53,10 @@ VMIME_TEST_SUITE_BEGIN(utilityPathTest)
|
||||
VMIME_TEST(testIsParentOf_EquivalentCharset)
|
||||
|
||||
VMIME_TEST(testRenameParent)
|
||||
|
||||
VMIME_TEST(testFromString)
|
||||
VMIME_TEST(testFromString_IgnoreLeadingOrTrailingSep)
|
||||
VMIME_TEST(testToString)
|
||||
VMIME_TEST_LIST_END
|
||||
|
||||
|
||||
@ -313,5 +317,41 @@ VMIME_TEST_SUITE_BEGIN(utilityPathTest)
|
||||
VASSERT_EQ("6", "d", p.getComponentAt(4).getBuffer());
|
||||
}
|
||||
|
||||
void testFromString()
|
||||
{
|
||||
path p = path::fromString("ab/cde/f", "/", vmime::charset("my-charset"));
|
||||
|
||||
VASSERT_EQ("count", 3, p.getSize());
|
||||
VASSERT_EQ("buffer1", "ab", p.getComponentAt(0).getBuffer());
|
||||
VASSERT_EQ("charset1", "my-charset", p.getComponentAt(0).getCharset().getName());
|
||||
VASSERT_EQ("buffer2", "cde", p.getComponentAt(1).getBuffer());
|
||||
VASSERT_EQ("charset2", "my-charset", p.getComponentAt(1).getCharset().getName());
|
||||
VASSERT_EQ("buffer3", "f", p.getComponentAt(2).getBuffer());
|
||||
VASSERT_EQ("charset3", "my-charset", p.getComponentAt(2).getCharset().getName());
|
||||
}
|
||||
|
||||
void testFromString_IgnoreLeadingOrTrailingSep()
|
||||
{
|
||||
path p = path::fromString("//ab/cde/f////", "/", vmime::charset("my-charset"));
|
||||
|
||||
VASSERT_EQ("count", 3, p.getSize());
|
||||
VASSERT_EQ("buffer1", "ab", p.getComponentAt(0).getBuffer());
|
||||
VASSERT_EQ("charset1", "my-charset", p.getComponentAt(0).getCharset().getName());
|
||||
VASSERT_EQ("buffer2", "cde", p.getComponentAt(1).getBuffer());
|
||||
VASSERT_EQ("charset2", "my-charset", p.getComponentAt(1).getCharset().getName());
|
||||
VASSERT_EQ("buffer3", "f", p.getComponentAt(2).getBuffer());
|
||||
VASSERT_EQ("charset3", "my-charset", p.getComponentAt(2).getCharset().getName());
|
||||
}
|
||||
|
||||
void testToString()
|
||||
{
|
||||
path p;
|
||||
p.appendComponent(comp("ab"));
|
||||
p.appendComponent(comp("cde"));
|
||||
p.appendComponent(comp("f"));
|
||||
|
||||
VASSERT_EQ("string", "ab/cde/f", p.toString("/", vmime::charset("us-ascii")));
|
||||
}
|
||||
|
||||
VMIME_TEST_SUITE_END
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user