Re: Correct parsers for bounded integral values

Viktor Dukhovni <[email protected]> Sun, 13 Jul 2025 23:18:30 +1000
Newsgroups gmane.comp.lang.haskell.cafe
Message-ID <[email protected]>
On Sun, Jul 13, 2025 at 01:34:15PM +0200, Stefan Klinger wrote:

> I've been bugged by the silent overflowing of integer parsers as
> provided by `base`, `attoparsec`, and others.  I'd go so far as to
> call it a bug when the user types `298` and the parser says `Right
> 42`.

Fortunately, silent overflow when parsing bounded integers is not a
behaviour of either the ByteString or streaming-bytestring libraries.

    $ ghci -v0
    λ> import Data.ByteString.Char8
    λ> :t readInt8
    readInt8 :: ByteString -> Maybe (GHC.Internal.Int.Int8, ByteString)
    λ> readInt8 $ pack "42 foo"
    Just (42," foo")
    λ> readInt8 $ pack "342 foo"
    Nothing
    λ> readInt16 $ pack "342 foo"
    Just (342," foo")
    λ> readInt16 $ pack "76342 foo"
    Nothing
    λ> readInt32 $ pack "76342 foo"
    Just (76342," foo")
    λ> readInt32 $ pack "9876531042 foo"
    Nothing
    λ> readInt64 $ pack "9876531042 foo"
    Just (9876531042," foo")

The bytestring library also supports the various Word sizes, Integer and
Natural!

Similarly, (be it for just the Int type):

    [viktor@chardros m]$ cabal repl -z -v0 --build-depend streaming-bytestring
    λ> import Streaming.ByteString.Char8
    λ> import Data.Functor.Of
    λ> import Data.Functor.Compose
    λ> import Data.Bifoldable
    λ> (x :> _) <-  getCompose <$> readInt (string "42 foo")
    λ> print x
    Just 42
    λ> (x :> _) <-  getCompose <$> readInt (string "9876543210987654210 foo")
    λ> print x
    Nothing

> Unfortunately, all parsing libraries I've looked at get this wrong.  A
> [solution][4] is proposed below.

There were more libraries to look at.

-- 
    Viktor.
_______________________________________________
Haskell-Cafe mailing list
To (un)subscribe, modify options or view archives go to:
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Only members subscribed via the mailman list are allowed to post.