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.Yenc (ency, decy)
import Encoding.LetterToNumber (encltn, decltn) import Encoding.LetterToNumber (encltn, decltn)
import Encoding.Rotate (rotate) import Encoding.Rotate (rotate)
import Encoding.Morse (encmorse, decmorse)
import Encoding.Solve (solveEnc) import Encoding.Solve (solveEnc)
@ -46,6 +47,7 @@ data Based = Decode {
yenc :: Bool, yenc :: Bool,
a1z26 :: Bool, a1z26 :: Bool,
rot :: Maybe Int, rot :: Maybe Int,
morse :: Bool,
solve :: Bool solve :: Bool
} }
| Encode { | Encode {
@ -67,6 +69,7 @@ data Based = Decode {
xx :: Bool, xx :: Bool,
yenc :: Bool, yenc :: Bool,
a1z26 :: Bool, a1z26 :: Bool,
morse :: Bool,
rot :: Maybe Int rot :: Maybe Int
} }
deriving(Show, Data, Typeable) deriving(Show, Data, Typeable)
@ -111,6 +114,8 @@ optionHandler Decode{a1z26=True} = decltn
optionHandler Encode{a1z26=True} = encltn optionHandler Encode{a1z26=True} = encltn
optionHandler Decode{rot=Just n} = rotate n optionHandler Decode{rot=Just n} = rotate n
optionHandler Encode{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 optionHandler Decode{solve=True} = solveEnc
decodeMode :: Based decodeMode :: Based
@ -134,6 +139,7 @@ decodeMode = Decode {
yenc = def &= help "decode yEncode", yenc = def &= help "decode yEncode",
a1z26 = def &= help "decode letter to number", a1z26 = def &= help "decode letter to number",
rot = def &= help "rotate characters by n positions", rot = def &= help "rotate characters by n positions",
morse = def &= help "decode morse",
solve = def &= help "solve encoding" solve = def &= help "solve encoding"
} &= help "Decode chosen base" &=auto } &= help "Decode chosen base" &=auto
@ -157,7 +163,8 @@ encodeMode = Encode {
xx = def &= help "encode xx, without padding", xx = def &= help "encode xx, without padding",
yenc = def &= help "encode yEncode", yenc = def &= help "encode yEncode",
a1z26 = def &= help "encode letter to number", 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" } &= help "Encode chosen base"
main :: IO() main :: IO()

View File

@ -5,13 +5,14 @@ module Encoding.Morse
import Text.Morse as M import Text.Morse as M
import Data.ByteString (ByteString) import Data.ByteString (ByteString)
import Data.Char (isSpace)
import qualified Data.ByteString.Char8 as BC import qualified Data.ByteString.Char8 as BC
encmorse :: ByteString -> ByteString encmorse :: ByteString -> ByteString
encmorse input = BC.pack $ encodeValidMorseChars (BC.unpack input) encmorse input = BC.pack $ encodeValidMorseChars (BC.unpack input)
where where
encodeValidMorseChars :: String -> String encodeValidMorseChars :: String -> String
encodeValidMorseChars = concatMap (\c -> if canEncodeToMorse c then M.encodeMorse [c] else "") encodeValidMorseChars = concatMap (\c -> M.encodeMorse [c])
decmorse :: ByteString -> ByteString decmorse :: ByteString -> ByteString
decmorse input = case M.decodeMorse (BC.unpack input) of decmorse input = case M.decodeMorse (BC.unpack input) of