added bindings for morse code, bug fix

This commit is contained in:
Stefan Friese 2024-06-27 23:16:56 +02:00
parent 3bd5530544
commit b60acf2008
2 changed files with 10 additions and 2 deletions

View File

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

View File

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