moved all encodings to bytestring in- and output
This commit is contained in:
parent
82aed9340c
commit
6ab4cbaa79
|
@ -37,6 +37,8 @@ import Data.Char (ord, chr, digitToInt, intToDigit)
|
||||||
import qualified Data.ByteString.Char8 as BC
|
import qualified Data.ByteString.Char8 as BC
|
||||||
import Data.ByteString (ByteString)
|
import Data.ByteString (ByteString)
|
||||||
import qualified Data.ByteString as B
|
import qualified Data.ByteString as B
|
||||||
|
import Data.Bits ((.&.), shiftR)
|
||||||
|
import Data.List (unfoldr)
|
||||||
|
|
||||||
binaryToChar :: ByteString -> Char
|
binaryToChar :: ByteString -> Char
|
||||||
binaryToChar binStr = chr $ binaryToInt (BC.unpack binStr)
|
binaryToChar binStr = chr $ binaryToInt (BC.unpack binStr)
|
||||||
|
@ -47,10 +49,20 @@ binaryToInt binStr = foldl (\acc x -> acc * 2 + digitToInt x) 0 binStr
|
||||||
dec2 :: ByteString -> ByteString
|
dec2 :: ByteString -> ByteString
|
||||||
dec2 input = BC.pack . map binaryToChar . BC.words $ input
|
dec2 input = BC.pack . map binaryToChar . BC.words $ input
|
||||||
|
|
||||||
|
-- charToBinary :: Char -> ByteString
|
||||||
|
-- charToBinary char = BC.pack $ replicate (7 - length binaryStr) '0' ++ binaryStr
|
||||||
|
-- where
|
||||||
|
-- binaryStr = intToBinary (ord char)
|
||||||
charToBinary :: Char -> ByteString
|
charToBinary :: Char -> ByteString
|
||||||
charToBinary char = BC.pack $ replicate (7 - length binaryStr) '0' ++ binaryStr
|
charToBinary char = BC.pack $ replicate (8 - length binaryStr) '0' ++ binaryStr
|
||||||
where
|
where
|
||||||
binaryStr = intToBinary (ord char)
|
codePoint = ord char
|
||||||
|
binaryStr = padToLength 8 (intToBinary' codePoint)
|
||||||
|
|
||||||
|
padToLength :: Int -> String -> String
|
||||||
|
padToLength len str
|
||||||
|
| length str >= len = take len str
|
||||||
|
| otherwise = replicate (len - length str) '0' ++ str
|
||||||
|
|
||||||
intToBinary :: Int -> String
|
intToBinary :: Int -> String
|
||||||
intToBinary n = reverse $ decimalToBinary' n
|
intToBinary n = reverse $ decimalToBinary' n
|
||||||
|
@ -58,5 +70,8 @@ intToBinary n = reverse $ decimalToBinary' n
|
||||||
decimalToBinary' 0 = "0"
|
decimalToBinary' 0 = "0"
|
||||||
decimalToBinary' m = let (q, r) = m `divMod` 2 in intToDigit r : decimalToBinary' q
|
decimalToBinary' m = let (q, r) = m `divMod` 2 in intToDigit r : decimalToBinary' q
|
||||||
|
|
||||||
|
intToBinary' :: Int -> String
|
||||||
|
intToBinary' n = reverse $ take 8 $ unfoldr (\x -> if x == 0 then Nothing else Just (intToDigit $ x .&. 1, x `shiftR` 1)) n
|
||||||
|
|
||||||
enc2 :: ByteString -> ByteString
|
enc2 :: ByteString -> ByteString
|
||||||
enc2 input = BC.unwords . map charToBinary . BC.unpack $ input
|
enc2 input = BC.unwords . map charToBinary . BC.unpack $ input
|
||||||
|
|
|
@ -142,7 +142,7 @@ encodeChunk [x1] = BC.pack $ map (BC.index base45Alphabet) [b, a]
|
||||||
encodeChunk _ = error "Invalid chunk length"
|
encodeChunk _ = error "Invalid chunk length"
|
||||||
|
|
||||||
dec45 :: ByteString -> ByteString
|
dec45 :: ByteString -> ByteString
|
||||||
dec45 encoded = B.pack $ map fromIntegral decodedBytes
|
dec45 encoded = B.pack decodedBytes
|
||||||
where
|
where
|
||||||
numericValues = map charToNumeric (BC.unpack encoded)
|
numericValues = map charToNumeric (BC.unpack encoded)
|
||||||
groups = groupIntoThrees numericValues
|
groups = groupIntoThrees numericValues
|
||||||
|
@ -162,8 +162,10 @@ toBase45 :: [Int] -> Int
|
||||||
toBase45 [c, d, e] = c + d * 45 + e * (45 * 45)
|
toBase45 [c, d, e] = c + d * 45 + e * (45 * 45)
|
||||||
toBase45 _ = error "Invalid Base45 group"
|
toBase45 _ = error "Invalid Base45 group"
|
||||||
|
|
||||||
fromBase45 :: Int -> [Int]
|
-- fromBase45 :: Int -> [Int]
|
||||||
fromBase45 n = unfoldr (\x -> if x == 0 then Nothing else Just (x `mod` 256, x `div` 256)) n
|
-- fromBase45 n = unfoldr (\x -> if x == 0 then Nothing else Just (x `mod` 256, x `div` 256)) n
|
||||||
|
fromBase45 :: Int -> [Word8]
|
||||||
|
fromBase45 n = reverse $ unfoldr (\x -> if x == 0 then Nothing else Just (fromIntegral $ x `mod` 256, x `div` 256)) n
|
||||||
|
|
||||||
bytesToChars :: [Int] -> [Char]
|
bytesToChars :: [Int] -> [Char]
|
||||||
bytesToChars = map chr
|
bytesToChars = map chr
|
||||||
|
|
Loading…
Reference in New Issue