mirror of
https://github.com/pocoproject/poco.git
synced 2025-11-02 14:03:41 +01:00
GH #440 MongoDB ObjectId string formatting
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user