GH #440 MongoDB ObjectId string formatting

This commit is contained in:
Alex Fabijanic
2014-05-08 21:35:56 -05:00
parent 74176f7c90
commit 556d8cf1c4
5 changed files with 103 additions and 70 deletions

View File

@@ -24,8 +24,18 @@ namespace Poco {
namespace MongoDB {
ObjectId::ObjectId()
ObjectId::ObjectId(const std::string& id)
{
if (id.size() == 12)
{
std::string::const_iterator it = id.begin();
std::string::const_iterator end = id.end();
for (int i = 0; i < 12; ++it, ++i) _id[i] = *it;
}
else if (id.size())
{
throw Poco::InvalidArgumentException("ID must be 12 characters long.");
}
}
@@ -34,15 +44,16 @@ ObjectId::~ObjectId()
}
std::string ObjectId::toString() const
std::string ObjectId::toString(const std::string& fmt) const
{
std::string s;
for(int i = 0; i < 12; ++i)
{
s += format("%x", (unsigned int) _id[i]);
s += format(fmt, (unsigned int) _id[i]);
}
return s;
}
} } // namespace Poco::MongoDB