module Codec.LineReady where import qualified Data.ByteString.Char8 as B import Data.Monoid import Data.List (foldl') import Data.Maybe toLineReady :: B.ByteString -> B.ByteString toLineReady blob = let as = zip [0..] (B.unpack blob) bs = filter ((=='\n') . snd) as is = map fst bs in B.pack (show is) <> foldl' (replaceCharStrIndex '#') blob is replaceCharStrIndex :: Char -> B.ByteString -> Int -> B.ByteString replaceCharStrIndex c str i = a <> B.singleton c <> B.drop 1 b where (a,b) = B.splitAt i str fromLineReady :: B.ByteString -> B.ByteString fromLineReady str = foldl' (replaceCharStrIndex '\n') (B.drop 1 str') is where is = map fst . mapMaybe B.readInt $ B.groupBy (\c d -> (c/=',')&&(d/=',')) ls (ls,str') = B.break (==']') (B.drop 1 str)