summaryrefslogtreecommitdiff
path: root/Codec/SafeBlob.hs
blob: a6db80edbb6974a1622a977c3a33f99216cb6da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
{-# LANGUAGE ViewPatterns #-}
module Codec.SafeBlob where

import qualified Data.ByteString.Char8 as B
import Data.Monoid
import Data.List (foldl')
import Data.Maybe

toSafe :: B.ByteString -> B.ByteString
toSafe 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 
                    where (a,B.uncons -> Just (_,b)) = B.splitAt i str

fromSafe 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.tail str)