From fd6b5dfd169da5b8e005b6aaed61676a37fbef01 Mon Sep 17 00:00:00 2001 From: Stefan Friese Date: Sun, 12 May 2024 23:16:21 +0200 Subject: [PATCH] added errorhandling for base62 encoding --- Main.hs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Main.hs b/Main.hs index 02f8622..d6396ca 100644 --- a/Main.hs +++ b/Main.hs @@ -213,14 +213,23 @@ dec62 input = -- maybe "Error decoding Base62.\n" show (B62.decode128 (Bytes.fromByteString (BSU.fromString input))) stringToInt :: String -> Maybe Integer -stringToInt str = readMaybe str +stringToInt = readMaybe -enc62 :: String -> String +-- enc62 :: String -> String -- 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 +-- working correct: +-- enc62 :: String -> String +-- enc62 = T.unpack . T.decodeUtf8 . BSU.fromString . Latin1.toString . Bytes.fromByteArray . B62.encode64 . fromIntegral . fromJust . stringToInt + +enc62 :: String -> String +enc62 input = + let intValue = fromMaybe (error "Error: Unable to convert input string to integer") (stringToInt input) + encoded = B62.encode64 (fromIntegral intValue) + encodedText = T.decodeUtf8 (BSU.fromString (Latin1.toString (Bytes.fromByteArray encoded))) + in T.unpack encodedText -- enc62 :: Text -> Text -- enc62 text = B62.text128 (fromIntegral (T.length text))