summaryrefslogtreecommitdiff
path: root/Codec
diff options
context:
space:
mode:
Diffstat (limited to 'Codec')
-rw-r--r--Codec/LineReady.hs23
-rw-r--r--Codec/SafeBlob.hs22
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 @@
1module Codec.LineReady where
2
3import qualified Data.ByteString.Char8 as B
4import Data.Monoid
5import Data.List (foldl')
6import Data.Maybe
7
8toLineReady :: B.ByteString -> B.ByteString
9toLineReady 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
15replaceCharStrIndex :: Char -> B.ByteString -> Int -> B.ByteString
16replaceCharStrIndex c str i = a <> B.singleton c <> B.drop 1 b
17 where (a,b) = B.splitAt i str
18
19fromLineReady :: B.ByteString -> B.ByteString
20fromLineReady 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 #-}
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)