From b91f110f4296bde060f558e6dc648f876da90382 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Sat, 8 Jun 2024 20:26:16 +0200 Subject: [PATCH] changed rotate to match ByteStrings instead of Strings --- app/Main.hs | 22 +++++++++++----------- src/Encoding/Rotate.hs | 28 ++++++++++++++++++++-------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 90c7ca9..27e73af 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -26,7 +26,7 @@ import Encoding.Xx (encxx, decxx) import Encoding.QuotedPrintable (encqp, decqp) import Encoding.UnixToUnix (encuu, decuu) import Encoding.Yenc (ency, decy) ---import Encoding.Rotate (rotate) +import Encoding.Rotate (rotate) data Based = Decode { @@ -46,8 +46,8 @@ data Based = Decode { qp :: Bool, uu :: Bool, xx :: Bool, - yenc :: Bool --- rot :: Maybe Int, + yenc :: Bool, + rot :: Maybe Int -- solve :: Bool } | Encode { @@ -67,8 +67,8 @@ data Based = Decode { qp :: Bool, uu :: Bool, xx :: Bool, - yenc :: Bool --- rot :: Maybe Int + yenc :: Bool, + rot :: Maybe Int } deriving(Show, Data, Typeable) @@ -185,8 +185,8 @@ optionHandler Decode{xx=True} = decxx optionHandler Encode{xx=True} = encxx optionHandler Decode{yenc=True} = decy optionHandler Encode{yenc=True} = ency ---optionHandler Encode{rot=Just n} = rotate n ---optionHandler Decode{rot=Just n} = rotate n +optionHandler Encode{rot=Just n} = rotate n +optionHandler Decode{rot=Just n} = rotate n --optionHandler Decode{solve=True} = solveEnc decodeMode :: Based @@ -207,8 +207,8 @@ decodeMode = Decode { qp = def &= help "decode quoted-printable", uu = def &= help "decode uu", xx = def &= help "decode xx", - yenc = def &= help "decode yEncode" --- rot = def &= help "rotate characters by n positions", + yenc = def &= help "decode yEncode", + rot = def &= help "rotate characters by n positions" -- solve = def &= help "solve encoding" } &= help "Decode chosen base" &=auto @@ -230,8 +230,8 @@ encodeMode = Encode { qp = def &= help "encode quoted-printable", uu = def &= help "encode uu", xx = def &= help "encode xx", - yenc = def &= help "encode yEncode" --- rot = def &= help "rotate characters by n positions" + yenc = def &= help "encode yEncode", + rot = def &= help "rotate characters by n positions" } &= help "Encode chosen base" --main :: IO() diff --git a/src/Encoding/Rotate.hs b/src/Encoding/Rotate.hs index fde0e53..8fcd2be 100644 --- a/src/Encoding/Rotate.hs +++ b/src/Encoding/Rotate.hs @@ -2,21 +2,33 @@ module Encoding.Rotate ( rotate ) where import Data.Maybe (fromMaybe) +import Data.ByteString as B +import qualified Data.ByteString.Char8 as BC -upperCase = ['A' .. 'Z'] -lowerCase = ['a' .. 'z'] +upperCase = BC.pack ['A' .. 'Z'] +lowerCase = BC.pack ['a' .. 'z'] --- rotate :: Int * -> String -rotate :: Int -> String -> String -rotate n s = map (rotchar n) s +-- rotate :: Int -> String -> String +-- rotate n s = map (rotchar n) s +-- where +-- rotchar n c = +-- case (lookup c $ transp n lowerCase) of +-- Just x -> x +-- Nothing -> fromMaybe c (lookup c $ transp n upperCase) +rotate :: Int -> BC.ByteString -> BC.ByteString +rotate n s = BC.map (rotchar n) s where - rotchar n c = + rotchar :: Int -> Char -> Char + rotchar n c = case (lookup c $ transp n lowerCase) of Just x -> x Nothing -> fromMaybe c (lookup c $ transp n upperCase) -transp :: Int -> [Char] -> [(Char, Char)] -transp n x = zip x ((drop n x) ++ (take n x)) + +-- transp :: Int -> [Char] -> [(Char, Char)] +-- transp n x = zip x ((drop n x) ++ (take n x)) +transp :: Int -> BC.ByteString -> [(Char, Char)] +transp n x = Prelude.zip (BC.unpack x) (BC.unpack $ BC.append (BC.drop n x) (BC.take n x))