@@ -6,56 +6,59 @@ namespace DuckDB.NET.Data.Extensions;
6
6
7
7
internal static class GuidConverter
8
8
{
9
+ private const string GuidFormat = "D" ;
9
10
private static readonly char [ ] HexDigits = "0123456789abcdef" . ToCharArray ( ) ;
10
11
11
12
//Ported from duckdb source code UUID::ToString
12
13
//https://github.com/duckdb/duckdb/blob/9c91b3a329073ea1767b0aaff94b51da98dd03e2/src/common/types/uuid.cpp#L56
13
14
public static Guid ConvertToGuid ( this DuckDBHugeInt input )
14
15
{
15
- var buffer = new char [ 36 ] ;
16
-
17
- var upper = input . Upper ^ ( ( ( long ) 1 ) << 63 ) ;
16
+ Span < char > buffer = stackalloc char [ 36 ] ;
17
+ var num = input . Upper ^ long . MinValue ;
18
18
var position = 0 ;
19
-
20
- ByteToHex ( ( ulong ) ( upper >> 56 & 0xFF ) ) ;
21
- ByteToHex ( ( ulong ) ( upper >> 48 & 0xFF ) ) ;
22
- ByteToHex ( ( ulong ) ( upper >> 40 & 0xFF ) ) ;
23
- ByteToHex ( ( ulong ) ( upper >> 32 & 0xFF ) ) ;
24
-
19
+
20
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 56 ) & 0xFF ) ) ;
21
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 48 ) & 0xFF ) ) ;
22
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 40 ) & 0xFF ) ) ;
23
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 32 ) & 0xFF ) ) ;
24
+
25
25
buffer [ position ++ ] = '-' ;
26
-
27
- ByteToHex ( ( ulong ) ( upper >> 24 & 0xFF ) ) ;
28
- ByteToHex ( ( ulong ) ( upper >> 16 & 0xFF ) ) ;
29
-
26
+
27
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 24 ) & 0xFF ) ) ;
28
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 16 ) & 0xFF ) ) ;
29
+
30
30
buffer [ position ++ ] = '-' ;
31
-
32
- ByteToHex ( ( ulong ) ( upper >> 8 & 0xFF ) ) ;
33
- ByteToHex ( ( ulong ) ( upper & 0xFF ) ) ;
34
-
31
+
32
+ ByteToHex ( buffer , ref position , ( ulong ) ( ( num >> 8 ) & 0xFF ) ) ;
33
+ ByteToHex ( buffer , ref position , ( ulong ) ( num & 0xFF ) ) ;
34
+
35
35
buffer [ position ++ ] = '-' ;
36
-
37
- ByteToHex ( input . Lower >> 56 & 0xFF ) ;
38
- ByteToHex ( input . Lower >> 48 & 0xFF ) ;
39
-
36
+
37
+ ByteToHex ( buffer , ref position , ( input . Lower >> 56 ) & 0xFF ) ;
38
+ ByteToHex ( buffer , ref position , ( input . Lower >> 48 ) & 0xFF ) ;
39
+
40
40
buffer [ position ++ ] = '-' ;
41
-
42
- ByteToHex ( input . Lower >> 40 & 0xFF ) ;
43
- ByteToHex ( input . Lower >> 32 & 0xFF ) ;
44
- ByteToHex ( input . Lower >> 24 & 0xFF ) ;
45
- ByteToHex ( input . Lower >> 16 & 0xFF ) ;
46
- ByteToHex ( input . Lower >> 8 & 0xFF ) ;
47
- ByteToHex ( input . Lower & 0xFF ) ;
48
-
49
- return new Guid ( new string ( buffer ) ) ;
50
-
51
- void ByteToHex ( ulong value )
41
+
42
+ ByteToHex ( buffer , ref position , ( input . Lower >> 40 ) & 0xFF ) ;
43
+ ByteToHex ( buffer , ref position , ( input . Lower >> 32 ) & 0xFF ) ;
44
+ ByteToHex ( buffer , ref position , ( input . Lower >> 24 ) & 0xFF ) ;
45
+ ByteToHex ( buffer , ref position , ( input . Lower >> 16 ) & 0xFF ) ;
46
+ ByteToHex ( buffer , ref position , ( input . Lower >> 8 ) & 0xFF ) ;
47
+ ByteToHex ( buffer , ref position , input . Lower & 0xFF ) ;
48
+
49
+ #if NET6_0_OR_GREATER
50
+ return Guid . ParseExact ( buffer , GuidFormat ) ;
51
+ #else
52
+ return Guid . ParseExact ( new string ( buffer . ToArray ( ) ) , GuidFormat ) ;
53
+ #endif
54
+
55
+ static void ByteToHex ( Span < char > buffer , ref int position , ulong value )
52
56
{
53
- buffer [ position ++ ] = HexDigits [ ( value >> 4 ) & 0xf ] ;
54
- buffer [ position ++ ] = HexDigits [ value & 0xf ] ;
57
+ buffer [ position ++ ] = HexDigits [ ( value >> 4 ) & 0xF ] ;
58
+ buffer [ position ++ ] = HexDigits [ value & 0xF ] ;
55
59
}
56
60
}
57
61
58
-
59
62
//https://github.com/duckdb/duckdb/blob/9c91b3a329073ea1767b0aaff94b51da98dd03e2/src/common/types/uuid.cpp#L6
60
63
public static DuckDBHugeInt ToHugeInt ( this Guid guid )
61
64
{
0 commit comments