summaryrefslogtreecommitdiff
path: root/libraries/base/Text
diff options
context:
space:
mode:
authorIan Lynagh <ian@well-typed.com>2013-01-02 23:18:18 +0000
committerIan Lynagh <ian@well-typed.com>2013-01-03 00:12:25 +0000
commit5f19f951d803a8c8485b59c557d99f9855084066 (patch)
treedc6e240d15a0b9750a3dbdf1cf6e4a6d7bb46d0e /libraries/base/Text
parentf6ee55a831c654ab2a6ca6fe7ac73797e1489381 (diff)
downloadhaskell-5f19f951d803a8c8485b59c557d99f9855084066.tar.gz
Fix Data.Fixed.Fixed's Read instance; fixes #7483
Diffstat (limited to 'libraries/base/Text')
-rw-r--r--libraries/base/Text/Read/Lex.hs18
1 files changed, 17 insertions, 1 deletions
diff --git a/libraries/base/Text/Read/Lex.hs b/libraries/base/Text/Read/Lex.hs
index 8a64e21b80..c1592c6c5c 100644
--- a/libraries/base/Text/Read/Lex.hs
+++ b/libraries/base/Text/Read/Lex.hs
@@ -19,7 +19,7 @@ module Text.Read.Lex
-- lexing types
( Lexeme(..)
- , numberToInteger, numberToRational, numberToRangedRational
+ , numberToInteger, numberToFixed, numberToRational, numberToRangedRational
-- lexer
, lex, expect
@@ -82,6 +82,22 @@ numberToInteger (MkNumber base iPart) = Just (val (fromIntegral base) 0 iPart)
numberToInteger (MkDecimal iPart Nothing Nothing) = Just (val 10 0 iPart)
numberToInteger _ = Nothing
+numberToFixed :: Integer -> Number -> Maybe (Integer, Integer)
+numberToFixed _ (MkNumber base iPart) = Just (val (fromIntegral base) 0 iPart, 0)
+numberToFixed _ (MkDecimal iPart Nothing Nothing) = Just (val 10 0 iPart, 0)
+numberToFixed p (MkDecimal iPart (Just fPart) Nothing)
+ = let i = val 10 0 iPart
+ f = val 10 0 (integerTake p (fPart ++ repeat 0))
+ -- Sigh, we really want genericTake, but that's above us in
+ -- the hierarchy, so we define our own version here (actually
+ -- specialised to Integer)
+ integerTake :: Integer -> [a] -> [a]
+ integerTake n _ | n <= 0 = []
+ integerTake _ [] = []
+ integerTake n (x:xs) = x : integerTake (n-1) xs
+ in Just (i, f)
+numberToFixed _ _ = Nothing
+
-- This takes a floatRange, and if the Rational would be outside of
-- the floatRange then it may return Nothing. Not that it will not
-- /necessarily/ return Nothing, but it is good enough to fix the