Portability | |
---|---|

Stability | provisional |

Maintainer | Don Stewart <dons@galois.com> |

- readDouble :: ByteString -> Maybe (Double, ByteString)
- unsafeReadDouble :: ByteString -> Maybe (Double, ByteString)

# Documentation

readDouble :: ByteString -> Maybe (Double, ByteString)Source

Parse the initial portion of the ByteString as a Double precision floating point value. The expected form of the numeric literal is given by:

- An optional
`+`

or`-`

sign - Decimal digits, OR
- 0 [oO] and a sequence of octal digits, OR
- 0 [xX] and a sequence of hexadecimal digits, OR
- An optional decimal point, followed by a sequence of decimal digits,
- And an optional exponent

The result is returned as a pair of a double-precisoin floating point value, and the remaining input, or Nothing, should no parse be found.

For example, to sum a file of floating point numbers, one per line,

import qualified Data.ByteString.Char8 as S import qualified Data.ByteString.Unsafe as S import Data.ByteString.Lex.Double main = print . go 0 =<< S.getContents where go n s = case readDouble s of Nothing -> n Just (k,rest) -> go (n+k) (S.tail rest)

unsafeReadDouble :: ByteString -> Maybe (Double, ByteString)Source

Bare bones, unsafe wrapper for strtod. This provides a non-copying direct parsing of Double values from a ByteString. It uses strtod directly on the bytestring buffer. strtod requires the string to be null terminated, or for a guarantee that parsing will find a floating point value before the end of the string.