22#include "pw_assert/assert.h"
23#include "pw_bytes/span.h"
24#include "pw_result/result.h"
25#include "pw_status/status.h"
26#include "pw_string/hex.h"
41 std::string_view{
"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"}.size();
59 {
reinterpret_cast<const uint8_t*
>(
span.data()),
span.size()});
66 Status status = ValidateString(
string);
78 constexpr bool operator==(
const Uuid& other)
const {
80 if (uuid_[i] != other.uuid_[i]) {
87 constexpr bool operator!=(
const Uuid& other)
const {
88 return !(*
this == other);
95 for (
size_t i = uuid_.size(); i-- != 0;) {
96 out += string::NibbleToHex(uuid_[i] >> 4);
97 out += string::NibbleToHex(uuid_[i] & 0xf);
98 if ((i == 12) || (i == 10) || (i == 8) || (i == 6)) {
106 constexpr explicit Uuid() : uuid_() {}
113 PW_ASSERT(
span.size() == uuid_.size());
114 for (
size_t i = 0; i < uuid_.size(); i++) {
122 constexpr explicit Uuid(std::string_view uuid_str) : uuid_() {
123 size_t out_hex_index = 2 * uuid_.size();
126 if (i == 8 || i == 13 || i == 18 || i == 23) {
127 PW_ASSERT(uuid_str[i] ==
'-');
130 PW_ASSERT(uuid_str[i] !=
'\0');
133 uint16_t value = string::HexToNibble(uuid_str[i]);
134 PW_ASSERT(value <= 0xf);
135 if (out_hex_index % 2 == 0) {
136 uuid_[out_hex_index / 2] |=
static_cast<uint8_t
>(value);
138 uuid_[out_hex_index / 2] =
static_cast<uint8_t
>(value << 4);
152 static constexpr Status ValidateString(std::string_view uuid_str) {
156 for (
size_t i = 0; i < uuid_str.size(); i++) {
157 if (i == 8 || i == 13 || i == 18 || i == 23) {
158 if (uuid_str[i] !=
'-') {
163 if (string::IsHexDigit(uuid_str[i]) == 0) {
178 static constexpr Status ValidateSpan(span<const uint8_t> span) {
185 std::array<uint8_t, kSizeBytes> uuid_;
pw::InlineBasicString is a fixed-capacity version of std::basic_string. In brief:
Definition: string.h:68
static constexpr Status InvalidArgument()
Argument was malformed; e.g. invalid characters when parsing integer.
Definition: status.h:131
constexpr bool ok() const
Definition: status.h:214
static constexpr Status FailedPrecondition()
System isn’t in the required state; e.g. deleting a non-empty directory.
Definition: status.h:162
Definition: span_impl.h:235
Represents a 128-bit universally unique identifier (UUID).
Definition: uuid.h:35
constexpr Status OkStatus()
Definition: status.h:297
static constexpr size_t kSizeBytes
Size of the UUID in bytes.
Definition: uuid.h:38
static Result< Uuid > FromSpan(ConstByteSpan span)
Definition: uuid.h:57
constexpr pw::span< const uint8_t, kSizeBytes > GetSpan() const
Return the backing span holding the uuid.
Definition: uuid.h:74
static constexpr Result< Uuid > FromString(std::string_view string)
Definition: uuid.h:65
static constexpr Result< Uuid > FromSpan(span< const uint8_t > span)
Definition: uuid.h:46
constexpr InlineString< kStringSize > ToString() const
Convert the Uuid to a human readable string.
Definition: uuid.h:92
static constexpr size_t kStringSize
Length of the UUID's string representation.
Definition: uuid.h:40
128-bit universally unique identifier library
Definition: uuid.h:30
pw::InlineBasicString and pw::InlineString are safer alternatives to std::basic_string and std::strin...