git.iko.soy/typeable/attoparsec
on master help sign in
Merge
commitd859eeaa136d202532e67e105141f8adbdf29ddb authorBryan O'Sullivan <bos@serpentine.com> dateFri 16 May 2014, 21:26 (12 years ago) parent7600fe9ce97ed1
3 files changed+38−32
Data/Attoparsec/ByteString/Char8.hs+19−16 Data/Attoparsec/Text.hs+18−15 attoparsec.cabal+1−1
Data/Attoparsec/ByteString/Char8.hs+19−16
@@ -133,8 +133,8 @@ import Data.Bits (Bits, (.|.), shiftL)
133133import Data.ByteString.Internal (c2w, w2c)
134134import Data.Int (Int8, Int16, Int32, Int64)
135135import Data.String (IsString(..))
136−import Data.Scientific (Scientific, coefficient, base10Exponent)
137−import qualified Data.Scientific as Sci (scientific)
136+import Data.Scientific (Scientific)
137+import qualified Data.Scientific as Sci
138138import Data.Word (Word8, Word16, Word32, Word64, Word)
139139import Prelude hiding (takeWhile)
140140import qualified Data.Attoparsec.ByteString as A
@@ -524,7 +524,7 @@ rational = scientifically realToFrac
524524-- This function does not accept string representations of \"NaN\" or
525525-- \"Infinity\".
526526double :: Parser Double
527−double = rational
527+double = scientifically Sci.toRealFloat
528528
529529-- | Parse a number, attempting to preserve both speed and precision.
530530--
@@ -539,11 +539,11 @@ double = rational
539539-- \"
540540number :: Parser Number
541541number = scientifically $ \s ->
542− let e = base10Exponent s
543− c = coefficient s
542+ let e = Sci.base10Exponent s
543+ c = Sci.coefficient s
544544 in if e >= 0
545545 then I (c * 10 ^ e)
546− else D (fromInteger c / 10 ^ negate e)
546+ else D (Sci.toRealFloat s)
547547
548548-- | Parse a scientific number.
549549--
@@ -551,6 +551,9 @@ number = scientifically $ \s ->
551551scientific :: Parser Scientific
552552scientific = scientifically id
553553
554+-- A strict pair
555+data SP = SP !Integer {-# UNPACK #-}!Int
556+
554557{-# INLINE scientifically #-}
555558scientifically :: (Scientific -> a) -> Parser a
556559scientifically h = do
@@ -563,21 +566,21 @@ scientifically h = do
563566
564567 n <- decimal
565568
566− let f fracDigits = Sci.scientific (B8.foldl' step n fracDigits)
567− (negate $ B8.length fracDigits)
569+ let f fracDigits = SP (B8.foldl' step n fracDigits)
570+ (negate $ B8.length fracDigits)
568571 step a w = a * 10 + fromIntegral (w - 48)
569572
570573 dotty <- I.peekWord8
571574 -- '.' -> ascii 46
572− s <- case dotty of
573− Just 46 -> I.anyWord8 *> (f <$> I.takeWhile isDigit_w8)
574− _ -> pure (Sci.scientific n 0)
575+ SP c e <- case dotty of
576+ Just 46 -> I.anyWord8 *> (f <$> I.takeWhile isDigit_w8)
577+ _ -> pure (SP n 0)
575578
576− let !signedCoeff | positive = coefficient s
577− | otherwise = negate $ coefficient s
579+ let !signedCoeff | positive = c
580+ | otherwise = -c
578581
579582 let littleE = 101
580583 bigE = 69
581− (I.satisfy (\c -> c == littleE || c == bigE) *>
582− fmap (h . Sci.scientific signedCoeff . (base10Exponent s +)) (signed decimal)) <|>
583− return (h $ Sci.scientific signedCoeff (base10Exponent s))
584+ (I.satisfy (\ex -> ex == littleE || ex == bigE) *>
585+ fmap (h . Sci.scientific signedCoeff . (e +)) (signed decimal)) <|>
586+ return (h $ Sci.scientific signedCoeff e)
Data/Attoparsec/Text.hs+18−15
@@ -124,8 +124,8 @@ module Data.Attoparsec.Text
124124import Control.Applicative (pure, (<$>), (*>), (<*), (<|>))
125125import Data.Attoparsec.Combinator
126126import Data.Attoparsec.Number (Number(..))
127−import Data.Scientific (Scientific, coefficient, base10Exponent)
128−import qualified Data.Scientific as Sci (scientific)
127+import Data.Scientific (Scientific)
128+import qualified Data.Scientific as Sci
129129import Data.Attoparsec.Text.Internal (Parser, Result, parse, takeWhile1)
130130import Data.Bits (Bits, (.|.), shiftL)
131131import Data.Char (isAlpha, isDigit, isSpace, ord)
@@ -373,7 +373,7 @@ rational = scientifically realToFrac
373373-- This function does not accept string representations of \"NaN\" or
374374-- \"Infinity\".
375375double :: Parser Double
376−double = rational
376+double = scientifically Sci.toRealFloat
377377
378378-- | Parse a number, attempting to preserve both speed and precision.
379379--
@@ -388,11 +388,11 @@ double = rational
388388-- \"Infinity\".
389389number :: Parser Number
390390number = scientifically $ \s ->
391− let e = base10Exponent s
392− c = coefficient s
391+ let e = Sci.base10Exponent s
392+ c = Sci.coefficient s
393393 in if e >= 0
394394 then I (c * 10 ^ e)
395− else D (fromInteger c / 10 ^ negate e)
395+ else D (Sci.toRealFloat s)
396396
397397-- | Parse a scientific number.
398398--
@@ -400,6 +400,9 @@ number = scientifically $ \s ->
400400scientific :: Parser Scientific
401401scientific = scientifically id
402402
403+-- A strict pair
404+data SP = SP !Integer {-# UNPACK #-}!Int
405+
403406{-# INLINE scientifically #-}
404407scientifically :: (Scientific -> a) -> Parser a
405408scientifically h = do
@@ -408,19 +411,19 @@ scientifically h = do
408411
409412 n <- decimal
410413
411− let f fracDigits = Sci.scientific (T.foldl' step n fracDigits)
412− (negate $ T.length fracDigits)
414+ let f fracDigits = SP (T.foldl' step n fracDigits)
415+ (negate $ T.length fracDigits)
413416 step a c = a * 10 + fromIntegral (ord c - 48)
414417
415− s <- (I.satisfy (=='.') *> (f <$> I.takeWhile isDigit)) <|>
416− pure (Sci.scientific n 0)
418+ SP c e <- (I.satisfy (=='.') *> (f <$> I.takeWhile isDigit)) <|>
419+ pure (SP n 0)
417420
418− let !signedCoeff | positive = coefficient s
419− | otherwise = negate $ coefficient s
421+ let !signedCoeff | positive = c
422+ | otherwise = -c
420423
421− (I.satisfy (\c -> c == 'e' || c == 'E') *>
422− fmap (h . Sci.scientific signedCoeff . (base10Exponent s +)) (signed decimal)) <|>
423− return (h $ Sci.scientific signedCoeff (base10Exponent s))
424+ (I.satisfy (\w -> w == 'e' || w == 'E') *>
425+ fmap (h . Sci.scientific signedCoeff . (e +)) (signed decimal)) <|>
426+ return (h $ Sci.scientific signedCoeff e)
424427
425428-- | Parse a single digit, as recognised by 'isDigit'.
426429digit :: Parser Char
attoparsec.cabal+1−1
@@ -44,7 +44,7 @@ library
4444 containers,
4545 deepseq,
4646 text >= 0.11.3.1,
47− scientific >= 0.2 && < 0.3
47+ scientific >= 0.3.1
4848
4949 exposed-modules: Data.Attoparsec
5050 Data.Attoparsec.ByteString