git.iko.soy/typeable/attoparsec
on master help sign in
Only use Sci.scientific to construct the final Scientific

Sci.scientific now normalizes so we should only use it once.

commit613f1662c90c4db628acc7f11b331948051b763c authorBas van Dijk <v.dijk.bas@gmail.com> dateWed 14 May 2014, 01:18 (12 years ago) parentc99728b
2 files changed+25−19
Data/Attoparsec/ByteString/Char8.hs+13−10 Data/Attoparsec/Text.hs+12−9
Data/Attoparsec/ByteString/Char8.hs+13−10
@@ -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+12−9
@@ -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