diff options
author | Will Thompson <will@willthompson.co.uk> | 2012-01-17 12:15:17 +0000 |
---|---|---|
committer | Will Thompson <will@willthompson.co.uk> | 2012-01-17 12:15:17 +0000 |
commit | dd17752871a9c469844caf1d657ac0403cdec255 (patch) | |
tree | d3258683a5d8b503670662fc612d7d03643b6f59 | |
parent | 249cbe99658860512e872cf748b59b591183df65 (diff) |
Format ASCII-printable arrays of bytes as Strings
-rw-r--r-- | Bustle/VariantFormatter.hs | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/Bustle/VariantFormatter.hs b/Bustle/VariantFormatter.hs index 060a30b..d4aa489 100644 --- a/Bustle/VariantFormatter.hs +++ b/Bustle/VariantFormatter.hs @@ -25,6 +25,7 @@ where import Data.Word import Data.Int import Data.List (intercalate) +import Data.Char (chr, isPrint) -- :'( import Data.Maybe (fromJust) import qualified Data.Text.Lazy as Text @@ -37,6 +38,15 @@ format_Bool = show format_Word8 :: Word8 -> String format_Word8 = show +format_ByteArray :: Array -> String +format_ByteArray ay = + if all (\y -> isPrint (chr (fromIntegral y))) bytes + then show (map (chr . fromIntegral) bytes :: String) + else format_Array ay + where + bytes = map (fromJust . fromVariant) (arrayItems ay) :: [Word8] + + format_Int16 :: Int16 -> String format_Int16 = show format_Int32 :: Int32 -> String @@ -129,6 +139,7 @@ format_Variant style v = TypeSignature -> format_Signature . fromJust . fromVariant TypeObjectPath -> format_ObjectPath . fromJust . fromVariant TypeVariant -> format_Variant VariantStyleAngleBrackets . fromJust . fromVariant + TypeArray TypeWord8 -> format_ByteArray . fromJust . fromVariant TypeArray _ -> format_Array . fromJust . fromVariant TypeDictionary _ _ -> format_Dictionary . fromJust . fromVariant TypeStructure _ -> format_Structure . fromJust . fromVariant |