[Pkg-haskell-commits] [SCM] haskell-testpack branch, master, updated. debian/1.0.2-1-4-gb0d6b36

John Goerzen jgoerzen at complete.org
Fri Apr 23 15:22:57 UTC 2010


The following commit has been merged in the master branch:
commit cc80fd97da7cd1573b20291639b19680c8f20608
Author: John Goerzen <jgoerzen at complete.org>
Date:   Tue Apr 15 01:06:06 2008 -0500

    Finishing up parseNumInt

diff --git a/src/Data/Quantity.hs b/src/Data/Quantity.hs
index 026fc7d..451b992 100644
--- a/src/Data/Quantity.hs
+++ b/src/Data/Quantity.hs
@@ -32,6 +32,8 @@ Written by John Goerzen, jgoerzen\@complete.org -}
 module Data.Quantity (
                           renderNum,
                           renderNums,
+                          parseNum,
+                          parseNumInt,
                           quantifyNum,
                           quantifyNums,
                           SizeOpts(..),
@@ -166,15 +168,50 @@ renderNums opts prec numbers =
           (convnums, suffix) = 
               (quantifyNums opts numbers)::([Double], Char)
 
-parseNum :: Real a => SizeOpts -> String -> Either String a
-parseNum opts inp =
+{- | Parses a String, possibly generated by 'renderNum'.  Parses the suffix
+and applies it to the number, which is read via the Read class.
+
+Returns Left "error message" on error, or Right number on successful parse.
+
+If you want an Integral result, the convenience function 'parseNumInt' is for
+you.
+-}
+parseNum :: (Read a, Fractional a) => 
+            SizeOpts            -- ^ Information on how to parse this data
+         -> Bool                -- ^ Whether to perform a case-insensitive match
+         -> String              -- ^ The string to parse
+         -> Either String a
+parseNum opts insensitive inp =
     case reads inp of
       [] -> Left "Couldn't parse numeric component of input"
       [(num, "")] -> Right num  -- No suffix; pass number unhindered
-      [(num, suffix)] ->
+      [(num, [suffix])] ->
           case lookup suffix suffixMap of
             Nothing -> Left $ "Unrecognized suffix " ++ show suffix
-            Just power -> Right $ num * ((base opts) ** power)
+            Just power -> Right $ num * multiplier power
+      [(_, suffix)] -> Left $ "Multi-character suffix " ++ show suffix
       _ -> Left "Multiple parses for input"
     where suffixMap = zip (suffixes opts) 
-                          (iterate (+ (powerIncr opts)) (firstPower opts))
\ No newline at end of file
+                          (iterate (+ (powerIncr opts)) (firstPower opts))
+          multiplier :: (Read a, Fractional a) => Int -> a
+          multiplier power =
+              fromRational . toRational $ 
+                           fromIntegral (base opts) ** fromIntegral power
+{- | Parse a number as with 'parseNum', but return the result as
+an 'Integral'.  Any type such as Integer, Int, etc. can be used for the
+result type.
+
+This function simply calls 'round' on the result of 'parseNum'.  A
+'Double' is used internally for the parsing of the numeric component.
+
+By using this function, a user can still say something like 1.5M and get an
+integral result. -}
+parseNumInt :: (Read a, Integral a) => 
+               SizeOpts         -- ^ Information on how to parse this data
+            -> Bool             -- ^ Whether to perform a case-insensitive match
+            -> String           -- ^ The string to parse
+            -> Either String a
+parseNumInt opts insensitive inp =
+    case (parseNum opts insensitive inp)::Either String Double of
+      Left x -> Left x
+      Right n -> Right (round n)
\ No newline at end of file

-- 
haskell-testpack



More information about the Pkg-haskell-commits mailing list