Skip to content

Commit 78c45cc

Browse files
authored
Merge pull request #3 from ajdawson/common-helpers
Use helper functions for converting to words
2 parents 8c27a51 + 670579f commit 78c45cc

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

src/Truncate.hs

+4-8
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,11 @@ module Truncate
4343

4444

4545
------------------------------------------------------------------------
46-
import Data.Char (digitToInt)
4746
import Data.Binary.IEEE754 ( wordToFloat
4847
, floatToWord
4948
, wordToDouble
5049
, doubleToWord
5150
)
52-
import Numeric ( readInt
53-
, readHex
54-
)
5551
import Text.Read (readMaybe)
5652

5753

@@ -95,8 +91,8 @@ data Binary = Bin32 String
9591
| Bin64 String
9692

9793
instance Truncatable Binary where
98-
makeBits (Bin32 b) = WordF32 $ fst (head (readInt 2 (`elem` "01") digitToInt b))
99-
makeBits (Bin64 b) = WordF64 $ fst (head (readInt 2 (`elem` "01") digitToInt b))
94+
makeBits (Bin32 b) = WordF32 $ binToWord b
95+
makeBits (Bin64 b) = WordF64 $ binToWord b
10096
fromBits (WordF32 b) = Bin32 (wordToBin b)
10197
fromBits (WordF64 b) = Bin64 (wordToBin b)
10298

@@ -171,8 +167,8 @@ data Hexadecimal = Hex32 String
171167
| Hex64 String
172168

173169
instance Truncatable Hexadecimal where
174-
makeBits (Hex32 s) = WordF32 $ fst (head (readHex s))
175-
makeBits (Hex64 s) = WordF64 $ fst (head (readHex s))
170+
makeBits (Hex32 s) = WordF32 $ hexToWord s
171+
makeBits (Hex64 s) = WordF64 $ hexToWord s
176172
fromBits (WordF32 b) = Hex32 (wordToHex b)
177173
fromBits (WordF64 b) = Hex64 (wordToHex b)
178174

src/Truncate/Internal.hs

+15-4
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,20 @@ module Truncate.Internal where
2929
------------------------------------------------------------------------
3030
import Data.Bits
3131
import Data.Word
32-
import Data.Char (intToDigit)
33-
import Numeric (showIntAtBase)
32+
import Data.Char ( digitToInt
33+
, intToDigit
34+
)
35+
import Numeric ( readInt
36+
, readHex
37+
, showIntAtBase
38+
)
3439

3540

3641
------------------------------------------------------------------------
3742
-- | A wrapper around 'Word32' and 'Word64' types
3843
data WordF = WordF32 Word32
39-
| WordF64 Word64
40-
deriving (Eq, Show)
44+
| WordF64 Word64
45+
deriving (Eq, Show)
4146

4247

4348
------------------------------------------------------------------------
@@ -69,6 +74,9 @@ dropBits n = (flip shiftL) n . (flip shiftR) n
6974
-----------------------------------------------------------------------
7075
-- Functions for handling binary representations
7176

77+
binToWord :: Num a => String -> a
78+
binToWord b = fst $ head $ readInt 2 (`elem` "01") digitToInt b
79+
7280
wordToBin :: (Show a, Integral a) => a -> String
7381
wordToBin b = showIntAtBase 2 intToDigit b ""
7482

@@ -78,6 +86,9 @@ validBin = all (`elem` "01")
7886
-----------------------------------------------------------------------
7987
-- Functions for handling hexadecimal representations
8088

89+
hexToWord :: (Num a, Eq a) => String -> a
90+
hexToWord s = fst (head (readHex s))
91+
8192
wordToHex :: (Show a, Integral a) => a -> String
8293
wordToHex b = showIntAtBase 16 intToDigit b ""
8394

0 commit comments

Comments
 (0)