DotNetDupe
4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Toggle main menu visibility
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
"
4
#include "
System/FormatException.h
"
5
#include <iomanip>
6
#include <sstream>
7
#include <algorithm>
8
#include <cstdio>
9
#include <cstring>
10
11
namespace
DotNetDupe
{
12
namespace
System
{
13
const
Guid
Guid::Empty
=
Guid
();
14
15
Guid::Guid
() {
16
for
(
size_t
i = 0; i < 16; ++i) {
17
_data[i] = 0;
18
}
19
}
20
21
Guid::Guid
(
const
Array<uint8_t>
& b) {
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
49
Guid
Guid::NewGuid
() {
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
64
Array<uint8_t>
Guid::ToByteArray
()
const
{
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
72
String
Guid::ToString
()
const
{
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
}
FormatException.h
Defines the exception thrown when the format of an argument is invalid or not compliant with specific...
Guid.h
Represents a globally unique identifier (GUID) mirroring .NET System.Guid.
String.h
High-performance UTF-8 / UTF-16 string manipulation class mirroring .NET System.String.
DotNetDupe::System::ArgumentException::ArgumentException
ArgumentException(const String &sMessage)
Initializes a new instance of the ArgumentException class with a specified error message.
Definition
ArgumentException.h:20
DotNetDupe::System::Array
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition
Array.h:29
DotNetDupe::System::Array::GetLength
int GetLength() const
Gets the total number of elements in all dimensions of the Array.
Definition
Array.h:142
DotNetDupe::System::FormatException::FormatException
FormatException(const String &sMessage)
Initializes a new instance of the FormatException class with a specified error message.
Definition
FormatException.h:20
DotNetDupe::System::Guid
Represents a 128-bit globally unique identifier (GUID).
Definition
Guid.h:22
DotNetDupe::System::Guid::ToString
String ToString() const
Returns a string representation of the value of this instance in standard "D" format (xxxxxxxx-xxxx-x...
Definition
Guid.cpp:72
DotNetDupe::System::Guid::Empty
static const Guid Empty
A read-only instance of the Guid structure whose value is all zeros.
Definition
Guid.h:40
DotNetDupe::System::Guid::ToByteArray
Array< uint8_t > ToByteArray() const
Returns a 16-element byte array that contains the value of this instance.
Definition
Guid.cpp:64
DotNetDupe::System::Guid::NewGuid
static Guid NewGuid()
Initializes a new instance of the Guid structure (RFC 4122 Version 4 random UUID).
Definition
Guid.cpp:49
DotNetDupe::System::Guid::operator==
bool operator==(const Guid &other) const
Determines whether two Guid instances have identical values.
Definition
Guid.cpp:83
DotNetDupe::System::Guid::operator!=
bool operator!=(const Guid &other) const
Determines whether two Guid instances have differing values.
Definition
Guid.cpp:87
DotNetDupe::System::Guid::Guid
Guid()
Initializes a new instance of the Guid structure initialized to all zeros.
Definition
Guid.cpp:15
DotNetDupe::System::String
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition
String.h:74
DotNetDupe::System::String::Substring
String Substring(int iStartIndex) const
Definition
String.cpp:659
DotNetDupe::System::String::GetLength
int GetLength() const
Definition
String.cpp:247
DotNetDupe::System::String::Replace
String Replace(char chOriginalChar, char chReplaceChar) const
Definition
String.cpp:533
DotNetDupe::System::String::String
String()
Initializes a new instance of the String class to an empty string.
Definition
String.cpp:64
DotNetDupe::System::String::GetRawString
const char * GetRawString() const
Definition
String.cpp:230
DotNetDupe::System
Definition
Action.h:11
DotNetDupe
Definition
IServiceCollection.h:7
pch.h
DotNetDupe
Guid.cpp
Generated by
1.18.0