on master help sign in
tidying things up a bit
commita5b3ab1cf13956eea344bb720594650b6f2a1167 authoraslatter <aslatter@gmail.com> dateWed 24 Sep 2008, 04:04 (18 years ago) parent80c83cb
2 files changed+21−84
Data/UUID.hs+21−21 Data/UUID/Internal.hs−63
Data/UUID.hs+21−21
@@ -20,7 +20,7 @@ module Data.UUID(UUID
2020 ,toString
2121 ,toStringUpper
2222 ,toStringLower
23− ,generate
23+ ,generate
2424 ,generateRandom
2525 ,generateTime
2626 ,null
@@ -124,20 +124,20 @@ instance Storable UUID where
124124
125125 peek p = do
126126 tl <- peek $ castPtr p
127− tm <- peekByteOff (castPtr p) 4
128− th <- peekByteOff (castPtr p) 6
129− ch <- peekByteOff (castPtr p) 8
130− cl <- peekByteOff (castPtr p) 9
131− node <- peekByteOff (castPtr p) 10
127+ tm <- peekByteOff p 4
128+ th <- peekByteOff p 6
129+ ch <- peekByteOff p 8
130+ cl <- peekByteOff p 9
131+ node <- peekByteOff p 10
132132 return $ UUID tl tm th ch cl node
133133
134134 poke p (UUID tl tm th ch cl node) = do
135135 poke (castPtr p) tl
136− pokeByteOff (castPtr p) 4 tm
137− pokeByteOff (castPtr p) 6 th
138− pokeByteOff (castPtr p) 8 ch
139− pokeByteOff (castPtr p) 9 cl
140− pokeByteOff (castPtr p) 10 node
136+ pokeByteOff p 4 tm
137+ pokeByteOff p 6 th
138+ pokeByteOff p 8 ch
139+ pokeByteOff p 9 cl
140+ pokeByteOff p 10 node
141141
142142instance Storable Node where
143143 sizeOf _ = 6
@@ -145,20 +145,20 @@ instance Storable Node where
145145
146146 peek p = do
147147 w1 <- peek $ castPtr p
148− w2 <- peekByteOff (castPtr p) 1
149− w3 <- peekByteOff (castPtr p) 2
150− w4 <- peekByteOff (castPtr p) 3
151− w5 <- peekByteOff (castPtr p) 4
152− w6 <- peekByteOff (castPtr p) 5
148+ w2 <- peekByteOff p 1
149+ w3 <- peekByteOff p 2
150+ w4 <- peekByteOff p 3
151+ w5 <- peekByteOff p 4
152+ w6 <- peekByteOff p 5
153153 return $ Node w1 w2 w3 w4 w5 w6
154154
155155 poke p (Node w1 w2 w3 w4 w5 w6) = do
156156 poke (castPtr p) w1
157− pokeByteOff (castPtr p) 1 w2
158− pokeByteOff (castPtr p) 2 w3
159− pokeByteOff (castPtr p) 3 w4
160− pokeByteOff (castPtr p) 4 w5
161− pokeByteOff (castPtr p) 5 w6
157+ pokeByteOff p 1 w2
158+ pokeByteOff p 2 w3
159+ pokeByteOff p 3 w4
160+ pokeByteOff p 4 w5
161+ pokeByteOff p 5 w6
162162
163163-- My goal with this instance was to make it work just enough to do what
164164-- I want when used with the HStringTemplate library.
Data/UUID/Internal.hsdeleted−63
@@ -1,63 +0,0 @@
1−-- |
2−-- Module : Data.UUID.Internal
3−-- Copyright : (c) 2008 Antoine Latter
4−--
5−-- License : BSD-style
6−--
7−-- Maintainer : aslatter@gmail.com
8−-- Stability : experimental
9−-- Portability : portable
10−--
11−-- If the public interface in "Data.UUID" doesn't give you
12−-- the flexibility you need, you should be able to find
13−-- something here.
14−
15−module Data.UUID.Internal(UUID(..)
16− ,toBytes
17− ,fromBytes
18− ,unsafeFromBytes
19− ,toForeignPtr
20− ,fromForeignPtr
21− ) where
22−
23−import Foreign.ForeignPtr
24−import Foreign.C
25−import Foreign
26−
27−-- |The UUID type. Represents 128-bits of identification.
28−newtype UUID = U (ForeignPtr CChar)
29−
30−-- |Returns the passed in UUID as a list of 16 bytes.
31−toBytes :: UUID -> [Word8]
32−toBytes (U fp) = unsafePerformIO $ withForeignPtr fp $ \p ->
33− peekArray 16 (castPtr p)
34−
35−-- |Creates a UUID out of a list of bytes.
36−-- Will throw an error if the list is not of length 16.
37−fromBytes :: [Word8] -> UUID
38−fromBytes xs = if length xs == 16
39− then unsafeFromBytes xs
40− else error "Data.UUID.Internal.fromBytes: passed in list of bytes must be of length 16."
41−
42−-- |Creates a UUID out of a list of bytes.
43−-- Does not perform a length check on the passed in list.
44−-- Behavior is undefined for lists not of length 16.
45−unsafeFromBytes :: [Word8] -> UUID
46−unsafeFromBytes xs = U $ castForeignPtr $ unsafePerformIO $ do
47− p <- mallocForeignPtrBytes 16
48− withForeignPtr p $ flip pokeArray xs
49− return p
50−
51−-- |Unsafe.
52−--
53−-- Given a UUID, returns a pointer to the 16 bytes
54−-- of memory that make up the UUID.
55−toForeignPtr :: UUID -> ForeignPtr CChar
56−toForeignPtr (U p) = p
57−
58−-- |Unsafe.
59−--
60−-- The passed in pointer is treated as if it were a pointer
61−-- to 16 bytes of memory, which is then returned as a UUID.
62−fromForeignPtr :: ForeignPtr CChar -> UUID
63−fromForeignPtr = U