@@ -2,9 +2,6 @@
22{-# LANGUAGE DeriveDataTypeable #-}
33{-# LANGUAGE TypeFamilies #-}
44{-# LANGUAGE ViewPatterns #-}
5−{-# LANGUAGE Unsafe #-}
6−{-# LANGUAGE DeriveLift #-}
7−{-# LANGUAGE StandaloneDeriving #-}
85{-# OPTIONS_HADDOCK hide #-}
96
107-- |
@@ -46,11 +43,13 @@ module Data.UUID.Types.Internal
4643
4744import Prelude hiding (null)
4845
46+import Control.Applicative ((<*>))
4947import Control.DeepSeq (NFData (..))
5048import Control.Monad (guard, liftM2)
5149import Data.Bits
5250import Data.Char
5351import Data.Data
52+import Data.Functor ((<$>))
5453import Data.Hashable
5554import Data.List (elemIndices)
5655import Foreign.Ptr (Ptr)
@@ -65,19 +64,19 @@ import qualified Data.ByteString.Internal as BI
6564import qualified Data.ByteString.Lazy as BL
6665import qualified Data.ByteString.Unsafe as BU
6766import Data.Text (Text)
68−import qualified Data.Text.Encoding as T
67+import qualified Data.Text as T
6968
7069import Data.UUID.Types.Internal.Builder
7170
72−import System.Random (Random (..), uniform)
73−import System.Random.Stateful (Uniform (..), uniformWord64)
71+import System.Random
7472
75−import Language.Haskell.TH.Syntax (Lift)
7673
77−-- | Type representing <https://en.wikipedia.org/wiki/UUID Universally Unique Identifiers (UUID)> as specified in
78−-- <http://tools.ietf.org/html/rfc4122 RFC 4122>.
74+-- |The UUID type. A 'Random' instance is provided which produces
75+-- insecure version 4 UUIDs as specified in <http://tools.ietf.org/html/rfc4122 RFC 4122>. The 'Storable' and
76+-- 'Binary' instances are compatible with <http://tools.ietf.org/html/rfc4122 RFC 4122>, storing the fields in
77+-- network order as 16 bytes.
7978data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
80− deriving (Eq, Ord)
79+ deriving (Eq, Ord, Typeable)
8180{-
8281 Prior to uuid-types-1.0.4:
8382 !Word32 !Word32 !Word32 !Word32
@@ -96,9 +95,6 @@ data UUID = UUID {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64
9695-- Useful for when you need to serialize a UUID and
9796-- neither 'Storable' nor 'Binary' are appropriate.
9897--
99−-- >>> toWords <$> fromString "550e8400-e29b-41d4-a716-446655440000"
100−-- Just (1427014656,3801825748,2803254374,1430519808)
101−--
10298-- See also 'toWords64'.
10399--
104100-- /Since: @uuid-1.2.2@/
@@ -126,9 +122,6 @@ fromWords w1 w2 w3 w4 = UUID (w32to64 w1 w2) (w32to64 w3 w4)
126122
127123-- | Convert a 'UUID' into a pair of 'Word64's.
128124--
129−-- >>> toWords64 <$> fromString "550e8400-e29b-41d4-a716-446655440000"
130−-- Just (6128981282234515924,12039885860129472512)
131−--
132125-- See also 'toWords'.
133126--
134127-- @since 1.0.4
@@ -195,10 +188,10 @@ pack unpacked =
195188
196189-- |Build a Word32 from four Word8 values, presented in big-endian order
197190word :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
198−word a b c d = (fromIntegral a `unsafeShiftL` 24)
199− .|. (fromIntegral b `unsafeShiftL` 16)
200− .|. (fromIntegral c `unsafeShiftL` 8)
201− .|. (fromIntegral d )
191+word a b c d = (fromIntegral a `shiftL` 24)
192+ .|. (fromIntegral b `shiftL` 16)
193+ .|. (fromIntegral c `shiftL` 8)
194+ .|. (fromIntegral d )
202195
203196-- |Extract a Word8 from a Word64. Bytes, high to low, are numbered from 7 to 0,
204197byte :: Int -> Word64 -> Word8
@@ -207,7 +200,7 @@ byte i w = fromIntegral (w `shiftR` (i * 8))
207200-- |Build a Word16 from two Word8 values, presented in big-endian order.
208201w8to16 :: Word8 -> Word8 -> Word16
209202w8to16 w0s w1s =
210− (w0 `unsafeShiftL` 8) .|. w1
203+ (w0 `shiftL` 8) .|. w1
211204 where
212205 w0 = fromIntegral w0s
213206 w1 = fromIntegral w1s
@@ -241,7 +234,7 @@ buildFromBytes v b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf =
241234 where b6' = b6 .&. 0x0f .|. (v `shiftL` 4)
242235 b8' = b8 .&. 0x3f .|. 0x80
243236
244−-- |Build a UUID of a given version from 'Word32' values.
237+-- |Build a UUID of a given version from Word32 values.
245238buildFromWords :: Word8 -> Word32 -> Word32 -> Word32 -> Word32 -> UUID
246239buildFromWords v w0 w1 w2 w3 = fromWords w0 w1' w2' w3
247240 where w1' = w1 .&. 0xffff0fff .|. ((fromIntegral v) `shiftL` 12)
@@ -275,8 +268,8 @@ null = (== nil)
275268 -- null (UUID 0 0 0 0) = True
276269 -- null _ = False
277270
278−-- |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'@.
271+-- |The nil UUID, as defined in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
272+-- It is a UUID of all zeros. @'null' u@ iff @'u' == 'nil'@.
280273nil :: UUID
281274nil = UUID 0 0
282275
@@ -286,8 +279,6 @@ fromByteString :: BL.ByteString -> Maybe UUID
286279fromByteString = fromList . BL.unpack
287280
288281-- |Encode a UUID into a 'ByteString' in network order.
289−--
290−-- This uses the same encoding as the 'Binary' instance.
291282toByteString :: UUID -> BL.ByteString
292283toByteString = BL.pack . toList
293284
@@ -295,8 +286,9 @@ toByteString = BL.pack . toList
295286-- The hyphens may not be omitted.
296287-- Example:
297288--
298−-- >>> fromString "c2cc10e1-57d6-4b6f-9899-38d972112d8c"
299−-- Just c2cc10e1-57d6-4b6f-9899-38d972112d8c
289+-- @
290+-- fromString \"c2cc10e1-57d6-4b6f-9899-38d972112d8c\"
291+-- @
300292--
301293-- Hex digits may be upper or lower-case.
302294fromString :: String -> Maybe UUID
@@ -328,39 +320,32 @@ fromString' s0 = do
328320-- | Convert a UUID into a hypenated string using lower-case letters.
329321-- Example:
330322--
331−-- >>> toString <$> fromString "550e8400-e29b-41d4-a716-446655440000"
332−-- Just "550e8400-e29b-41d4-a716-446655440000"
333−--
334−--
323+-- @
324+-- toString \<$\> fromString \"550e8400-e29b-41d4-a716-446655440000\"
325+-- @
335326toString :: UUID -> String
336−toString uuid = hexw0 w0 $ hexw1 w1 ""
337− where hexw0 :: Word64 -> 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
341− : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0
342− : s
343−
344− hexw1 :: Word64 -> String -> String
345− hexw1 w s = '-' : hexn w 60 : hexn w 56 : hexn w 52 : hexn w 48
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
349− : s
350−
351− hexn :: Word64 -> Int -> Char
327+toString uuid = hexw w0 $ hexw' w1 $ hexw' w2 $ hexw w3 ""
328+ where hexw :: Word32 -> String -> String
329+ hexw w s = hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
330+ : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
331+
332+ hexw' :: Word32 -> String -> String
333+ hexw' w s = '-' : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
334+ : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
335+
336+ hexn :: Word32 -> Int -> Char
352337 hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)
353338
354− (w0,w1) = toWords64 uuid
339+ (w0,w1,w2,w3) = toWords uuid
355340
356341-- | If the passed in `Text` can be parsed as an ASCII representation of
357342-- a `UUID`, it will be. The hyphens may not be omitted.
358343fromText :: Text -> Maybe UUID
359−fromText = fromASCIIBytes . T.encodeUtf8
344+fromText = fromString . T.unpack
360345
361346-- | Convert a UUID into a hyphentated string using lower-case letters.
362347toText :: UUID -> Text
363−toText = T.decodeLatin1 . toASCIIBytes
348+toText = T.pack . toString
364349
365350-- | Convert a UUID into a hyphentated string using lower-case letters, packed
366351-- as ASCII bytes into `B.ByteString`.
@@ -455,30 +440,49 @@ fromASCIIBytes bs = do
455440-- | Similar to `toASCIIBytes` except we produce a lazy `BL.ByteString`.
456441toLazyASCIIBytes :: UUID -> BL.ByteString
457442toLazyASCIIBytes =
443+#if MIN_VERSION_bytestring(0,10,0)
458444 BL.fromStrict
445+#else
446+ BL.fromChunks . return
447+#endif
459448 . toASCIIBytes
460449
461450-- | Similar to `fromASCIIBytes` except parses from a lazy `BL.ByteString`.
462451fromLazyASCIIBytes :: BL.ByteString -> Maybe UUID
463452fromLazyASCIIBytes bs =
464− if BL.length bs == 36 then fromASCIIBytes (BL.toStrict bs) else Nothing
453+ if BL.length bs == 36 then fromASCIIBytes (
454+#if MIN_VERSION_bytestring(0,10,0)
455+ BL.toStrict bs
456+#else
457+ B.concat $ BL.toChunks bs
458+#endif
459+ ) else Nothing
465460
466461--
467462-- Class Instances
468463--
469464
470−-- | This 'Random' instance produces __insecure__ version 4 UUIDs as
471−-- specified in <http://tools.ietf.org/html/rfc4122 RFC 4122>.
472465instance Random UUID where
473− random = uniform
466+ random g = (fromGenNext w0 w1 w2 w3 w4, g4)
467+ where (w0, g0) = next g
468+ (w1, g1) = next g0
469+ (w2, g2) = next g1
470+ (w3, g3) = next g2
471+ (w4, g4) = next g3
474472 randomR _ = random -- range is ignored
475473
476−-- @since 1.0.4
477−instance Uniform UUID where
478− uniformM gen = do
479− w0 <- uniformWord64 gen
480− w1 <- uniformWord64 gen
481− pure $ buildFromBytes 4 /-/ w0 /-/ w1
474+-- |Build a UUID from the results of five calls to next on a StdGen.
475+-- While next on StdGet returns an Int, it doesn't provide 32 bits of
476+-- randomness. This code relies on at last 28 bits of randomness in the
477+-- and optimizes its use so as to make only five random values, not six.
478+fromGenNext :: Int -> Int -> Int -> Int -> Int -> UUID
479+fromGenNext w0 w1 w2 w3 w4 =
480+ buildFromBytes 4 /-/ (ThreeByte w0)
481+ /-/ (ThreeByte w1)
482+ /-/ w2 -- use all 4 bytes because we know the version
483+ -- field will "cover" the upper, non-random bits
484+ /-/ (ThreeByte w3)
485+ /-/ (ThreeByte w4)
482486
483487-- |A ByteSource to extract only three bytes from an Int, since next on StdGet
484488-- only returns 31 bits of randomness.
@@ -486,8 +490,8 @@ type instance ByteSink ThreeByte g = Takes3Bytes g
486490newtype ThreeByte = ThreeByte Int
487491instance ByteSource ThreeByte where
488492 f /-/ (ThreeByte w) = f b1 b2 b3
489− where b1 = fromIntegral (w `unsafeShiftR` 16)
490− b2 = fromIntegral (w `unsafeShiftR` 8)
493+ where b1 = fromIntegral (w `shiftR` 16)
494+ b2 = fromIntegral (w `shiftR` 8)
491495 b3 = fromIntegral w
492496
493497instance NFData UUID where
@@ -504,11 +508,6 @@ instance Hashable UUID where
504508 `hashWithSalt` w2
505509 `hashWithSalt` w3
506510
507−-- | Pretty prints a 'UUID' (without quotation marks). See also 'toString'.
508−--
509−-- >>> show nil
510−-- "00000000-0000-0000-0000-000000000000"
511−--
512511instance Show UUID where
513512 show = toString
514513
@@ -519,7 +518,7 @@ instance Read UUID where
519518 Nothing -> []
520519 Just u -> [(u,drop 36 noSpaces)]
521520
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__.
521+
523522instance Storable UUID where
524523 sizeOf _ = 16
525524 alignment _ = 4
@@ -556,7 +555,6 @@ instance Storable UUID where
556555 pokeByteOff p (off+14) x9
557556 pokeByteOff p (off+15) x10
558557
559−-- | This 'Binary' instance is compatible with <http://tools.ietf.org/html/rfc4122 RFC 4122>, storing the fields in network order as 16 bytes.
560558instance Binary UUID where
561559 put (UUID w0 w1) = putWord64be w0 >> putWord64be w1
562560 get = liftM2 UUID getWord64be getWord64be
@@ -572,4 +570,8 @@ instance Data UUID where
572570uuidType :: DataType
573571uuidType = mkNoRepType "Data.UUID.Types.UUID"
574572
575−deriving instance Lift UUID
573+#if !MIN_VERSION_base(4,5,0)
574+unsafeShiftR, unsafeShiftL :: Word64 -> Int -> Word64
575+unsafeShiftR = shiftR
576+unsafeShiftL = shiftL
577+#endif