@@ -2,9 +2,6 @@
2{-# LANGUAGE DeriveDataTypeable #-}
2{-# LANGUAGE DeriveDataTypeable #-}
3{-# LANGUAGE TypeFamilies #-}
3{-# LANGUAGE TypeFamilies #-}
4{-# LANGUAGE ViewPatterns #-}
4{-# LANGUAGE ViewPatterns #-}
5−{-# LANGUAGE Unsafe #-}
6−{-# LANGUAGE DeriveLift #-}
7−{-# LANGUAGE StandaloneDeriving #-}
8{-# OPTIONS_HADDOCK hide #-}
5{-# OPTIONS_HADDOCK hide #-}
@@ -46,15 +43,22 @@ module Data.UUID.Types.Internal
47import Prelude hiding (null)
44import Prelude hiding (null)
46+import Control.Applicative ((<*>))
49import Control.DeepSeq (NFData (..))
47import Control.DeepSeq (NFData (..))
50import Control.Monad (guard, liftM2)
48import Control.Monad (guard, liftM2)
51import Data.Bits
49import Data.Bits
52import Data.Char
50import Data.Char
53−import Data.Data
51+import Data.Functor ((<$>))
54import Data.Hashable
52import Data.Hashable
55import Data.List (elemIndices)
53import Data.List (elemIndices)
56import Foreign.Ptr (Ptr)
54import Foreign.Ptr (Ptr)
56+#if MIN_VERSION_base(4,0,0)
59+import Data.Generics.Basics
58import Foreign.Storable
62import Foreign.Storable
60import Data.Binary
64import Data.Binary
@@ -65,19 +69,19 @@ import qualified Data.ByteString.Internal as BI
65import qualified Data.ByteString.Lazy as BL
69import qualified Data.ByteString.Lazy as BL
66import qualified Data.ByteString.Unsafe as BU
70import qualified Data.ByteString.Unsafe as BU
67import Data.Text (Text)
71import Data.Text (Text)
68−import qualified Data.Text.Encoding as T
72+import qualified Data.Text as T
70import Data.UUID.Types.Internal.Builder
74import Data.UUID.Types.Internal.Builder
72−import System.Random (Random (..), uniform)
76+import System.Random
73−import System.Random.Stateful (Uniform (..), uniformWord64)
75−import Language.Haskell.TH.Syntax (Lift)
77−-- | Type representing <https://en.wikipedia.org/wiki/UUID Universally Unique Identifiers (UUID)> as specified in
79+-- |The UUID type. A 'Random' instance is provided which produces
80+-- insecure version 4 UUIDs as specified in <http://tools.ietf.org/html/rfc4122 RFC 4122>. The 'Storable' and
78−-- <http://tools.ietf.org/html/rfc4122 RFC 4122>.
81+-- 'Binary' instances are compatible with <http://tools.ietf.org/html/rfc4122 RFC 4122>, storing the fields in
82+-- network order as 16 bytes.
79data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
83data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
80− deriving (Eq, Ord)
84+ deriving (Eq, Ord, Typeable)
82 Prior to uuid-types-1.0.4:
86 Prior to uuid-types-1.0.4:
83 !Word32 !Word32 !Word32 !Word32
87 !Word32 !Word32 !Word32 !Word32
@@ -96,9 +100,6 @@ data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
96-- Useful for when you need to serialize a UUID and
100-- Useful for when you need to serialize a UUID and
97-- neither 'Storable' nor 'Binary' are appropriate.
101-- neither 'Storable' nor 'Binary' are appropriate.
99−-- >>> toWords <$> fromString "550e8400-e29b-41d4-a716-446655440000"
100−-- Just (1427014656,3801825748,2803254374,1430519808)
102-- See also 'toWords64'.
103-- See also 'toWords64'.
104-- /Since: @uuid-1.2.2@/
105-- /Since: @uuid-1.2.2@/
@@ -126,9 +127,6 @@ fromWords w1 w2 w3 w4 = UUID (w32to64 w1 w2) (w32to64 w3 w4)
127-- | Convert a 'UUID' into a pair of 'Word64's.
128-- | Convert a 'UUID' into a pair of 'Word64's.
129−-- >>> toWords64 <$> fromString "550e8400-e29b-41d4-a716-446655440000"
130−-- Just (6128981282234515924,12039885860129472512)
132-- See also 'toWords'.
130-- See also 'toWords'.
134-- @since 1.0.4
132-- @since 1.0.4
@@ -195,10 +193,10 @@ pack unpacked =
196-- |Build a Word32 from four Word8 values, presented in big-endian order
194-- |Build a Word32 from four Word8 values, presented in big-endian order
197word :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
195word :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
198−word a b c d = (fromIntegral a `unsafeShiftL` 24)
196+word a b c d = (fromIntegral a `shiftL` 24)
199− .|. (fromIntegral b `unsafeShiftL` 16)
197+ .|. (fromIntegral b `shiftL` 16)
200− .|. (fromIntegral c `unsafeShiftL` 8)
198+ .|. (fromIntegral c `shiftL` 8)
201− .|. (fromIntegral d )
199+ .|. (fromIntegral d )
203-- |Extract a Word8 from a Word64. Bytes, high to low, are numbered from 7 to 0,
201-- |Extract a Word8 from a Word64. Bytes, high to low, are numbered from 7 to 0,
204byte :: Int -> Word64 -> Word8
202byte :: Int -> Word64 -> Word8
@@ -207,7 +205,7 @@ byte i w = fromIntegral (w `shiftR` (i * 8))
207-- |Build a Word16 from two Word8 values, presented in big-endian order.
205-- |Build a Word16 from two Word8 values, presented in big-endian order.
208w8to16 :: Word8 -> Word8 -> Word16
206w8to16 :: Word8 -> Word8 -> Word16
209w8to16 w0s w1s =
207w8to16 w0s w1s =
210− (w0 `unsafeShiftL` 8) .|. w1
208+ (w0 `shiftL` 8) .|. w1
212 w0 = fromIntegral w0s
210 w0 = fromIntegral w0s
213 w1 = fromIntegral w1s
211 w1 = fromIntegral w1s
@@ -241,7 +239,7 @@ buildFromBytes v b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf =
241 where b6' = b6 .&. 0x0f .|. (v `shiftL` 4)
239 where b6' = b6 .&. 0x0f .|. (v `shiftL` 4)
242 b8' = b8 .&. 0x3f .|. 0x80
240 b8' = b8 .&. 0x3f .|. 0x80
244−-- |Build a UUID of a given version from 'Word32' values.
242+-- |Build a UUID of a given version from Word32 values.
245buildFromWords :: Word8 -> Word32 -> Word32 -> Word32 -> Word32 -> UUID
243buildFromWords :: Word8 -> Word32 -> Word32 -> Word32 -> Word32 -> UUID
246buildFromWords v w0 w1 w2 w3 = fromWords w0 w1' w2' w3
244buildFromWords v w0 w1 w2 w3 = fromWords w0 w1' w2' w3
247 where w1' = w1 .&. 0xffff0fff .|. ((fromIntegral v) `shiftL` 12)
245 where w1' = w1 .&. 0xffff0fff .|. ((fromIntegral v) `shiftL` 12)
@@ -275,8 +273,8 @@ null = (== nil)
275 -- null (UUID 0 0 0 0) = True
273 -- null (UUID 0 0 0 0) = True
276 -- null _ = False
274 -- null _ = False
278−-- |The 'nil' UUID, as defined in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
276+-- |The nil UUID, as defined in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
279−-- It is a UUID of all zeros. @'null' u@ /iff/ @'u' == 'nil'@.
277+-- It is a UUID of all zeros. @'null' u@ iff @'u' == 'nil'@.
280nil :: UUID
278nil :: UUID
281nil = UUID 0 0
279nil = UUID 0 0
@@ -286,8 +284,6 @@ fromByteString :: BL.ByteString -> Maybe UUID
286fromByteString = fromList . BL.unpack
284fromByteString = fromList . BL.unpack
288-- |Encode a UUID into a 'ByteString' in network order.
286-- |Encode a UUID into a 'ByteString' in network order.
290−-- This uses the same encoding as the 'Binary' instance.
291toByteString :: UUID -> BL.ByteString
287toByteString :: UUID -> BL.ByteString
292toByteString = BL.pack . toList
288toByteString = BL.pack . toList
@@ -295,8 +291,9 @@ toByteString = BL.pack . toList
295-- The hyphens may not be omitted.
291-- The hyphens may not be omitted.
296-- Example:
292-- Example:
298−-- >>> fromString "c2cc10e1-57d6-4b6f-9899-38d972112d8c"
295+-- fromString \"c2cc10e1-57d6-4b6f-9899-38d972112d8c\"
299−-- Just c2cc10e1-57d6-4b6f-9899-38d972112d8c
296+-- @
301-- Hex digits may be upper or lower-case.
298-- Hex digits may be upper or lower-case.
302fromString :: String -> Maybe UUID
299fromString :: String -> Maybe UUID
@@ -328,39 +325,32 @@ fromString' s0 = do
328-- | Convert a UUID into a hypenated string using lower-case letters.
325-- | Convert a UUID into a hypenated string using lower-case letters.
329-- Example:
326-- Example:
331−-- >>> toString <$> fromString "550e8400-e29b-41d4-a716-446655440000"
329+-- toString \<$\> fromString \"550e8400-e29b-41d4-a716-446655440000\"
332−-- Just "550e8400-e29b-41d4-a716-446655440000"
330+-- @
335toString :: UUID -> String
331toString :: UUID -> String
336−toString uuid = hexw0 w0 $ hexw1 w1 ""
332+toString uuid = hexw w0 $ hexw' w1 $ hexw' w2 $ hexw w3 ""
337− where hexw0 :: Word64 -> String -> String
333+ where hexw :: Word32 -> String -> String
338− hexw0 w s = hexn w 60 : hexn w 56 : hexn w 52 : hexn w 48
339− : hexn w 44 : hexn w 40 : hexn w 36 : hexn w 32
340− : '-' : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
334+ hexw w s = hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
341− : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0
335+ : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
344− hexw1 :: Word64 -> String -> String
337+ hexw' :: Word32 -> String -> String
345− hexw1 w s = '-' : hexn w 60 : hexn w 56 : hexn w 52 : hexn w 48
338+ hexw' w s = '-' : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
346− : '-' : hexn w 44 : hexn w 40 : hexn w 36 : hexn w 32
347− : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
348− : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0
339+ : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
351− hexn :: Word64 -> Int -> Char
341+ hexn :: Word32 -> Int -> Char
352 hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)
342 hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)
354− (w0,w1) = toWords64 uuid
344+ (w0,w1,w2,w3) = toWords uuid
356-- | If the passed in `Text` can be parsed as an ASCII representation of
346-- | If the passed in `Text` can be parsed as an ASCII representation of
357-- a `UUID`, it will be. The hyphens may not be omitted.
347-- a `UUID`, it will be. The hyphens may not be omitted.
358fromText :: Text -> Maybe UUID
348fromText :: Text -> Maybe UUID
359−fromText = fromASCIIBytes . T.encodeUtf8
349+fromText = fromString . T.unpack
361-- | Convert a UUID into a hyphentated string using lower-case letters.
351-- | Convert a UUID into a hyphentated string using lower-case letters.
362toText :: UUID -> Text
352toText :: UUID -> Text
363−toText = T.decodeLatin1 . toASCIIBytes
353+toText = T.pack . toString
365-- | Convert a UUID into a hyphentated string using lower-case letters, packed
355-- | Convert a UUID into a hyphentated string using lower-case letters, packed
366-- as ASCII bytes into `B.ByteString`.
356-- as ASCII bytes into `B.ByteString`.
@@ -455,30 +445,49 @@ fromASCIIBytes bs = do
455-- | Similar to `toASCIIBytes` except we produce a lazy `BL.ByteString`.
445-- | Similar to `toASCIIBytes` except we produce a lazy `BL.ByteString`.
456toLazyASCIIBytes :: UUID -> BL.ByteString
446toLazyASCIIBytes :: UUID -> BL.ByteString
457toLazyASCIIBytes =
447toLazyASCIIBytes =
448+#if MIN_VERSION_bytestring(0,10,0)
458 BL.fromStrict
449 BL.fromStrict
451+ BL.fromChunks . return
459 . toASCIIBytes
453 . toASCIIBytes
461-- | Similar to `fromASCIIBytes` except parses from a lazy `BL.ByteString`.
455-- | Similar to `fromASCIIBytes` except parses from a lazy `BL.ByteString`.
462fromLazyASCIIBytes :: BL.ByteString -> Maybe UUID
456fromLazyASCIIBytes :: BL.ByteString -> Maybe UUID
463fromLazyASCIIBytes bs =
457fromLazyASCIIBytes bs =
464− if BL.length bs == 36 then fromASCIIBytes (BL.toStrict bs) else Nothing
458+ if BL.length bs == 36 then fromASCIIBytes (
459+#if MIN_VERSION_bytestring(0,10,0)
462+ B.concat $ BL.toChunks bs
467-- Class Instances
467-- Class Instances
470−-- | This 'Random' instance produces __insecure__ version 4 UUIDs as
471−-- specified in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
472instance Random UUID where
470instance Random UUID where
473− random = uniform
471+ random g = (fromGenNext w0 w1 w2 w3 w4, g4)
472+ where (w0, g0) = next g
474 randomR _ = random -- range is ignored
477 randomR _ = random -- range is ignored
476−-- @since 1.0.4
479+-- |Build a UUID from the results of five calls to next on a StdGen.
477−instance Uniform UUID where
480+-- While next on StdGet returns an Int, it doesn't provide 32 bits of
478− uniformM gen = do
481+-- randomness. This code relies on at last 28 bits of randomness in the
479− w0 <- uniformWord64 gen
482+-- and optimizes its use so as to make only five random values, not six.
480− w1 <- uniformWord64 gen
483+fromGenNext :: Int -> Int -> Int -> Int -> Int -> UUID
484+fromGenNext w0 w1 w2 w3 w4 =
481− pure $ buildFromBytes 4 /-/ w0 /-/ w1
485+ buildFromBytes 4 /-/ (ThreeByte w0)
487+ /-/ w2 -- use all 4 bytes because we know the version
488+ -- field will "cover" the upper, non-random bits
483-- |A ByteSource to extract only three bytes from an Int, since next on StdGet
492-- |A ByteSource to extract only three bytes from an Int, since next on StdGet
484-- only returns 31 bits of randomness.
493-- only returns 31 bits of randomness.
@@ -486,8 +495,8 @@ type instance ByteSink ThreeByte g = Takes3Bytes g
486newtype ThreeByte = ThreeByte Int
495newtype ThreeByte = ThreeByte Int
487instance ByteSource ThreeByte where
496instance ByteSource ThreeByte where
488 f /-/ (ThreeByte w) = f b1 b2 b3
497 f /-/ (ThreeByte w) = f b1 b2 b3
489− where b1 = fromIntegral (w `unsafeShiftR` 16)
498+ where b1 = fromIntegral (w `shiftR` 16)
490− b2 = fromIntegral (w `unsafeShiftR` 8)
499+ b2 = fromIntegral (w `shiftR` 8)
491 b3 = fromIntegral w
500 b3 = fromIntegral w
493instance NFData UUID where
502instance NFData UUID where
@@ -504,11 +513,6 @@ instance Hashable UUID where
504 `hashWithSalt` w2
513 `hashWithSalt` w2
505 `hashWithSalt` w3
514 `hashWithSalt` w3
507−-- | Pretty prints a 'UUID' (without quotation marks). See also 'toString'.
510−-- "00000000-0000-0000-0000-000000000000"
512instance Show UUID where
516instance Show UUID where
513 show = toString
517 show = toString
@@ -519,7 +523,7 @@ instance Read UUID where
519 Nothing -> []
523 Nothing -> []
520 Just u -> [(u,drop 36 noSpaces)]
524 Just u -> [(u,drop 36 noSpaces)]
522−-- | This 'Storable' instance uses the memory layout as described in <http://tools.ietf.org/html/rfc4122 RFC 4122>, but in contrast to the 'Binary' instance, __the fields are stored in host byte order__.
526+
523instance Storable UUID where
527instance Storable UUID where
524 sizeOf _ = 16
528 sizeOf _ = 16
525 alignment _ = 4
529 alignment _ = 4
@@ -556,7 +560,6 @@ instance Storable UUID where
556 pokeByteOff p (off+14) x9
560 pokeByteOff p (off+14) x9
557 pokeByteOff p (off+15) x10
561 pokeByteOff p (off+15) x10
559−-- | This 'Binary' instance is compatible with <http://tools.ietf.org/html/rfc4122 RFC 4122>, storing the fields in network order as 16 bytes.
560instance Binary UUID where
563instance Binary UUID where
561 put (UUID w0 w1) = putWord64be w0 >> putWord64be w1
564 put (UUID w0 w1) = putWord64be w0 >> putWord64be w1
562 get = liftM2 UUID getWord64be getWord64be
565 get = liftM2 UUID getWord64be getWord64be
@@ -572,4 +575,13 @@ instance Data UUID where
572uuidType :: DataType
575uuidType :: DataType
573uuidType = mkNoRepType "Data.UUID.Types.UUID"
576uuidType = mkNoRepType "Data.UUID.Types.UUID"
575−deriving instance Lift UUID
578+#if !(MIN_VERSION_base(4,2,0))
579+mkNoRepType :: String -> DataType
580+mkNoRepType = mkNorepType
583+#if !MIN_VERSION_base(4,5,0)
584+unsafeShiftR, unsafeShiftL :: Word64 -> Int -> Word64
585+unsafeShiftR = shiftR
586+unsafeShiftL = shiftL