1-- | Benchmarking utilities.  For example, functions for generating
2-- random strings.
3module Util.String where
4
5import System.Random (mkStdGen, randomRs)
6
7-- | Generate a number of fixed length strings where the content of
8-- the strings are letters in ascending order.
9asc :: Int  -- ^ Length of each string
10    -> Int  -- ^ Number of strings
11    -> [String]
12asc strlen num = take num $ iterate (snd . inc) $ replicate strlen 'a'
13  where inc [] = (True, [])
14        inc (c:cs) = case inc cs of (True, cs') | c == 'z'  -> (True, 'a' : cs')
15                                                | otherwise -> (False, succ c : cs')
16                                    (False, cs')            -> (False, c : cs')
17
18-- | Generate a number of fixed length strings where the content of
19-- the strings are letters in random order.
20rnd :: Int  -- ^ Length of each string
21    -> Int  -- ^ Number of strings
22    -> [String]
23rnd strlen num = take num $ split $ randomRs ('a', 'z') $ mkStdGen 1234
24  where
25    split cs = case splitAt strlen cs of (str, cs') -> str : split cs'
26
27-- | Generate a number of fixed length strings where the content of
28-- the strings are letters in random order, different from rnd
29rnd' :: Int  -- ^ Length of each string
30     -> Int  -- ^ Number of strings
31     -> [String]
32rnd' strlen num = take num $ split $ randomRs ('a', 'z') $ mkStdGen 5678
33  where
34    split cs = case splitAt strlen cs of (str, cs') -> str : split cs'
35