Skip to content

Instantly share code, notes, and snippets.

@ObjectBoxPC
Created May 31, 2021 09:50
Show Gist options
  • Save ObjectBoxPC/4a554a15d8d2b13168e25d65b97e657a to your computer and use it in GitHub Desktop.
Save ObjectBoxPC/4a554a15d8d2b13168e25d65b97e657a to your computer and use it in GitHub Desktop.
Programs to transform text strings
import StrConv
fullWidth c
| c >= 0x21 && c <= 0x7E = Just $ c + 0xFEE0 -- ASCII printable
| c == 0x20 = Just 0x3000 -- ASCII space -> ideographic space
| otherwise = Nothing
main = strConv (charify fullWidth)
import StrConv
regional c
| c >= 0x41 && c <= 0x5A = Just $ c + 0x1F1A5 -- Uppercase
| c >= 0x61 && c <= 0x7A = Just $ c + 0x1F185 -- Lowercase
| otherwise = Nothing
main = strConv (charify regional)
module StrConv (strConv, charify) where
import System.Environment
import System.Exit
import Data.Char
convertStr convertChar = map $ \c -> case convertChar c of
Just converted -> converted
_ -> c
processArgs args
| length args == 0 = putStrLn "Not enough arguments" >> return Nothing
| length args == 1 = return (Just $ args !! 0)
| otherwise =
putStrLn "Warning: Only the first argument will be converted"
>> return (Just $ args !! 0)
writeArg convertChar argResult = case argResult of
Just arg -> writeConverted >> exitSuccess
where writeConverted = putStrLn $ convertStr convertChar arg
Nothing -> exitFailure
strConv :: (Char -> Maybe Char) -> IO ()
strConv convertChar = getArgs >>= processArgs >>= writeArg convertChar
charify :: (Int -> Maybe Int) -> Char -> Maybe Char
charify convert = (fmap chr) . convert . ord
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment