0 | module Libraries.Utils.Octal
2 | import Data.Primitives.Views
6 | octDigit : Int -> Char
19 | asOct : Int -> String
20 | asOct n = pack $
asOct' n []
22 | asOct' : Int -> List Char -> List Char
24 | asOct' n oct with (n `divides` 8)
25 | asOct' (8 * div + rem) oct | DivBy div rem _ =
26 | assert_total $
asOct' div (octDigit rem :: oct)
29 | fromOctDigit : Char -> Maybe Int
30 | fromOctDigit '0' = Just 0
31 | fromOctDigit '1' = Just 1
32 | fromOctDigit '2' = Just 2
33 | fromOctDigit '3' = Just 3
34 | fromOctDigit '4' = Just 4
35 | fromOctDigit '5' = Just 5
36 | fromOctDigit '6' = Just 6
37 | fromOctDigit '7' = Just 7
38 | fromOctDigit _ = Nothing
41 | fromOctChars : List Char -> Maybe Integer
42 | fromOctChars = fromOctChars' 1
44 | fromOctChars' : Integer -> List Char -> Maybe Integer
45 | fromOctChars' _ [] = Just 0
46 | fromOctChars' m (d :: ds)
47 | = do digit <- fromOctDigit (toLower d)
48 | digits <- fromOctChars' (m*8) ds
49 | pure $
cast digit * m + digits
52 | fromOct : String -> Maybe Integer
53 | fromOct = fromOctChars . unpack