added error handling to base91, still reworking b62

This commit is contained in:
Stefan Friese 2024-05-09 00:21:53 +02:00
parent 87ccc20b85
commit 6972d08ff7
2 changed files with 28 additions and 4 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
dist-newstyle/** dist-newstyle/**
based.link based.link
.Main*

31
Main.hs
View File

@ -5,7 +5,10 @@ module Main where
import System.Console.CmdArgs import System.Console.CmdArgs
import Control.Arrow import Control.Arrow
import Data.Text (pack, unpack) import Data.Text (pack, unpack)
import Text.Read (readMaybe)
import qualified Data.Text as T 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 Data.Text (Text)
import qualified Data.Text.Lazy as TL import qualified Data.Text.Lazy as TL
import Data.Text.Lazy.Builder (toLazyText) 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 -- without the show func, sequences like \n will not be shown as characters but will be executed as newline
dec91 :: String -> String 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 :: String -> String
-- dec91 input = -- dec91 input =
@ -191,12 +197,29 @@ encurl :: String -> String
encurl = HB.urlEncode encurl = HB.urlEncode
dec62 :: String -> String 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 = show . fromJust . B62.decode128 . (Bytes.fromByteString . BSU.fromString)
dec62 input = 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 :: 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 -> Text
-- enc62 text = B62.text128 (fromIntegral (T.length 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 :: String -> String
-- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack -- dec58 = C.unpack . fromJust . B58.decodeBase58 . pack
dec58 input = 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 :: String -> String
enc58 = unpack . B58.encodeBase58 . BSU.fromString enc58 = unpack . B58.encodeBase58 . BSU.fromString