1317027Sdim//===-- UUID.h --------------------------------------------------*- C++ -*-===//
2317027Sdim//
3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4353358Sdim// See https://llvm.org/LICENSE.txt for license information.
5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6317027Sdim//
7317027Sdim//===----------------------------------------------------------------------===//
8317027Sdim
9317027Sdim#ifndef LLDB_UTILITY_UUID_H
10317027Sdim#define LLDB_UTILITY_UUID_H
11317027Sdim
12360784Sdim#include "llvm/ADT/ArrayRef.h"
13360784Sdim#include "llvm/ADT/StringRef.h"
14317027Sdim#include <stddef.h>
15317027Sdim#include <stdint.h>
16317027Sdim#include <string>
17317027Sdim
18317027Sdimnamespace lldb_private {
19317027Sdim
20317027Sdim  class Stream;
21317027Sdim
22317027Sdimclass UUID {
23317027Sdimpublic:
24341825Sdim  UUID() = default;
25317027Sdim
26341825Sdim  /// Creates a UUID from the data pointed to by the bytes argument. No special
27341825Sdim  /// significance is attached to any of the values.
28341825Sdim  static UUID fromData(const void *bytes, uint32_t num_bytes) {
29341825Sdim    if (bytes)
30341825Sdim      return fromData({reinterpret_cast<const uint8_t *>(bytes), num_bytes});
31341825Sdim    return UUID();
32341825Sdim  }
33317027Sdim
34341825Sdim  /// Creates a uuid from the data pointed to by the bytes argument. No special
35341825Sdim  /// significance is attached to any of the values.
36341825Sdim  static UUID fromData(llvm::ArrayRef<uint8_t> bytes) { return UUID(bytes); }
37317027Sdim
38341825Sdim  /// Creates a UUID from the data pointed to by the bytes argument. Data
39341825Sdim  /// consisting purely of zero bytes is treated as an invalid UUID.
40341825Sdim  static UUID fromOptionalData(const void *bytes, uint32_t num_bytes) {
41341825Sdim    if (bytes)
42341825Sdim      return fromOptionalData(
43341825Sdim          {reinterpret_cast<const uint8_t *>(bytes), num_bytes});
44341825Sdim    return UUID();
45341825Sdim  }
46317027Sdim
47341825Sdim  /// Creates a UUID from the data pointed to by the bytes argument. Data
48341825Sdim  /// consisting purely of zero bytes is treated as an invalid UUID.
49341825Sdim  static UUID fromOptionalData(llvm::ArrayRef<uint8_t> bytes) {
50341825Sdim    if (llvm::all_of(bytes, [](uint8_t b) { return b == 0; }))
51341825Sdim      return UUID();
52341825Sdim    return UUID(bytes);
53341825Sdim  }
54317027Sdim
55341825Sdim  void Clear() { m_bytes.clear(); }
56341825Sdim
57317027Sdim  void Dump(Stream *s) const;
58317027Sdim
59341825Sdim  llvm::ArrayRef<uint8_t> GetBytes() const { return m_bytes; }
60317027Sdim
61341825Sdim  explicit operator bool() const { return IsValid(); }
62341825Sdim  bool IsValid() const { return !m_bytes.empty(); }
63317027Sdim
64341825Sdim  std::string GetAsString(llvm::StringRef separator = "-") const;
65317027Sdim
66318384Sdim  size_t SetFromStringRef(llvm::StringRef str, uint32_t num_uuid_bytes = 16);
67360784Sdim
68360784Sdim  // Same as SetFromStringRef, but if the resultant UUID is all 0 bytes, set the
69353358Sdim  // UUID to invalid.
70360784Sdim  size_t SetFromOptionalStringRef(llvm::StringRef str,
71353358Sdim                                  uint32_t num_uuid_bytes = 16);
72317027Sdim
73317027Sdim  // Decode as many UUID bytes (up to 16) as possible from the C string "cstr"
74317027Sdim  // This is used for auto completion where a partial UUID might have been
75317027Sdim  // typed in. It
76317027Sdim  /// Decode as many UUID bytes (up to 16) as possible from the C
77317027Sdim  /// string \a cstr.
78317027Sdim  ///
79360784Sdim  /// \param[in] str
80360784Sdim  ///     An llvm::StringRef that points at a UUID string value (no leading
81360784Sdim  ///     spaces). The string must contain only hex characters and optionally
82360784Sdim  ///     can contain the '-' sepearators.
83317027Sdim  ///
84353358Sdim  /// \param[in] uuid_bytes
85360784Sdim  ///     A buffer of bytes that will contain a full or partially decoded UUID.
86317027Sdim  ///
87353358Sdim  /// \return
88317027Sdim  ///     The original string, with all decoded bytes removed.
89317027Sdim  static llvm::StringRef
90341825Sdim  DecodeUUIDBytesFromString(llvm::StringRef str,
91341825Sdim                            llvm::SmallVectorImpl<uint8_t> &uuid_bytes,
92317027Sdim                            uint32_t num_uuid_bytes = 16);
93317027Sdim
94341825Sdimprivate:
95341825Sdim  UUID(llvm::ArrayRef<uint8_t> bytes) : m_bytes(bytes.begin(), bytes.end()) {}
96317027Sdim
97341825Sdim  // GNU ld generates 20-byte build-ids. Size chosen to avoid heap allocations
98341825Sdim  // for this case.
99341825Sdim  llvm::SmallVector<uint8_t, 20> m_bytes;
100317027Sdim
101341825Sdim  friend bool operator==(const UUID &LHS, const UUID &RHS) {
102341825Sdim    return LHS.m_bytes == RHS.m_bytes;
103341825Sdim  }
104341825Sdim  friend bool operator!=(const UUID &LHS, const UUID &RHS) {
105341825Sdim    return !(LHS == RHS);
106341825Sdim  }
107341825Sdim  friend bool operator<(const UUID &LHS, const UUID &RHS) {
108341825Sdim    return LHS.m_bytes < RHS.m_bytes;
109341825Sdim  }
110341825Sdim  friend bool operator<=(const UUID &LHS, const UUID &RHS) {
111341825Sdim    return !(RHS < LHS);
112341825Sdim  }
113341825Sdim  friend bool operator>(const UUID &LHS, const UUID &RHS) { return RHS < LHS; }
114341825Sdim  friend bool operator>=(const UUID &LHS, const UUID &RHS) {
115341825Sdim    return !(LHS < RHS);
116341825Sdim  }
117341825Sdim};
118317027Sdim} // namespace lldb_private
119317027Sdim
120317027Sdim#endif // LLDB_UTILITY_UUID_H
121