diff options
author | James Crayne <jim.crayne@gmail.com> | 2015-06-22 18:58:47 -0400 |
---|---|---|
committer | James Crayne <jim.crayne@gmail.com> | 2015-06-22 18:59:39 -0400 |
commit | 2966db997f43c063389285ddc40579acad5c6a29 (patch) | |
tree | 76a2aaa39aae69d892162fc16754c5993329ce70 /Codec | |
parent | aed7356d85229ae0ee19d55edb682e6212b5a8a0 (diff) |
kikid: Serialization...
Diffstat (limited to 'Codec')
-rw-r--r-- | Codec/LineReady.hs | 23 | ||||
-rw-r--r-- | Codec/SafeBlob.hs | 22 |
2 files changed, 23 insertions, 22 deletions
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 @@ | |||
1 | module Codec.LineReady where | ||
2 | |||
3 | import qualified Data.ByteString.Char8 as B | ||
4 | import Data.Monoid | ||
5 | import Data.List (foldl') | ||
6 | import Data.Maybe | ||
7 | |||
8 | toLineReady :: B.ByteString -> B.ByteString | ||
9 | toLineReady blob = | ||
10 | 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 <> B.singleton '\n' | ||
14 | |||
15 | replaceCharStrIndex :: Char -> B.ByteString -> Int -> B.ByteString | ||
16 | replaceCharStrIndex c str i = a <> B.singleton c <> B.drop 1 b | ||
17 | where (a,b) = B.splitAt i str | ||
18 | |||
19 | fromLineReady :: B.ByteString -> B.ByteString | ||
20 | fromLineReady str = foldl' (replaceCharStrIndex '\n') (B.drop 1 str') is | ||
21 | where is = map fst . mapMaybe B.readInt $ | ||
22 | B.groupBy (\c d -> (c/=',')&&(d/=',')) ls | ||
23 | (ls,str') = B.break (==']') (B.tail str) | ||
diff --git a/Codec/SafeBlob.hs b/Codec/SafeBlob.hs deleted file mode 100644 index a6db80e..0000000 --- a/Codec/SafeBlob.hs +++ /dev/null | |||
@@ -1,22 +0,0 @@ | |||
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) | ||