git.iko.soy/typeable/attoparsec
on master help sign in
47 lines 1.2 kB history raw
1{-# LANGUAGE CPP #-}
2{-# OPTIONS_GHC -fno-warn-orphans #-}
4module Common (
5 chunksOf
6 , pathTo
7 , rechunkBS
8 , rechunkT
9 ) where
11import Control.DeepSeq (NFData(rnf))
12import System.Directory (doesDirectoryExist)
13import System.FilePath ((</>))
14import Text.Parsec (ParseError)
15import qualified Data.ByteString as B
16import qualified Data.ByteString.Lazy as BL
17import qualified Data.Text as T
18import qualified Data.Text.Lazy as TL
20#if !MIN_VERSION_bytestring(0,10,0)
21import Data.ByteString.Internal (ByteString(..))
23instance NFData ByteString where
24 rnf (PS _ _ _) = ()
25#endif
27instance NFData ParseError where
28 rnf = rnf . show
30chunksOf :: Int -> [a] -> [[a]]
31chunksOf k = go
32 where go xs = case splitAt k xs of
33 ([],_) -> []
34 (y, ys) -> y : go ys
36rechunkBS :: Int -> B.ByteString -> BL.ByteString
37rechunkBS n = BL.fromChunks . map B.pack . chunksOf n . B.unpack
39rechunkT :: Int -> T.Text -> TL.Text
40rechunkT n = TL.fromChunks . map T.pack . chunksOf n . T.unpack
42pathTo :: String -> IO FilePath
43pathTo wat = do
44 exists <- doesDirectoryExist "benchmarks"
45 return $ if exists
46 then "benchmarks" </> wat
47 else wat