reworked encode octal, decode octal has an issues with missing leading zeros in an octal value
This commit is contained in:
parent
a5c73c27f6
commit
dec7ccc3c5
84
Main.hs
84
Main.hs
|
@ -48,7 +48,7 @@ import qualified Data.ByteString.Char8 as C
|
|||
-- Regex imports
|
||||
import Text.Regex.TDFA
|
||||
import Data.Word (Word8)
|
||||
import Data.Char (ord, chr, intToDigit)
|
||||
import Data.Char (ord, chr, intToDigit, digitToInt)
|
||||
|
||||
data Based = Decode {
|
||||
b91 :: Bool,
|
||||
|
@ -269,29 +269,93 @@ enc10 :: String -> String
|
|||
-- enc10 = C.unpack . BSU.fromString . intToHexString . decimalStringToInt -- Depending on what you want, do enc10 = show . map ord
|
||||
enc10 str = C.unpack $ C.pack $ Prelude.foldl (\acc char -> acc ++ show (ord char)) "" str
|
||||
|
||||
-- decode octal
|
||||
octalToChar :: String -> Char
|
||||
octalToChar str = chr $ Prelude.foldl (\acc c -> acc * 8 + read [c]) 0 str
|
||||
|
||||
chunksOf :: Int -> [a] -> [[a]]
|
||||
chunksOf _ [] = []
|
||||
chunksOf n xs = Prelude.take n xs : chunksOf n (Prelude.drop n xs)
|
||||
|
||||
decodeOctal :: String -> String
|
||||
decodeOctal = map octalToChar . words
|
||||
|
||||
-- decodeOctal :: String -> String
|
||||
-- decodeOctal = map (octalToChar . padOctal) . words
|
||||
-- where
|
||||
-- -- Function to pad an octal number string with leading '0's if needed
|
||||
-- padOctal str
|
||||
-- | Prelude.length str == 1 && str /= "0" = "00" ++ str
|
||||
-- | Prelude.length str == 2 && str /= "0" = "0" ++ str
|
||||
-- | otherwise = str
|
||||
|
||||
-- Function to decode a string of octal numbers to characters
|
||||
dec8 :: String -> String
|
||||
dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt
|
||||
dec8 = map octalToChar . chunksOf 3 . filter (/= ' ')
|
||||
|
||||
-- dec8 :: String -> String
|
||||
-- dec8 = C.unpack . encodeUtf8 . toText . showbOct . hexStringToInt
|
||||
|
||||
-- newtype Octal = Octal Int
|
||||
-- octalToString :: Octal -> String
|
||||
-- octalToString (Octal n) = show n
|
||||
|
||||
-- dec8 = map (chr . octalToDecimal) . chunksOf 3
|
||||
-- where
|
||||
-- octalToDecimal :: Octal -> Int
|
||||
-- octalToDecimal (Octal n) = Prelude.foldl (\acc c -> acc * 8 + digitToInt c) O (show n)
|
||||
|
||||
-- chunksOf :: Int -> [a] -> [[a]]
|
||||
-- chunksOf _ [] = []
|
||||
-- chunksOf n xs = Prelude.take n xs : chunksOf n (Prelude.drop n xs)
|
||||
|
||||
-- enc8 :: String -> String
|
||||
-- enc8 = C.unpack . BSU.fromString . intToHexString . octToInt . (reverse . (map fromJust . (map fromOctDigit)))
|
||||
--
|
||||
unicodeToOctal :: Char -> String
|
||||
unicodeToOctal c = reverse $ padTo3Bits $ decimalToOctal' (ord c)
|
||||
|
||||
|
||||
-- unicodeToOctal :: Char -> String
|
||||
-- unicodeToOctal c
|
||||
-- | ord c >= 0 && ord c <= 7 = ['0', intToDigit (ord c)]
|
||||
-- | otherwise = padTo3Bits $ decimalToOctal' (ord c)
|
||||
-- where
|
||||
-- decimalToOctal' 0 = ""
|
||||
-- decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : decimalToOctal' q
|
||||
|
||||
-- padTo3Bits :: String -> String
|
||||
-- padTo3Bits bits
|
||||
-- | Prelude.length bits < 3 = replicate (3 - Prelude.length bits) '0' ++ bits
|
||||
-- | otherwise = bits
|
||||
|
||||
-- enc8 :: String -> String
|
||||
-- enc8 = unwords . map (concatMap unicodeToOctal . (:[]))
|
||||
|
||||
unicodeToOctal :: Char -> [String]
|
||||
unicodeToOctal c = chunksOf 3 $ reverse $ decimalToOctal' (ord c)
|
||||
where
|
||||
decimalToOctal' 0 = "0"
|
||||
decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : decimalToOctal' q
|
||||
|
||||
padTo3Bits :: String -> String
|
||||
padTo3Bits bits = replicate (3 - Prelude.length bits) '0' ++ bits
|
||||
decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : if q == 0 then "" else decimalToOctal' q
|
||||
|
||||
enc8 :: String -> String
|
||||
enc8 = concatMap unicodeToOctal
|
||||
enc8 = unwords . concatMap unicodeToOctal
|
||||
|
||||
-- unicodeToOctal :: Char -> String
|
||||
-- unicodeToOctal c = reverse $ padTo3Bits $ decimalToOctal' (ord c)
|
||||
-- where
|
||||
-- decimalToOctal' 0 = "0"
|
||||
-- decimalToOctal' m = let (q, r) = m `divMod` 8 in intToDigit r : decimalToOctal' q
|
||||
|
||||
-- padTo3Bits :: String -> String
|
||||
-- padTo3Bits bits = replicate (3 - Prelude.length bits) '0' ++ bits
|
||||
|
||||
-- enc8 :: String -> String
|
||||
-- enc8 = concatMap unicodeToOctal
|
||||
|
||||
dec2 :: String -> String
|
||||
-- dec2 = C.unpack . encodeUtf8 . toText . showbBin . hexStringToInt
|
||||
dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt
|
||||
-- dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt . enc16
|
||||
dec2 = TL.unpack . toLazyText . showbBin . hexStringToInt
|
||||
|
||||
|
||||
enc2 :: String -> String
|
||||
-- enc2 = C.unpack . BSU.fromString . intToHexString . binToInt . (reverse . (map fromJust . (map fromBinDigit)))
|
||||
|
|
Loading…
Reference in New Issue