module Z.Data.Builder.UUID
( uuid, uuidUpper
, encodeUUID
) where
import Data.UUID.Types.Internal
import Data.Word
import Data.Bits
import Z.Data.ASCII
import qualified Z.Data.Builder.Base as B
import qualified Z.Data.Builder.Numeric as B
uuid :: UUID -> B.Builder ()
{-# INLINABLE uuid #-}
uuid :: UUID -> Builder ()
uuid (UUID Word64
wh Word64
wl) = do
let !w1 :: Word32
w1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word32 forall a b. (a -> b) -> a -> b
$ Word64
wh forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32
!w2 :: Word16
w2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wh forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
16 forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
!w3 :: Word16
w3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wh forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
!w4 :: Word16
w4 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wl forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
48
!w5 :: Word16
w5 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wl forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32 forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
!w6 :: Word32
w6 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word32 forall a b. (a -> b) -> a -> b
$ Word64
wl forall a. Bits a => a -> a -> a
.&. Word64
0xFFFFFFFF
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hex Word32
w1
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hex Word16
w2
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hex Word16
w3
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hex Word16
w4
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hex Word16
w5
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hex Word32
w6
uuidUpper :: UUID -> B.Builder ()
{-# INLINABLE uuidUpper #-}
uuidUpper :: UUID -> Builder ()
uuidUpper (UUID Word64
wh Word64
wl) = do
let !w1 :: Word32
w1 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word32 forall a b. (a -> b) -> a -> b
$ Word64
wh forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32
!w2 :: Word16
w2 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wh forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
16 forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
!w3 :: Word16
w3 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wh forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
!w4 :: Word16
w4 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wl forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
48
!w5 :: Word16
w5 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word16 forall a b. (a -> b) -> a -> b
$ Word64
wl forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
32 forall a. Bits a => a -> a -> a
.&. Word64
0xFFFF
!w6 :: Word32
w6 = forall a b. (Integral a, Num b) => a -> b
fromIntegral @Word64 @Word32 forall a b. (a -> b) -> a -> b
$ Word64
wl forall a. Bits a => a -> a -> a
.&. Word64
0xFFFFFFFF
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hexUpper Word32
w1
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hexUpper Word16
w2
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hexUpper Word16
w3
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hexUpper Word16
w4
Word8 -> Builder ()
B.word8 Word8
HYPHEN
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hexUpper Word16
w5
forall a. (FiniteBits a, Integral a) => a -> Builder ()
B.hexUpper Word32
w6
encodeUUID :: UUID -> B.Builder ()
{-# INLINABLE encodeUUID #-}
encodeUUID :: UUID -> Builder ()
encodeUUID (UUID Word64
wh Word64
wl) = do
Word64 -> Builder ()
B.encodeWord64BE Word64
wh
Word64 -> Builder ()
B.encodeWord64BE Word64
wl