git.iko.soy/typeable/pandoc
on f449dfd help sign in
77 lines 2.3 kB history raw
1{-
2Copyright (C) 2010 John MacFarlane <jgm@berkeley.edu>
4This program is free software; you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation; either version 2 of the License, or
7(at your option) any later version.
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
14You should have received a copy of the GNU General Public License
15along with this program; if not, write to the Free Software
16Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17-}
19{- |
20 Module : Text.Pandoc.UUID
21 Copyright : Copyright (C) 2010 John MacFarlane
22 License : GNU GPL, version 2 or above
24 Maintainer : John MacFarlane <jgm@berkeley.edu>
25 Stability : alpha
26 Portability : portable
28UUID generation using Version 4 (random method) described
29in RFC4122. See http://tools.ietf.org/html/rfc4122
30-}
32module Text.Pandoc.UUID ( UUID, getRandomUUID ) where
34import Text.Printf ( printf )
35import System.Random ( randomIO )
36import Data.Word
37import Data.Bits ( setBit, clearBit )
38import Control.Monad ( liftM )
40data UUID = UUID Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8
41 Word8 Word8 Word8 Word8 Word8 Word8 Word8 Word8
43instance Show UUID where
44 show (UUID a b c d e f g h i j k l m n o p) =
45 "urn:uuid:" ++
46 printf "%02x" a ++
47 printf "%02x" b ++
48 printf "%02x" c ++
49 printf "%02x" d ++
50 "-" ++
51 printf "%02x" e ++
52 printf "%02x" f ++
53 "-" ++
54 printf "%02x" g ++
55 printf "%02x" h ++
56 "-" ++
57 printf "%02x" i ++
58 printf "%02x" j ++
59 "-" ++
60 printf "%02x" k ++
61 printf "%02x" l ++
62 printf "%02x" m ++
63 printf "%02x" n ++
64 printf "%02x" o ++
65 printf "%02x" p
67getRandomUUID :: IO UUID
68getRandomUUID = do
69 let getRN :: a -> IO Word8
70 getRN _ = liftM fromIntegral (randomIO :: IO Int)
71 [a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p] <- mapM getRN ([1..16] :: [Int])
72 -- set variant
73 let i' = i `setBit` 7 `clearBit` 6
74 -- set version (0100 for random)
75 let g' = g `clearBit` 7 `setBit` 6 `clearBit` 5 `clearBit` 4
76 return $ UUID a b c d e f g' h i' j k l m n o p