File: Common.hs

package info (click to toggle)
haskell-binary-parsers 0.2.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,252 kB
  • sloc: haskell: 2,156; makefile: 5
file content (43 lines) | stat: -rw-r--r-- 1,100 bytes parent folder | download | duplicates (4)
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
39
40
41
42
43
{-# LANGUAGE CPP #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}

module Common (
      chunksOf
    , pathTo
    , rechunkBS
    , rechunkT
    ) where

import Control.DeepSeq (NFData(rnf))
import System.Directory (doesDirectoryExist)
import System.FilePath ((</>))
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL
import qualified Data.Text as T
import qualified Data.Text.Lazy as TL

#if !MIN_VERSION_bytestring(0,10,0)
import Data.ByteString.Internal (ByteString(..))

instance NFData ByteString where
    rnf (PS _ _ _) = ()
#endif

chunksOf :: Int -> [a] -> [[a]]
chunksOf k = go
  where go xs = case splitAt k xs of
                  ([],_)  -> []
                  (y, ys) -> y : go ys

rechunkBS :: Int -> B.ByteString -> BL.ByteString
rechunkBS n = BL.fromChunks . map B.pack . chunksOf n . B.unpack

rechunkT :: Int -> T.Text -> TL.Text
rechunkT n = TL.fromChunks . map T.pack . chunksOf n . T.unpack

pathTo :: String -> IO FilePath
pathTo wat = do
  exists <- doesDirectoryExist "bench"
  return $ if exists
           then "bench" </> wat
           else wat