diff options
author | James Crayne <jim.crayne@gmail.com> | 2015-06-22 15:11:32 -0400 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2015-06-22 15:11:32 -0400 |
commit | aed7356d85229ae0ee19d55edb682e6212b5a8a0 (patch) | |
tree | 2a6a9b1a10812e5d13e9df54d7b168e767c07428 | |
parent | 3989ea283124745ab378c440459138f66602dc52 (diff) |
WIP Serializing.. kikid
-rw-r--r-- | Codec/SafeBlob.hs | 22 |
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 #-} | ||
2 | module Codec.SafeBlob where | ||
3 | |||
4 | import qualified Data.ByteString.Char8 as B | ||
5 | import Data.Monoid | ||
6 | import Data.List (foldl') | ||
7 | import Data.Maybe | ||
8 | |||
9 | toSafe :: B.ByteString -> B.ByteString | ||
10 | toSafe 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 | |||
15 | replaceCharStrIndex :: Char -> B.ByteString -> Int -> B.ByteString | ||
16 | replaceCharStrIndex c str i = a <> B.singleton c <> b | ||
17 | where (a,B.uncons -> Just (_,b)) = B.splitAt i str | ||
18 | |||
19 | fromSafe 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) | ||