on master help sign in
ByteString conversion and nil constant
13188f0Mark Lentczner
Data/UUID.hs+5−1
@@ -12,6 +12,7 @@
12-- This library is useful for comparing, parsing and
12-- This library is useful for comparing, parsing and
13-- printing Universally Unique Identifiers.
13-- printing Universally Unique Identifiers.
14-- See <http://en.wikipedia.org/wiki/UUID> for the general idea.
14-- See <http://en.wikipedia.org/wiki/UUID> for the general idea.
15+-- See <http://tools.ietf.org/html/rfc4122> for the specification.
15--
16--
16-- For generating UUIDs, check out 'Data.UUID.V1', 'Data.UUID.V5' and
17-- For generating UUIDs, check out 'Data.UUID.V1', 'Data.UUID.V5' and
17-- 'System.Random'.
18-- 'System.Random'.
@@ -19,7 +20,10 @@
19module Data.UUID(UUID
20module Data.UUID(UUID
20 ,toString
21 ,toString
21 ,fromString
22 ,fromString
23+ ,toByteString
24+ ,fromByteString
22 ,null
25 ,null
26+ ,nil
23 ) where
27 ) where
24
28
25import Prelude hiding (null)
29import Prelude hiding (null)
@@ -27,4 +31,4 @@ import Data.UUID.Internal
27
31
28-- Everything is really implemented in Data.UUID.Internal,
32-- Everything is really implemented in Data.UUID.Internal,
29-- but I don't want to export the constructors out of the
33-- but I don't want to export the constructors out of the
30−-- package.
34+-- package.
Data/UUID/Internal.hs+20−1
@@ -13,10 +13,13 @@
13module Data.UUID.Internal
13module Data.UUID.Internal
14 (UUID(..)
14 (UUID(..)
15 ,null
15 ,null
16+ ,nil
16 ,Node(..)
17 ,Node(..)
17 ,nullNode
18 ,nullNode
18 ,nodeToList
19 ,nodeToList
19 ,listToNode
20 ,listToNode
21+ ,fromByteString
22+ ,toByteString
20 ,fromString
23 ,fromString
21 ,toString
24 ,toString
22 ,versionMask
25 ,versionMask
@@ -47,6 +50,7 @@ import Foreign.Storable
47import Data.Binary
50import Data.Binary
48import Data.Binary.Put
51import Data.Binary.Put
49import Data.Binary.Get
52import Data.Binary.Get
53+import qualified Data.ByteString.Lazy as Lazy
50
54
51import System.Random
55import System.Random
52
56
@@ -71,11 +75,16 @@ data UUID = UUID
71 ,uuid_node :: SLOT(Node)
75 ,uuid_node :: SLOT(Node)
72 } deriving (Eq, Ord, Typeable)
76 } deriving (Eq, Ord, Typeable)
73
77
74−-- |Returns true if the passed-in UUID is the null UUID.
78+-- |Returns true if the passed-in UUID is the 'nil' UUID.
75null :: UUID -> Bool
79null :: UUID -> Bool
76null (UUID 0 0 0 0 0 node) | nullNode node = True
80null (UUID 0 0 0 0 0 node) | nullNode node = True
77null _ = False
81null _ = False
78
82
83+-- |The nil UUID, as defined in RFC 4122.
84+-- It is a UUID of all zeros. @'null' u@ iff @'u' == 'nil'@.
85+nil :: UUID
86+nil = UUID 0 0 0 0 0 (Node 0 0 0 0 0 0)
87+
79instance Random UUID where
88instance Random UUID where
80 random g = let (timeLow, g1) = randomBoundedIntegral g
89 random g = let (timeLow, g1) = randomBoundedIntegral g
81 (timeMid, g2) = randomBoundedIntegral g1
90 (timeMid, g2) = randomBoundedIntegral g1
@@ -220,6 +229,16 @@ instance Binary Node where
220 return $ Node w1 w2 w3 w4 w5 w6
229 return $ Node w1 w2 w3 w4 w5 w6
221
230
222
231
232+-- |Extract a UUID from a 'ByteString'.
233+-- The argument must be 16 bytes long, otherwise 'nil' is returned.
234+fromByteString :: Lazy.ByteString -> UUID
235+fromByteString bs | Lazy.length bs == 16 = decode bs
236+ | otherwise = nil
237+
238+-- |Encode a UUID into a 'ByteString' in network order.
239+toByteString :: UUID -> Lazy.ByteString
240+toByteString uu = encode uu
241+
223-- My goal with this instance was to make it work just enough to do what
242-- My goal with this instance was to make it work just enough to do what
224-- I want when used with the HStringTemplate library.
243-- I want when used with the HStringTemplate library.
225instance Data UUID where
244instance Data UUID where