From 6972d08ff784311d96c27cf4aa589953753a31ba Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Thu, 9 May 2024 00:21:53 +0200 Subject: [PATCH] added error handling to base91, still reworking b62 --- .gitignore | 1 + Main.hs | 31 +++++++++++++++++++++++++++---- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index ae567a4..92f0ccc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ dist-newstyle/** based.link +.Main* diff --git a/Main.hs b/Main.hs index 67bf711..02f8622 100644 --- a/Main.hs +++ b/Main.hs @@ -5,7 +5,10 @@ module Main where import System.Console.CmdArgs import Control.Arrow import Data.Text (pack, unpack) +import Text.Read (readMaybe) import qualified Data.Text as T +import qualified Data.Text.Encoding as T +import qualified Data.Text.IO as T import Data.Text (Text) import qualified Data.Text.Lazy as TL import Data.Text.Lazy.Builder (toLazyText) @@ -127,7 +130,10 @@ octToInt (x : xs) = x + 8 * octToInt xs -- without the show func, sequences like \n will not be shown as characters but will be executed as newline dec91 :: String -> String -dec91 = C.unpack . B91.decode +dec91 = T.unpack . T.decodeUtf8 . B91.decode + +-- dec91 input = +-- maybe "Error decoding Base91.\n" T.unpack (T.decodeUtf8 (B91.decode input) -- dec91 :: String -> String -- dec91 input = @@ -191,12 +197,29 @@ encurl :: String -> String encurl = HB.urlEncode dec62 :: String -> String +-- dec62 = T.unpack . B62.text128 . fromJust . B62.decode128 . Bytes.fromAsciiString +-- dec62 input = +-- case B62.decode128 (Bytes.fromAsciiString input) of +-- Just decoded -> (T.unpack (B62.text128 decoded)) +-- -- Just decoded -> C.unpack decoded +-- Nothing -> "Error decoding Base62.\n" + -- dec62 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString) dec62 input = - maybe "Error decoding Base62.\n" show (B62.decode128 (Bytes.fromByteString (BSU.fromString input))) + let decoded = B62.decode128 (Bytes.fromByteString (BSU.fromString input)) + in fromMaybe "Error decoding Base62.\n" (show <$> decoded) + +-- dec62 input = +-- maybe "Error decoding Base62.\n" show (B62.decode128 (Bytes.fromByteString (BSU.fromString input))) + +stringToInt :: String -> Maybe Integer +stringToInt str = readMaybe str enc62 :: String -> String -enc62 = C.unpack . BSU.fromString. Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt))) +-- enc62 = C.unpack . BSU.fromString. Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt))) +-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . (Bytes.fromByteArray . ( B62.encode64 . (intToWord64 . decimalStringToInt))) +-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . (Bytes.fromByteArray . (B62.encode128 . (fromIntegral . ( BSU.fromString . fromJust StringToInt)))) +enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . Bytes.fromByteArray . B62.encode64 . fromIntegral . fromJust . stringToInt -- enc62 :: Text -> Text -- enc62 text = B62.text128 (fromIntegral (T.length text)) @@ -204,7 +227,7 @@ enc62 = C.unpack . BSU.fromString. Latin1.toString . (Bytes.fromByteArray . ( B6 dec58 :: String -> String -- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack dec58 input = - maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (pack input)) + maybe "Error decoding Base58.\n" C.unpack (B58.decodeBase58 (T.pack input)) enc58 :: String -> String enc58 = unpack . B58.encodeBase58 . BSU.fromString