1{-# LANGUAGE DeriveDataTypeable, TypeFamilies, CPP #-}
5-- Copyright : (c) 2008-2009 Antoine Latter
6-- (c) 2009 Mark Lentczner
10-- Maintainer : aslatter@gmail.com
11-- Stability : experimental
12-- Portability : portable
14module Data.UUID.Internal
28import Prelude hiding (null)
30import Control.Monad (liftM4)
34import Data.List (elemIndices)
36#if MIN_VERSION_base(4,0,0)
39import Data.Generics.Basics
42import Foreign.Storable
47import qualified Data.ByteString.Lazy as Lazy
49import Data.UUID.Builder
54-- |The UUID type. A 'Random' instance is provided which produces
55-- version 4 UUIDs as specified in RFC 4122. The 'Storable' and
56-- 'Binary' instances are compatible with RFC 4122, storing the fields
57-- in network order as 16 bytes.
58data UUID = UUID !Word32 !Word32 !Word32 !Word32
59 deriving (Eq, Ord, Typeable)
61 Other representations that we tried are:
62 Mimic V1 structure: !Word32 !Word16 !Word16 !Word16
63 !Word8 !Word8 !Word8 !Word8 !Word8 !Word8
64 Sixteen bytes: !Word8 ... (x 16)
65 Simple list of bytes: [Word8]
66 ByteString (strict) ByteString
67 Immutable array: UArray Int Word8
69 None was as fast, overall, as the representation used here.
72-- | Covert a 'UUID' into a sequence of 'Word32' values.
73-- Usefull for when you need to serialize a UUID and
74-- neither 'Storable' nor 'Binary' are appropriate.
75toWords :: UUID -> (Word32, Word32, Word32, Word32)
76toWords (UUID w1 w2 w3 w4) = (w1, w2, w3, w4)
78-- | Create a 'UUID' from a sequence of 'Word32'. The
79-- opposite of 'toWords'.
80fromWords :: Word32 -> Word32 -> Word32 -> Word32 -> UUID
87-- |Build a Word32 from four Word8 values, presented in big-endian order
88word :: Word8 -> Word8 -> Word8 -> Word8 -> Word32
89word a b c d = (fromIntegral a `shiftL` 24)
90 .|. (fromIntegral b `shiftL` 16)
91 .|. (fromIntegral c `shiftL` 8)
94-- |Extract a Word8 from a Word32. Bytes, high to low, are numbered from 3 to 0,
95byte :: Int -> Word32 -> Word8
96byte i w = fromIntegral (w `shiftR` (i * 8))
99-- |Make a UUID from sixteen Word8 values
100makeFromBytes :: Word8 -> Word8 -> Word8 -> Word8
101 -> Word8 -> Word8 -> Word8 -> Word8
102 -> Word8 -> Word8 -> Word8 -> Word8
103 -> Word8 -> Word8 -> Word8 -> Word8
105makeFromBytes b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
107 where w0 = word b0 b1 b2 b3
108 w1 = word b4 b5 b6 b7
109 w2 = word b8 b9 ba bb
110 w3 = word bc bd be bf
112-- |Make a UUID from four Word32 values
113makeFromWords :: Word32 -> Word32 -> Word32 -> Word32 -> UUID
116-- |A Builder for constructing a UUID of a given version.
117buildFromBytes :: Word8
118 -> Word8 -> Word8 -> Word8 -> Word8
119 -> Word8 -> Word8 -> Word8 -> Word8
120 -> Word8 -> Word8 -> Word8 -> Word8
121 -> Word8 -> Word8 -> Word8 -> Word8
123buildFromBytes v b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf =
124 makeFromBytes b0 b1 b2 b3 b4 b5 b6' b7 b8' b9 ba bb bc bd be bf
125 where b6' = b6 .&. 0x0f .|. (v `shiftL` 4)
126 b8' = b8 .&. 0x3f .|. 0x80
128-- |Build a UUID of a given version from Word32 values.
129buildFromWords :: Word8 -> Word32 -> Word32 -> Word32 -> Word32 -> UUID
130buildFromWords v w0 w1 w2 w3 = makeFromWords w0 w1' w2' w3
131 where w1' = w1 .&. 0xffff0fff .|. ((fromIntegral v) `shiftL` 12)
132 w2' = w2 .&. 0x3fffffff .|. 0x80000000
135-- |Return the bytes that make up the UUID
136toList :: UUID -> [Word8]
137toList (UUID w0 w1 w2 w3) =
138 [byte 3 w0, byte 2 w0, byte 1 w0, byte 0 w0,
139 byte 3 w1, byte 2 w1, byte 1 w1, byte 0 w1,
140 byte 3 w2, byte 2 w2, byte 1 w2, byte 0 w2,
141 byte 3 w3, byte 2 w3, byte 1 w3, byte 0 w3]
143-- |Construct a UUID from a list of Word8. Returns Nothing if the list isn't
144-- exactly sixteen bytes long
145fromList :: [Word8] -> Maybe UUID
146fromList [b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bb, bc, bd, be, bf] =
147 Just $ makeFromBytes b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
155-- |Returns true if the passed-in UUID is the 'nil' UUID.
158 -- Note: This actually faster than:
159 -- null (UUID 0 0 0 0) = True
162-- |The nil UUID, as defined in RFC 4122.
163-- It is a UUID of all zeros. @'null' u@ iff @'u' == 'nil'@.
167-- |Extract a UUID from a 'ByteString' in network byte order.
168-- The argument must be 16 bytes long, otherwise 'Nothing' is returned.
169fromByteString :: Lazy.ByteString -> Maybe UUID
170fromByteString = fromList . Lazy.unpack
172-- |Encode a UUID into a 'ByteString' in network order.
173toByteString :: UUID -> Lazy.ByteString
174toByteString = Lazy.pack . toList
176-- |If the passed in 'String' can be parsed as a 'UUID', it will be.
177-- The hyphens may not be omitted.
181-- fromString \"c2cc10e1-57d6-4b6f-9899-38d972112d8c\"
184-- Hex digits may be upper or lower-case.
185fromString :: String -> Maybe UUID
186fromString xs | validFmt = fromString' xs
187 | otherwise = Nothing
188 where validFmt = elemIndices '-' xs == [8,13,18,23]
190fromString' :: String -> Maybe UUID
192 (w0, s1) <- hexWord s0
193 (w1, s2) <- hexWord s1
194 (w2, s3) <- hexWord s2
195 (w3, s4) <- hexWord s3
196 if s4 /= "" then Nothing
197 else Just $ UUID w0 w1 w2 w3
198 where hexWord :: String -> Maybe (Word32, String)
199 hexWord s = Just (0, s) >>= hexByte >>= hexByte
200 >>= hexByte >>= hexByte
202 hexByte :: (Word32, String) -> Maybe (Word32, String)
203 hexByte (w, '-':ds) = hexByte (w, ds)
204 hexByte (w, hi:lo:ds)
205 | bothHex = Just ((w `shiftL` 8) .|. octet, ds)
206 | otherwise = Nothing
207 where bothHex = isHexDigit hi && isHexDigit lo
208 octet = fromIntegral (16 * digitToInt hi + digitToInt lo)
211-- | Convert a UUID into a hypenated string using lower-case letters.
215-- toString $ fromString \"550e8400-e29b-41d4-a716-446655440000\"
217toString :: UUID -> String
218toString (UUID w0 w1 w2 w3) = hexw w0 $ hexw' w1 $ hexw' w2 $ hexw w3 ""
219 where hexw :: Word32 -> String -> String
220 hexw w s = hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
221 : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
223 hexw' :: Word32 -> String -> String
224 hexw' w s = '-' : hexn w 28 : hexn w 24 : hexn w 20 : hexn w 16
225 : '-' : hexn w 12 : hexn w 8 : hexn w 4 : hexn w 0 : s
227 hexn :: Word32 -> Int -> Char
228 hexn w r = intToDigit $ fromIntegral ((w `shiftR` r) .&. 0xf)
235instance Random UUID where
236 random g = (fromGenNext w0 w1 w2 w3 w4, g4)
237 where (w0, g0) = next g
242 randomR _ = random -- range is ignored
244-- |Build a UUID from the results of five calls to next on a StdGen.
245-- While next on StdGet returns an Int, it doesn't provide 32 bits of
246-- randomness. This code relies on at last 28 bits of randomness in the
247-- and optimizes its use so as to make only five random values, not six.
248fromGenNext :: Int -> Int -> Int -> Int -> Int -> UUID
249fromGenNext w0 w1 w2 w3 w4 =
250 buildFromBytes 4 /-/ (ThreeByte w0)
252 /-/ w2 -- use all 4 bytes because we know the version
253 -- field will "cover" the upper, non-random bits
257-- |A ByteSource to extract only three bytes from an Int, since next on StdGet
258-- only returns 31 bits of randomness.
259type instance ByteSink ThreeByte g = Takes3Bytes g
260newtype ThreeByte = ThreeByte Int
261instance ByteSource ThreeByte where
262 f /-/ (ThreeByte w) = f b1 b2 b3
263 where b1 = fromIntegral (w `shiftR` 16)
264 b2 = fromIntegral (w `shiftR` 8)
268instance Show UUID where
271instance Read UUID where
273 let noSpaces = dropWhile isSpace str
274 in case fromString (take 36 noSpaces) of
276 Just u -> [(u,drop 36 noSpaces)]
279instance Storable UUID where
281 alignment _ = 1 -- UUIDs are stored as individual octets
284 mapM (peekByteOff p) [off..(off+15)] >>= return . fromJust . fromList
286 pokeByteOff p off u =
287 sequence_ $ zipWith (pokeByteOff p) [off..] (toList u)
290instance Binary UUID where
291 put (UUID w0 w1 w2 w3) =
292 putWord32be w0 >> putWord32be w1 >> putWord32be w2 >> putWord32be w3
293 get = liftM4 UUID getWord32be getWord32be getWord32be getWord32be
296-- My goal with this instance was to make it work just enough to do what
297-- I want when used with the HStringTemplate library.
298instance Data UUID where
299 toConstr uu = mkConstr uuidType (show uu) [] (error "fixity")
300 gunfold _ _ = error "gunfold"
301 dataTypeOf _ = uuidType
304uuidType = mkNoRepType "Data.UUID.UUID"
306#if !(MIN_VERSION_base(4,2,0))
307mkNoRepType :: String -> DataType
308mkNoRepType = mkNorepType
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list