From 2966db997f43c063389285ddc40579acad5c6a29 Mon Sep 17 00:00:00 2001 From: James Crayne Date: Mon, 22 Jun 2015 18:58:47 -0400 Subject: kikid: Serialization... --- Codec/LineReady.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Codec/LineReady.hs (limited to 'Codec/LineReady.hs') diff --git a/Codec/LineReady.hs b/Codec/LineReady.hs new file mode 100644 index 0000000..ca2cde3 --- /dev/null +++ b/Codec/LineReady.hs @@ -0,0 +1,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 <> B.singleton '\n' + +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.tail str) -- cgit v1.2.3