Биты хранятся в Vector
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
module Main (main) where
|
module Main (main) where
|
||||||
|
|
||||||
import Codec.Picture
|
import Codec.Picture
|
||||||
|
import qualified Data.Vector.Unboxed as VU
|
||||||
import Lib
|
import Lib
|
||||||
|
|
||||||
caesarShift :: Int
|
caesarShift :: Int
|
||||||
@@ -15,7 +16,7 @@ main = do
|
|||||||
let encryptedText = encryptCaesar alphabet caesarShift inputText
|
let encryptedText = encryptCaesar alphabet caesarShift inputText
|
||||||
putStrLn $ take 30 encryptedText
|
putStrLn $ take 30 encryptedText
|
||||||
let encryptedTextBits = textToBits encryptedText
|
let encryptedTextBits = textToBits encryptedText
|
||||||
putStrLn $ concat (take 30 (map show encryptedTextBits))
|
putStrLn $ concat (take 30 (map show (VU.toList encryptedTextBits)))
|
||||||
let encryptedTextFromBits = bitsToText encryptedTextBits
|
let encryptedTextFromBits = bitsToText encryptedTextBits
|
||||||
putStrLn $ take 30 encryptedTextFromBits
|
putStrLn $ take 30 encryptedTextFromBits
|
||||||
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits
|
let decryptedText = decryptCaesar alphabet caesarShift encryptedTextFromBits
|
||||||
|
|||||||
@@ -36,6 +36,7 @@ library
|
|||||||
build-depends:
|
build-depends:
|
||||||
JuicyPixels
|
JuicyPixels
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
|
, vector
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
executable lab3-exe
|
executable lab3-exe
|
||||||
@@ -51,6 +52,7 @@ executable lab3-exe
|
|||||||
JuicyPixels
|
JuicyPixels
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, lab3
|
, lab3
|
||||||
|
, vector
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|
||||||
test-suite lab3-test
|
test-suite lab3-test
|
||||||
@@ -67,4 +69,5 @@ test-suite lab3-test
|
|||||||
JuicyPixels
|
JuicyPixels
|
||||||
, base >=4.7 && <5
|
, base >=4.7 && <5
|
||||||
, lab3
|
, lab3
|
||||||
|
, vector
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ description: Please see the README on GitHub at <https://github.com/gith
|
|||||||
dependencies:
|
dependencies:
|
||||||
- base >= 4.7 && < 5
|
- base >= 4.7 && < 5
|
||||||
- JuicyPixels
|
- JuicyPixels
|
||||||
|
- vector
|
||||||
|
|
||||||
ghc-options:
|
ghc-options:
|
||||||
- -Wall
|
- -Wall
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ module Lib
|
|||||||
|
|
||||||
import Data.Char (ord, chr)
|
import Data.Char (ord, chr)
|
||||||
import Data.Bits (testBit)
|
import Data.Bits (testBit)
|
||||||
|
import qualified Data.Vector.Unboxed as VU
|
||||||
|
|
||||||
createAlphabetFromText :: String -> [Char]
|
createAlphabetFromText :: String -> [Char]
|
||||||
createAlphabetFromText [] = []
|
createAlphabetFromText [] = []
|
||||||
@@ -35,13 +36,15 @@ decryptCaesar alphabet shift =
|
|||||||
where
|
where
|
||||||
alphabetLength = length alphabet
|
alphabetLength = length alphabet
|
||||||
|
|
||||||
textToBits :: String -> [Int]
|
textToBits :: String -> VU.Vector Int
|
||||||
textToBits = concatMap charToBits
|
textToBits text = VU.fromList $ concatMap charToBits text
|
||||||
where charToBits c = [ if testBit (ord c) i then 1 else 0 | i <- [7,6..0] ]
|
where
|
||||||
|
charToBits c = [if testBit (ord c) i then 1 else 0 | i <- [7,6..0]]
|
||||||
|
|
||||||
bitsToText :: [Int] -> String
|
bitsToText :: VU.Vector Int -> String
|
||||||
bitsToText [] = []
|
bitsToText bits
|
||||||
bitsToText bits = (chr $ bitsToInt (take 8 bits)) : bitsToText (drop 8 bits)
|
| VU.null bits = []
|
||||||
|
| otherwise = (chr $ bitsToInt (VU.take 8 bits)) : bitsToText (VU.drop 8 bits)
|
||||||
where
|
where
|
||||||
bitsToInt charBits =
|
bitsToInt charBits =
|
||||||
sum [bit * (2 ^ index) | (bit, index) <- zip charBits [7 :: Int, 6 .. 0]]
|
sum [bit * (2 ^ index) | (bit, index) <- zip (VU.toList charBits) [7 :: Int,6..0]]
|
||||||
Reference in New Issue
Block a user