Added UUID random generation
This commit is contained in:
@@ -19,40 +19,71 @@
|
|||||||
|
|
||||||
#include "utils/UUID.hpp"
|
#include "utils/UUID.hpp"
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <iomanip>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
|
#include <sstream>
|
||||||
|
|
||||||
|
#include "utils/Random.hpp"
|
||||||
#include "utils/String.hpp"
|
#include "utils/String.hpp"
|
||||||
|
|
||||||
namespace StringUtils
|
namespace StringUtils
|
||||||
{
|
{
|
||||||
template <>
|
template <>
|
||||||
std::optional<UUID>
|
std::optional<UUID>
|
||||||
readAs(std::string_view str)
|
readAs(std::string_view str)
|
||||||
{
|
{
|
||||||
return UUID::fromString(str);
|
return UUID::fromString(str);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
namespace
|
||||||
static
|
|
||||||
bool
|
|
||||||
stringIsUUID(std::string_view str)
|
|
||||||
{
|
{
|
||||||
static const std::regex re { R"([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})"};
|
bool stringIsUUID(std::string_view str)
|
||||||
|
{
|
||||||
|
static const std::regex re{ R"([0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})" };
|
||||||
|
|
||||||
return std::regex_match(std::cbegin(str), std::cend(str), re);
|
return std::regex_match(std::cbegin(str), std::cend(str), re);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
UUID::UUID(std::string_view str)
|
UUID::UUID(std::string_view str)
|
||||||
: _value {StringUtils::stringToLower(str)}
|
: _value{ StringUtils::stringToLower(str) }
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
std::optional<UUID>
|
std::optional<UUID> UUID::fromString(std::string_view str)
|
||||||
UUID::fromString(std::string_view str)
|
|
||||||
{
|
{
|
||||||
if (!stringIsUUID(str))
|
if (!stringIsUUID(str))
|
||||||
return std::nullopt;
|
return std::nullopt;
|
||||||
|
|
||||||
return UUID {str};
|
return UUID{ str };
|
||||||
|
}
|
||||||
|
|
||||||
|
UUID UUID::generate()
|
||||||
|
{
|
||||||
|
// Form is "123e4567-e89b-12d3-a456-426614174000"
|
||||||
|
// TODO: store 128 bits and only convert to string when necessary
|
||||||
|
|
||||||
|
std::ostringstream oss;
|
||||||
|
|
||||||
|
auto concatRandomBytes{ [](std::ostream& os, std::size_t byteCount)
|
||||||
|
{
|
||||||
|
for (std::size_t i {}; i < byteCount; ++i)
|
||||||
|
os << std::hex << std::setfill('0') << std::setw(2) << static_cast<int>(Random::getRandom<std::uint8_t>(0, 255));
|
||||||
|
} };
|
||||||
|
|
||||||
|
concatRandomBytes(oss, 4);
|
||||||
|
oss << "-";
|
||||||
|
concatRandomBytes(oss, 2);
|
||||||
|
oss << "-";
|
||||||
|
concatRandomBytes(oss, 2);
|
||||||
|
oss << "-";
|
||||||
|
concatRandomBytes(oss, 2);
|
||||||
|
oss << "-";
|
||||||
|
concatRandomBytes(oss, 6);
|
||||||
|
|
||||||
|
const auto uuid{fromString(oss.str())};
|
||||||
|
assert(uuid);
|
||||||
|
return uuid.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,20 +27,21 @@
|
|||||||
|
|
||||||
class UUID
|
class UUID
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static std::optional<UUID> fromString(std::string_view str);
|
static std::optional<UUID> fromString(std::string_view str);
|
||||||
|
static UUID generate();
|
||||||
|
|
||||||
std::string_view getAsString() const { return _value; }
|
std::string_view getAsString() const { return _value; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UUID(std::string_view value);
|
UUID(std::string_view value);
|
||||||
std::string _value;
|
std::string _value;
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace StringUtils
|
namespace StringUtils
|
||||||
{
|
{
|
||||||
template <>
|
template <>
|
||||||
std::optional<UUID>
|
std::optional<UUID>
|
||||||
readAs(std::string_view str);
|
readAs(std::string_view str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user