From aed7356d85229ae0ee19d55edb682e6212b5a8a0 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Mon, 22 Jun 2015 15:11:32 -0400 Subject: WIP Serializing.. kikid --- Codec/SafeBlob.hs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Codec/SafeBlob.hs (limited to 'Codec/SafeBlob.hs') diff --git a/Codec/SafeBlob.hs b/Codec/SafeBlob.hs new file mode 100644 index 0000000..a6db80e --- /dev/null +++ b/Codec/SafeBlob.hs @@ -0,0 +1,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) -- cgit v1.2.3