@@ -6,56 +6,59 @@ namespace DuckDB.NET.Data.Extensions;
66
77internal static class GuidConverter
88{
9+ private const string GuidFormat = "D" ;
910 private static readonly char [ ] HexDigits = "0123456789abcdef" . ToCharArray ( ) ;
1011
1112 //Ported from duckdb source code UUID::ToString
1213 //https://github.com/duckdb/duckdb/blob/9c91b3a329073ea1767b0aaff94b51da98dd03e2/src/common/types/uuid.cpp#L56
1314 public static Guid ConvertToGuid ( this DuckDBHugeInt input )
1415 {
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 ;
1818 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+
2525 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+
3030 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+
3535 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+
4040 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 )
5256 {
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 ] ;
5559 }
5660 }
5761
58-
5962 //https://github.com/duckdb/duckdb/blob/9c91b3a329073ea1767b0aaff94b51da98dd03e2/src/common/types/uuid.cpp#L6
6063 public static DuckDBHugeInt ToHugeInt ( this Guid guid )
6164 {
0 commit comments