blob: 73a1c4cb0dfcb5aacb62f354c37600fbb57280db (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
-- |
-- Module : Verismith
-- Description : Verismith
-- Copyright : (c) 2020, Yann Herklotz
-- License : GPL-3
-- Maintainer : yann [at] yannherklotz [dot] com
-- Stability : experimental
-- Portability : POSIX
module Verismith.Utils
( generateByteString,
)
where
import qualified Data.ByteString.Lazy as L
import Data.Text (Text)
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8)
import qualified Data.Text.IO as T
import Data.ByteString.Builder (byteStringHex, toLazyByteString)
import Data.ByteString (ByteString, pack)
import System.Random (mkStdGen, newStdGen, randoms)
generateByteString :: (Maybe Int) -> Int -> Int -> IO [ByteString]
generateByteString mseed size n = do
fmap pack . chunksOf size . take (size * n) . randoms
<$> case mseed of
Just seed' -> return $ mkStdGen seed'
Nothing -> newStdGen
where
chunksOf i _ | i <= 0 = error $ "chunksOf, number must be positive, got " ++ show i
chunksOf i xs = repeatedly (splitAt i) xs
repeatedly _ [] = []
repeatedly f as = b : repeatedly f as'
where
(b, as') = f as
showBS :: ByteString -> Text
showBS = decodeUtf8 . L.toStrict . toLazyByteString . byteStringHex
|