summaryrefslogtreecommitdiff
path: root/Codec
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2015-06-22 15:11:32 -0400
committerJames Crayne <jim.crayne@gmail.com>2015-06-22 15:11:32 -0400
commitaed7356d85229ae0ee19d55edb682e6212b5a8a0 (patch)
tree2a6a9b1a10812e5d13e9df54d7b168e767c07428 /Codec
parent3989ea283124745ab378c440459138f66602dc52 (diff)
WIP Serializing.. kikid
Diffstat (limited to 'Codec')
-rw-r--r--Codec/SafeBlob.hs22
1 files changed, 22 insertions, 0 deletions
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 @@
1{-# LANGUAGE ViewPatterns #-}
2module Codec.SafeBlob where
3
4import qualified Data.ByteString.Char8 as B
5import Data.Monoid
6import Data.List (foldl')
7import Data.Maybe
8
9toSafe :: B.ByteString -> B.ByteString
10toSafe blob = let as = zip [0..] (B.unpack blob)
11 bs = filter ((=='\n') . snd) as
12 is = map fst bs
13 in B.pack (show is) <> foldl' (replaceCharStrIndex '#') blob is
14
15replaceCharStrIndex :: Char -> B.ByteString -> Int -> B.ByteString
16replaceCharStrIndex c str i = a <> B.singleton c <> b
17 where (a,B.uncons -> Just (_,b)) = B.splitAt i str
18
19fromSafe str = foldl' (replaceCharStrIndex '\n') (B.drop 1 str') is
20 where is = map fst . mapMaybe B.readInt $
21 B.groupBy (\c d -> (c/=',')&&(d/=',')) ls
22 (ls,str') = B.break (==']') (B.tail str)