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 = forall a. (a -> [Word8]) -> a -> Text
prettyHexByteString ByteString -> [Word8]
B.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
B.toLazyByteString forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Builder
B.floatBE

testD :: Double -> Text
testD :: Double -> Text
testD = forall a. (a -> [Word8]) -> a -> Text
prettyHexByteString ByteString -> [Word8]
B.unpack forall b c a. (b -> c) -> (a -> b) -> a -> c
. Builder -> ByteString
B.toLazyByteString 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
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> [a] -> [a]
List.intersperse (Char -> Text
Text.singleton Char
' ')
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char, Char) -> Text
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> Word8 -> (Char, Char)
prettyHexByte Char -> Char
Char.toUpper)
    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 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) = forall a b. (Integral a, Num b) => a -> b
fromIntegral Word8
w forall a. Integral a => a -> a -> (a, a)
`divMod` Int
0x10
    prettyNibble :: Int -> Char
prettyNibble = Char -> Char
f 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 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Char, Char) -> Text
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Char -> Char) -> Word8 -> (Char, Char)
prettyHexByte forall a. a -> a
id) 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 forall a b. (a -> b) -> a -> b
$ Char -> Text
Text.singleton Char
c2