Skip to content

Commit 6c3cfdf

Browse files
committed
Add toBytes / ofBytes for Guid
1 parent 62b17cd commit 6c3cfdf

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/FSharpPlus/Control/Converter.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ type OfBytes =
4949
static member OfBytes (_: int64 , _: OfBytes) = fun (x, i, e) -> BitConverter.ToInt64 (x, i, e)
5050
static member OfBytes (_: float32, _: OfBytes) = fun (x, i, e) -> BitConverter.ToSingle (x, i, e)
5151

52-
static member OfBytes (_: string , _: OfBytes) = fun (x, i, _) -> BitConverter.ToString (x, i)
52+
static member OfBytes (_: string , _: OfBytes) = fun (x, i, _) -> BitConverter.ToString (x, i)
53+
static member OfBytes (_: Guid , _: OfBytes) = fun (x, i, e) -> BitConverter.ToGuid (x, i, e)
5354

5455
static member OfBytes (_: uint16 , _: OfBytes) = fun (x, i, e) -> BitConverter.ToUInt16 (x, i, e)
5556
static member OfBytes (_: uint32 , _: OfBytes) = fun (x, i, e) -> BitConverter.ToUInt32 (x, i, e)
@@ -70,6 +71,7 @@ type ToBytes =
7071
static member ToBytes (x: int64 , e, _: ToBytes) = BitConverter.GetBytes (x, BitConverter.IsLittleEndian = e)
7172
static member ToBytes (x: float32, e, _: ToBytes) = BitConverter.GetBytes (x, BitConverter.IsLittleEndian = e)
7273
static member ToBytes (x: string , _, _: ToBytes) = Array.map byte (x.ToCharArray ())
74+
static member ToBytes (x: Guid , e, _: ToBytes) = BitConverter.GetBytes (x, BitConverter.IsLittleEndian = e)
7375
static member ToBytes (x: uint16 , e, _: ToBytes) = BitConverter.GetBytes (x, BitConverter.IsLittleEndian = e)
7476
static member ToBytes (x: uint32 , e, _: ToBytes) = BitConverter.GetBytes (x, BitConverter.IsLittleEndian = e)
7577
static member ToBytes (x: uint64 , e, _: ToBytes) = BitConverter.GetBytes (x, BitConverter.IsLittleEndian = e)

src/FSharpPlus/Internals.fs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ type BitConverter =
190190
let mutable value = value
191191
BitConverter.GetBytes ((&&value |> NativePtr.toNativeInt |> NativePtr.ofNativeInt |> NativePtr.read : int64), isLittleEndian)
192192

193+
/// Converts a Guid into an array of bytes with length
194+
/// eight.
195+
static member GetBytes (value: Guid, isLittleEndian) =
196+
let bytes = value.ToByteArray ()
197+
if isLittleEndian then bytes
198+
else
199+
let p1 = Array.rev bytes.[0 .. 3]
200+
let p2 = Array.rev bytes.[4 .. 5]
201+
let p3 = Array.rev bytes.[6 .. 7]
202+
let p4 = bytes.[8 .. 15]
203+
Array.concat [p1; p2; p3; p4]
204+
193205
/// Converts an array of bytes into a char.
194206
static member ToChar (value: byte [], startIndex: int, isLittleEndian: bool) =
195207
char <| BitConverter.ToInt16 (value, startIndex, isLittleEndian)
@@ -236,6 +248,19 @@ type BitConverter =
236248
let i2 = (int64 (NativePtr.get pbyte 4) <<< 24) ||| (int64 (NativePtr.get pbyte 5) <<< 16) ||| (int64 (NativePtr.get pbyte 6) <<< 8) ||| (int64 (NativePtr.get pbyte 7))
237249
i2 ||| (i1 <<< 32)
238250

251+
static member ToGuid (value: byte[], startIndex: int, isLittleEndian: bool) =
252+
if isNull value then nullArg "value"
253+
if startIndex >= value.Length then raise <| new ArgumentOutOfRangeException ("startIndex", "ArgumentOutOfRange_Index")
254+
if startIndex > value.Length - 16 then raise <| new ArgumentException "Arg_ArrayPlusOffTooSmall"
255+
if isLittleEndian then
256+
if startIndex = 0 then Guid value
257+
else Guid value.[startIndex .. startIndex + 15]
258+
else
259+
let p1 = Array.rev value.[startIndex + 0 .. startIndex + 3]
260+
let p2 = Array.rev value.[startIndex + 4 .. startIndex + 5]
261+
let p3 = Array.rev value.[startIndex + 6 .. startIndex + 7]
262+
let p4 = value.[startIndex + 8 .. startIndex + 15]
263+
Guid (Array.concat [p1; p2; p3; p4])
239264

240265
/// Converts an array of bytes into an ushort.
241266
///

0 commit comments

Comments
 (0)