From 3bd5530544bd5498ba13610c5fec04c4d97567a0 Mon Sep 17 00:00:00 2001 From: stefan Date: Wed, 26 Jun 2024 23:56:59 +0200 Subject: [PATCH] added morse encoding, WIP --- based.cabal | 4 +++- src/Encoding/Morse.hs | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 src/Encoding/Morse.hs diff --git a/based.cabal b/based.cabal index 3e21a98..90ed323 100644 --- a/based.cabal +++ b/based.cabal @@ -26,6 +26,7 @@ library Encoding.Yenc Encoding.Rotate Encoding.LetterToNumber + Encoding.Morse Encoding.Solve other-modules: -- Data.Bytes.Text.Ascii @@ -45,7 +46,8 @@ library text, primitive, regex-tdfa, - base64-bytestring + base64-bytestring, + MorseCode default-language: Haskell2010 executable based diff --git a/src/Encoding/Morse.hs b/src/Encoding/Morse.hs new file mode 100644 index 0000000..5063e23 --- /dev/null +++ b/src/Encoding/Morse.hs @@ -0,0 +1,20 @@ +module Encoding.Morse + ( encmorse + , decmorse + ) where + +import Text.Morse as M +import Data.ByteString (ByteString) +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 "") + +decmorse :: ByteString -> ByteString +decmorse input = case M.decodeMorse (BC.unpack input) of + "" -> BC.pack "Invalid Morse Code" + decoded -> BC.pack decoded +