Compare commits
2 Commits
Author | SHA1 | Date |
---|---|---|
Stefan Friese | 85b57ef9aa | |
Stefan Friese | e39ac84573 |
|
@ -1,5 +1,13 @@
|
||||||
Revision history for based
|
Revision history for based
|
||||||
|
|
||||||
|
0.4.5.0 -- 2024-09-25
|
||||||
|
|
||||||
|
Added tap code
|
||||||
|
Removed some bugs of the solve functions
|
||||||
|
Added bindings for morse code, bug fix
|
||||||
|
Simplified functions, ignore non ascii characters
|
||||||
|
Added test for rotate function and yencode/decode
|
||||||
|
|
||||||
0.4.4.0 -- 2024-06-09
|
0.4.4.0 -- 2024-06-09
|
||||||
|
|
||||||
Added tests for every encode function included
|
Added tests for every encode function included
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
module Encoding.Base2
|
module Encoding.Base2
|
||||||
( enc2
|
( enc2
|
||||||
, dec2
|
, dec2
|
||||||
|
, binaryToChar
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Data.Char (ord, chr, digitToInt, intToDigit)
|
import Data.Char (ord, chr, digitToInt, intToDigit)
|
||||||
|
|
|
@ -0,0 +1,63 @@
|
||||||
|
module Encoding.Unary
|
||||||
|
( encunary
|
||||||
|
, decunary
|
||||||
|
) where
|
||||||
|
|
||||||
|
import Data.ByteString (ByteString)
|
||||||
|
import Data.List (group)
|
||||||
|
import qualified Data.ByteString.Char8 as BC
|
||||||
|
import Encoding.Base2 (dec2, enc2, binaryToChar)
|
||||||
|
import Debug.Trace (trace)
|
||||||
|
|
||||||
|
encodeChuckNorris :: ByteString -> ByteString
|
||||||
|
encodeChuckNorris binary =
|
||||||
|
let cleanBinary = BC.filter (/= ' ') binary
|
||||||
|
in BC.unwords . concatMap encodeRunLength . group $ BC.unpack cleanBinary
|
||||||
|
where
|
||||||
|
encodeRunLength :: String -> [ByteString]
|
||||||
|
encodeRunLength xs@(x:_) = [prefix x, BC.pack (replicate (length xs) '0')]
|
||||||
|
encodeRunLength [] = []
|
||||||
|
|
||||||
|
prefix '0' = BC.pack "00"
|
||||||
|
prefix '1' = BC.pack "0"
|
||||||
|
prefix c = error $ "Invalid binary character encoding" ++ show c
|
||||||
|
|
||||||
|
pairs :: [a] -> [(a, a)]
|
||||||
|
pairs (x:y:rest) = (x, y) : pairs rest
|
||||||
|
pairs _ = []
|
||||||
|
|
||||||
|
encunary :: ByteString -> ByteString
|
||||||
|
encunary input = encodeChuckNorris $ enc2 input
|
||||||
|
|
||||||
|
decodeChuckNorris :: ByteString -> ByteString
|
||||||
|
decodeChuckNorris encoded = BC.pack . concatMap (BC.unpack . decodeRunLength) . pairs . BC.words $ encoded
|
||||||
|
where
|
||||||
|
decodeRunLength :: (ByteString, ByteString) -> ByteString
|
||||||
|
decodeRunLength (prefix, zeros)
|
||||||
|
| prefix == BC.pack "00" = BC.replicate (BC.length zeros) '0'
|
||||||
|
| prefix == BC.pack "0" = BC.replicate (BC.length zeros) '1'
|
||||||
|
| otherwise = error "Invalid Unary encoding"
|
||||||
|
-- pairs (x:y:rest) = (x, y) : pairs rest
|
||||||
|
-- pairs _ = []
|
||||||
|
|
||||||
|
-- decodeRunLength ("00", zeros) = replicate (BC.length zeros) '0'
|
||||||
|
-- decodeRunLength ("0", zeros) = replicate (BC.length zeros) '1'
|
||||||
|
-- decodeRunLength _ = error "Invalid Unary encoding"
|
||||||
|
|
||||||
|
decunary :: ByteString -> ByteString
|
||||||
|
decunary encoded =
|
||||||
|
let binaryStr = decodeChuckNorris encoded
|
||||||
|
in BC.pack $ map binaryToChar (chunkBinaryString binaryStr)
|
||||||
|
where
|
||||||
|
chunkBinaryString :: ByteString -> [ByteString]
|
||||||
|
chunkBinaryString bs
|
||||||
|
| BC.null bs = []
|
||||||
|
| otherwise = let (chunk, rest) = BC.splitAt 8 bs
|
||||||
|
in chunk : chunkBinaryString rest
|
||||||
|
|
||||||
|
-- encunary input =
|
||||||
|
-- let binaryStr = enc2 input
|
||||||
|
-- in trace (show binaryStr) $ encodeChuckNorris binaryStr
|
||||||
|
|
||||||
|
-- decunary :: ByteString -> ByteString
|
||||||
|
-- decunary encoded = dec2 $ decodeChuckNorris encoded
|
Loading…
Reference in New Issue