Add a Read instance for Data.Fixed.Fixed
authorIan Lynagh <igloo@earth.li>
Tue, 16 Nov 2010 21:19:10 +0000 (21:19 +0000)
committerIan Lynagh <igloo@earth.li>
Tue, 16 Nov 2010 21:19:10 +0000 (21:19 +0000)
Data/Fixed.hs

index bb76e01..98d212d 100644 (file)
@@ -35,6 +35,8 @@ module Data.Fixed
 ) where
 
 import Prelude -- necessary to get dependencies right
+import Data.Char
+import Data.List
 #ifndef __NHC__
 import Data.Typeable
 import Data.Data
@@ -152,9 +154,30 @@ showFixed chopTrailingZeros fa@(MkFixed a) = (show i) ++ (withDot (showIntegerZe
     maxnum = 10 ^ digits
     fracNum = div (d * maxnum) res
 
+readsFixed :: (HasResolution a) => ReadS (Fixed a)
+readsFixed = readsSigned
+    where readsSigned ('-' : xs) = [ (negate x, rest)
+                                   | (x, rest) <- readsUnsigned xs ]
+          readsSigned xs = readsUnsigned xs
+          readsUnsigned xs = case span isDigit xs of
+                             ([], _) -> []
+                             (is, xs') ->
+                                 let i = fromInteger (read is)
+                                 in case xs' of
+                                    '.' : xs'' ->
+                                        case span isDigit xs'' of
+                                        ([], _) -> []
+                                        (js, xs''') ->
+                                            let j = fromInteger (read js)
+                                                l = genericLength js :: Integer
+                                            in [(i + (j / (10 ^ l)), xs''')]
+                                    _ -> [(i, xs')]
+
 instance (HasResolution a) => Show (Fixed a) where
     show = showFixed False
 
+instance (HasResolution a) => Read (Fixed a) where
+    readsPrec _ = readsFixed
 
 data E0 = E0
 #ifndef __NHC__