From b60acf2008f8a4f4503066afade300e9c68209eb Mon Sep 17 00:00:00 2001 From: stefan Date: Thu, 27 Jun 2024 23:16:56 +0200 Subject: [PATCH] added bindings for morse code, bug fix --- app/Main.hs | 9 ++++++++- src/Encoding/Morse.hs | 3 ++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/Main.hs b/app/Main.hs index 677d2c6..6589eb7 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -23,6 +23,7 @@ import Encoding.UnixToUnix (encuu, decuu) import Encoding.Yenc (ency, decy) import Encoding.LetterToNumber (encltn, decltn) import Encoding.Rotate (rotate) +import Encoding.Morse (encmorse, decmorse) import Encoding.Solve (solveEnc) @@ -46,6 +47,7 @@ data Based = Decode { yenc :: Bool, a1z26 :: Bool, rot :: Maybe Int, + morse :: Bool, solve :: Bool } | Encode { @@ -67,6 +69,7 @@ data Based = Decode { xx :: Bool, yenc :: Bool, a1z26 :: Bool, + morse :: Bool, rot :: Maybe Int } deriving(Show, Data, Typeable) @@ -111,6 +114,8 @@ optionHandler Decode{a1z26=True} = decltn optionHandler Encode{a1z26=True} = encltn optionHandler Decode{rot=Just n} = rotate n optionHandler Encode{rot=Just n} = rotate n +optionHandler Decode{morse=True} = decmorse +optionHandler Encode{morse=True} = encmorse optionHandler Decode{solve=True} = solveEnc decodeMode :: Based @@ -134,6 +139,7 @@ decodeMode = Decode { yenc = def &= help "decode yEncode", a1z26 = def &= help "decode letter to number", rot = def &= help "rotate characters by n positions", + morse = def &= help "decode morse", solve = def &= help "solve encoding" } &= help "Decode chosen base" &=auto @@ -157,7 +163,8 @@ encodeMode = Encode { xx = def &= help "encode xx, without padding", yenc = def &= help "encode yEncode", a1z26 = def &= help "encode letter to number", - rot = def &= help "rotate characters by n positions" + rot = def &= help "rotate characters by n positions", + morse = def &= help "encode morse" } &= help "Encode chosen base" main :: IO() diff --git a/src/Encoding/Morse.hs b/src/Encoding/Morse.hs index 5063e23..746f36f 100644 --- a/src/Encoding/Morse.hs +++ b/src/Encoding/Morse.hs @@ -5,13 +5,14 @@ module Encoding.Morse import Text.Morse as M import Data.ByteString (ByteString) +import Data.Char (isSpace) import qualified Data.ByteString.Char8 as BC encmorse :: ByteString -> ByteString encmorse input = BC.pack $ encodeValidMorseChars (BC.unpack input) where encodeValidMorseChars :: String -> String - encodeValidMorseChars = concatMap (\c -> if canEncodeToMorse c then M.encodeMorse [c] else "") + encodeValidMorseChars = concatMap (\c -> M.encodeMorse [c]) decmorse :: ByteString -> ByteString decmorse input = case M.decodeMorse (BC.unpack input) of