summaryrefslogtreecommitdiff
path: root/Codec/LineReady.hs
blob: a6961ca9523221027d7f6c17fe827ebd3970e1fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)