[Haskell-cafe] ANN: ByteString Nums

Jason Dusek jason.dusek at gmail.com
Sun Aug 23 03:26:31 EDT 2009


  This is a simple package for relatively careless parsing of
  numbers from ByteStrings. It works to parse out integer
  strings, floating point strings and hex strings.

    http://hackage.haskell.org/package/bytestring-nums

  For integer strings, an initial sign is detected and non-digit
  characters are ignored:

Prelude> :load Data.ByteString.Nums.Careless.Int
*Data.ByteString.Nums.Careless.Int> let strings = ["121.3040", "1o2",
"-a1", "0xff", "-0x30"]
*Data.ByteString.Nums.Careless.Int> fmap (int . pack) strings :: [Int]
[1213040,12,-1,0,-30]
*Data.ByteString.Nums.Careless.Int> fmap (int . pack) strings :: [Double]
[1213040.0,12.0,-1.0,0.0,-30.0]

  For hexadecimal strings, no signs are recognized and
  characters that aren't hex digits are ignored. One consequence
  of this is that an initial `0x` is skipped.

Prelude> :load Data.ByteString.Nums.Careless.Hex
*Data.ByteString.Nums.Careless.Hex> let strings = ["121.3040", "1o2",
"-a1", "0xff", "-0x30"]
*Data.ByteString.Nums.Careless.Hex> fmap (hex . pack) strings :: [Int]
[18952256,18,161,255,48]

  The float parser recognizes the last group of digits behind a
  comma or period as the fractional part; all preceding digits
  are treated as the integral part. Naturally, signs are
  handled.

Prelude> :load Data.ByteString.Nums.Careless.Float
*Data.ByteString.Nums.Careless.Float> let strings = ["121.3040",
"2.100,32", "1,o2", "-a1", "0xff", "-0x30"]
*Data.ByteString.Nums.Careless.Float> fmap (float . pack) strings :: [Double]
[122.304,2100.32,1.2,-1.0,0.0,-30.0]
*Data.ByteString.Nums.Careless.Float> fmap (float . pack) strings :: [Rational]
[15163 % 125,52508 % 25,6 % 5,(-1) % 1,0 % 1,(-30) % 1]

  I have not applied any `SPECIALIZE` or `INLINE` directives at
  this time; it'd be great to have some insight in to optimizing
  collections of small functions like this library.

--
Jason Dusek


More information about the Haskell-Cafe mailing list