changed rotate to match ByteStrings instead of Strings

This commit is contained in:
Stefan Friese 2024-06-08 20:26:16 +02:00
parent fbb10d0edf
commit b91f110f42
2 changed files with 31 additions and 19 deletions

View File

@ -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()

View File

@ -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 :: 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))