-
Notifications
You must be signed in to change notification settings - Fork 1
/
MB.hs
58 lines (42 loc) · 1.2 KB
/
MB.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
module MB(
MB,
xorMB,
zeroMB,
splitInMBs,
combineMBs,
)
where
import Data.Bits(xor)
import Data.Char(chr,ord)
import Rhs
data MB = MB String
showMB :: MB -> String
showMB (MB mb) =
"< " ++ (summ mb) ++ "> "
where
summ [] = " 0"
summ (x : r) = show x ++ " ..." ++ show (length mb)
xorMB :: MB -> MB -> MB
xorMB (MB a) (MB b) = MB (zipWith xorC a b)
where xorC ca cb = chr ((ord ca) `xor` (ord cb))
c0 = chr 0
zeroMB :: Int -> MB
zeroMB n = MB (replicate n c0)
_rconvert :: String -> MB
_rconvert mb = MB (reverse mb)
_split :: Int -> [MB] -> String -> Int -> String -> [MB]
_split bs acc cb i [] = reverse (rcb : acc) where rcb = _rconvert cb
_split bs acc cb i (c : t) | i == bs = _split bs (rcb:acc) [c] 1 t where rcb = _rconvert cb
_split bs acc cb i (c : t) = _split bs acc (c:cb) (i+1) t
splitInMBs :: Int -> String -> [MB]
splitInMBs bs = _split bs [] [] 0
combineMBs :: [MB] -> Int -> String
combineMBs mbs n =
x
where
(x,_) = foldl (\ (acc,i) (MB mb) -> if i < n then (acc ++ mb, i+1) else (acc,i+1))
("",0) mbs
instance Show MB where
show = showMB
instance RhsC MB where
(-:) = xorMB