ObjectId constructor translates a hex string (24 characters) into a 12 byte object id

This commit is contained in:
fbraem
2014-12-20 19:11:16 +01:00
parent 9f7076d7a7
commit eb879985b0
3 changed files with 46 additions and 28 deletions

View File

@@ -15,7 +15,6 @@
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/MongoDB/ObjectId.h"
#include "Poco/Format.h"
@@ -23,21 +22,26 @@
namespace Poco {
namespace MongoDB {
ObjectId::ObjectId()
{
memset(_id, 0, sizeof(_id));
}
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.");
poco_assert_dbg(id.size() == 24);
const char *p = id.c_str();
for (std::size_t i = 0; i < 12; ++i) {
_id[i] = fromHex(p);
p += 2;
}
}
ObjectId::ObjectId(const ObjectId& copy)
{
memcpy(_id, copy._id, sizeof(_id));
}
ObjectId::~ObjectId()
{