DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
Guid.cpp
Go to the documentation of this file.
1#include "pch.h"
2#include "System/Guid.h"
3#include "System/String.h"
5#include <iomanip>
6#include <sstream>
7#include <algorithm>
8#include <cstdio>
9#include <cstring>
10
11namespace DotNetDupe {
12 namespace System {
13 const Guid Guid::Empty = Guid();
14
16 for (size_t i = 0; i < 16; ++i) {
17 _data[i] = 0;
18 }
19 }
20
22 if (b.GetLength() != 16) throw ArgumentException("Guid should be 16 bytes.");
23 for (size_t i = 0; i < 16; ++i) {
24 _data[i] = b[static_cast<int>(i)];
25 }
26 }
27
28 Guid::Guid(const String& g) {
30 String s = g.Replace("{", "").Replace("}", "").Replace("-", "");
31
33 if (s.GetLength() != 32) throw FormatException("Guid string should only contain 32 hexadecimal characters.");
34
36 for (size_t i = 0; i < 16; ++i) {
37 unsigned int byteVal;
38 String sub = s.Substring(static_cast<int>(i * 2), 2);
39#if defined(_WIN32)
40 if (sscanf_s(sub.GetRawString(), "%02x", &byteVal) != 1)
41#else
42 if (sscanf(sub.GetRawString(), "%02x", &byteVal) != 1)
43#endif
44 throw FormatException("Guid string should only contain 32 hexadecimal characters.");
45 _data[i] = (uint8_t)byteVal;
46 }
47 }
48
51 Array<uint8_t> data(16);
52 for (size_t i = 0; i < 16; ++i) {
53 data[static_cast<int>(i)] = static_cast<uint8_t>(rand() % 256);
54 }
55
57 data[6] = (data[6] & 0x0F) | 0x40;
58 data[8] = (data[8] & 0x3F) | 0x80;
59
61 return Guid(data);
62 }
63
65 Array<uint8_t> result(16);
66 for (size_t i = 0; i < 16; ++i) {
67 result[static_cast<int>(i)] = _data[i];
68 }
69 return result;
70 }
71
73 char buf[37];
74 snprintf(buf, 37, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
75 _data[0], _data[1], _data[2], _data[3],
76 _data[4], _data[5],
77 _data[6], _data[7],
78 _data[8], _data[9],
79 _data[10], _data[11], _data[12], _data[13], _data[14], _data[15]);
80 return String(buf);
81 }
82
83 bool Guid::operator==(const Guid& other) const {
84 return std::memcmp(_data, other._data, 16) == 0;
85 }
86
87 bool Guid::operator!=(const Guid& other) const {
88 return !(*this == other);
89 }
90 }
91}
Defines the exception thrown when the format of an argument is invalid or not compliant with specific...
Represents a globally unique identifier (GUID) mirroring .NET System.Guid.
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition Array.h:29
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition Array.h:142
FormatException(const String &sMessage)
Initializes a new instance of the FormatException class with a specified error message.
Represents a 128-bit globally unique identifier (GUID).
Definition Guid.h:22
String ToString() const
Returns a string representation of the value of this instance in standard "D" format (xxxxxxxx-xxxx-x...
Definition Guid.cpp:72
static const Guid Empty
A read-only instance of the Guid structure whose value is all zeros.
Definition Guid.h:40
Array< uint8_t > ToByteArray() const
Returns a 16-element byte array that contains the value of this instance.
Definition Guid.cpp:64
static Guid NewGuid()
Initializes a new instance of the Guid structure (RFC 4122 Version 4 random UUID).
Definition Guid.cpp:49
bool operator==(const Guid &other) const
Determines whether two Guid instances have identical values.
Definition Guid.cpp:83
bool operator!=(const Guid &other) const
Determines whether two Guid instances have differing values.
Definition Guid.cpp:87
Guid()
Initializes a new instance of the Guid structure initialized to all zeros.
Definition Guid.cpp:15
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
String Substring(int iStartIndex) const
Definition String.cpp:659
String Replace(char chOriginalChar, char chReplaceChar) const
Definition String.cpp:533
String()
Initializes a new instance of the String class to an empty string.
Definition String.cpp:64
const char * GetRawString() const
Definition String.cpp:230