module Language.Fortran.Repr.Tmp where

import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Lazy as B
import Data.Word
import qualified Data.Text as Text
import Data.Text ( Text )
import qualified Data.Char as Char
import qualified Data.List as List

testF :: Float -> Text
testF :: Float -> Text
testF = (ByteString -> [Word8]) -> ByteString -> Text
forall a. (a -> [Word8]) -> a -> Text
prettyHexByteString ByteString -> [Word8]
B.unpack (ByteString -> Text) -> (Float -> ByteString) -> Float -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
B.toLazyByteString (Builder -> ByteString)
-> (Float -> Builder) -> Float -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Builder
B.floatBE

testD :: Double -> Text
testD :: Double -> Text
testD = (ByteString -> [Word8]) -> ByteString -> Text
forall a. (a -> [Word8]) -> a -> Text
prettyHexByteString ByteString -> [Word8]
B.unpack (ByteString -> Text) -> (Double -> ByteString) -> Double -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
B.toLazyByteString (Builder -> ByteString)
-> (Double -> Builder) -> Double -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Double -> Builder
B.doubleBE


-- | Pretty print to default format @00 12 AB FF@: space between each byte, all
--   caps.
--
-- This format I consider most human readable. I prefer caps to draw attention
-- to this being data instead of text (you don't see that many capital letters
-- packed together in prose).
prettyHexByteString :: (a -> [Word8]) -> a -> Text
prettyHexByteString :: forall a. (a -> [Word8]) -> a -> Text
prettyHexByteString a -> [Word8]
unpack =
      [Text] -> Text
Text.concat
    ([Text] -> Text) -> (a -> [Text]) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
List.intersperse (Char -> Text
Text.singleton Char
' ')
    ([Text] -> [Text]) -> (a -> [Text]) -> a -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Text) -> [Word8] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char, Char) -> Text
f ((Char, Char) -> Text) -> (Word8 -> (Char, Char)) -> Word8 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> Word8 -> (Char, Char)
prettyHexByte Char -> Char
Char.toUpper)
    ([Word8] -> [Text]) -> (a -> [Word8]) -> a -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Word8]
unpack
  where
    f :: (Char, Char) -> Text
    f :: (Char, Char) -> Text
f (Char
c1, Char
c2) = Char -> Text -> Text
Text.cons Char
c1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Char -> Text
Text.singleton Char
c2

prettyHexByte :: (Char -> Char) -> Word8 -> (Char, Char)
prettyHexByte :: (Char -> Char) -> Word8 -> (Char, Char)
prettyHexByte Char -> Char
f Word8
w = (Int -> Char
prettyNibble Int
h, Int -> Char
prettyNibble Int
l)
  where
    (Int
h,Int
l) = Word8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
0x10
    prettyNibble :: Int -> Char
prettyNibble = Char -> Char
f (Char -> Char) -> (Int -> Char) -> Int -> Char
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int -> Char
Char.intToDigit -- Char.intToDigit returns lower case

-- | Pretty print to "compact" format @0012abff@ (often output by hashers).
prettyHexByteStringCompact :: (a -> [Word8]) -> a -> Text
prettyHexByteStringCompact :: forall a. (a -> [Word8]) -> a -> Text
prettyHexByteStringCompact a -> [Word8]
unpack =
    [Text] -> Text
Text.concat ([Text] -> Text) -> (a -> [Text]) -> a -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Text) -> [Word8] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char, Char) -> Text
f ((Char, Char) -> Text) -> (Word8 -> (Char, Char)) -> Word8 -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> Word8 -> (Char, Char)
prettyHexByte Char -> Char
forall a. a -> a
id) ([Word8] -> [Text]) -> (a -> [Word8]) -> a -> [Text]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [Word8]
unpack
  where
    f :: (Char, Char) -> Text
    f :: (Char, Char) -> Text
f (Char
c1, Char
c2) = Char -> Text -> Text
Text.cons Char
c1 (Text -> Text) -> Text -> Text
forall a b. (a -> b) -> a -> b
$ Char -> Text
Text.singleton Char
c2