From 203723195e9db89086d510e388449f3a9e55c943 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Peter=20Tr=C5=A1ko?= Date: Sun, 6 Sep 2015 21:57:51 +0200 Subject: [PATCH] Implementation of lengthWordHex --- src/Data/Word/Length.hs | 11 +++++++++++ test/TestCase/Data/Word/Length.hs | 10 ++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/Data/Word/Length.hs b/src/Data/Word/Length.hs index c816486..db911cd 100644 --- a/src/Data/Word/Length.hs +++ b/src/Data/Word/Length.hs @@ -114,4 +114,15 @@ lengthWord64hex n -- Maximum is 18446744073709551615 = 0xffffffffffffffff. -- n >= 0x10000 {-# INLINE lengthWord64hex #-} +lengthWordHex :: Word -> Int +lengthWordHex n + | is64bit = lengthWord64hex (fromIntegral n) + | otherwise = lengthWord32hex (fromIntegral n) + where + is64bit = maxWord32 < maxWord + + maxWord32 = fromIntegral (maxBound :: Word32) :: Word64 + maxWord = fromIntegral (maxBound :: Word) :: Word64 +{-# INLINE lengthWordHex #-} + -- }}} Hexadecimal ------------------------------------------------------------ diff --git a/test/TestCase/Data/Word/Length.hs b/test/TestCase/Data/Word/Length.hs index 98fa29e..2502e56 100644 --- a/test/TestCase/Data/Word/Length.hs +++ b/test/TestCase/Data/Word/Length.hs @@ -38,7 +38,7 @@ import Data.Word.Length , lengthWord64hex , lengthWord8 , lengthWord8hex --- , lengthWordHex + , lengthWordHex ) @@ -80,9 +80,9 @@ tests = , testCase "lengthWord64hex maxBound" test_lengthWord64hex_maxBound , testProperty "lengthWord64hex = length . show" property_lengthWord64hex --- , testCase "lengthWordHex minBound" test_lengthWordHex_minBound --- , testCase "lengthWordHex maxBound" test_lengthWordHex_maxBound --- , testProperty "lengthWordHex = length . show" property_lengthWordHex + , testCase "lengthWordHex minBound" test_lengthWordHex_minBound + , testCase "lengthWordHex maxBound" test_lengthWordHex_maxBound + , testProperty "lengthWordHex = length . show" property_lengthWordHex ] numberLength :: Show n => n -> Int @@ -227,7 +227,6 @@ property_lengthWord64hex = lengthWord64hex <==> numberLengthHex -- }}} lengthWord64hex -------------------------------------------------------- -- {{{ lengthWordHex ---------------------------------------------------------- -{- test_lengthWordHex_minBound, test_lengthWordHex_maxBound :: Assertion test_lengthWordHex_minBound = @@ -238,7 +237,6 @@ test_lengthWordHex_maxBound = property_lengthWordHex :: Word -> Bool property_lengthWordHex = lengthWordHex <==> numberLengthHex --} -- }}} lengthWordHex ---------------------------------------------------------- -- }}} Hexadecimal ------------------------------------------------------------