bytestring-nums (empty) → 0.1.0
raw patch · 8 files changed
+429/−0 lines, 8 filesdep +basedep +bytestringdep +containerssetup-changed
Dependencies added: base, bytestring, containers
Files
- Data/ByteString/Nums/Careless.hs +18/−0
- Data/ByteString/Nums/Careless/Float.hs +59/−0
- Data/ByteString/Nums/Careless/Hex.hs +128/−0
- Data/ByteString/Nums/Careless/Int.hs +158/−0
- LICENSE +28/−0
- README +4/−0
- Setup.hs +4/−0
- bytestring-nums.cabal +30/−0
+ Data/ByteString/Nums/Careless.hs view
@@ -0,0 +1,18 @@++++{-| Careless conversion of @ByteString@s to numbers, ignoring bytes that+ aren't hex or decimal digits.+ -}+module Data.ByteString.Nums.Careless+ ( module Data.ByteString.Nums.Careless.Int+ , module Data.ByteString.Nums.Careless.Hex+ , module Data.ByteString.Nums.Careless.Float+ ) where+++import Data.ByteString.Nums.Careless.Int (Intable(..))+import Data.ByteString.Nums.Careless.Hex (Hexable(..))+import Data.ByteString.Nums.Careless.Float (Floatable(..))++
+ Data/ByteString/Nums/Careless/Float.hs view
@@ -0,0 +1,59 @@+++{-# LANGUAGE MultiParamTypeClasses+ #-}+++module Data.ByteString.Nums.Careless.Float where+++import Prelude hiding (length, splitAt)+import Data.ByteString.Char8 hiding (inits, elem, last)+import qualified Data.ByteString.Lazy.Char8 as Lazy+++import Data.ByteString.Nums.Careless.Int++++++{-| Types that can be read from floating point strings. The fractional part is+ taken to be the last group of digits behind a decimal point or comma.+ Characters are not decimal digits are simply skipped.+ -}+class (Intable b f, Fractional f) => Floatable b f where+ float :: b -> f++instance Floatable ByteString Float where+ float = strict_float+instance Floatable ByteString Double where+ float = strict_float+instance Floatable ByteString Rational where+ float = strict_float++instance Floatable Lazy.ByteString Float where+ float = lazy_float+instance Floatable Lazy.ByteString Double where+ float = lazy_float+instance Floatable Lazy.ByteString Rational where+ float = lazy_float+++strict_float bytes = case findIndices (`elem` ".,") bytes of+ [ ] -> int bytes+ idx -> hi' + (int lo * (0.1 ^ length lo) * s)+ where+ (hi, lo) = splitAt (last idx) bytes+ hi' = int hi+ s = signum hi'++lazy_float bytes = case Lazy.findIndices (`elem` ".,") bytes of+ [ ] -> int bytes+ idx -> hi' + (int lo * (0.1 ^ Lazy.length lo) * s)+ where+ (hi, lo) = Lazy.splitAt (last idx) bytes+ hi' = int hi+ s = signum hi'++
+ Data/ByteString/Nums/Careless/Hex.hs view
@@ -0,0 +1,128 @@+++{-# LANGUAGE MultiParamTypeClasses+ #-}+++module Data.ByteString.Nums.Careless.Hex where+++import Prelude hiding (head, tail, drop)+import Data.Word+import Data.Int+import Data.Ratio+import Data.ByteString hiding (head, pack)+import Data.ByteString.Char8 hiding (foldl')+import Data.ByteString.Internal+import qualified Data.ByteString.Lazy.Char8 as Lazy+import qualified Data.ByteString.Lazy.Internal as Lazy+++++{-| Types that can be read from hexadecimal strings. Characters that are not+ hexadecimal digits are skipped over.+ -}+class (Num n) => Hexable b n where+ hex :: b -> n++instance Hexable ByteString Word8 where+ hex = strict_hex+instance Hexable ByteString Word16 where+ hex = strict_hex+instance Hexable ByteString Word32 where+ hex = strict_hex+instance Hexable ByteString Word64 where+ hex = strict_hex+instance Hexable ByteString Word where+ hex = strict_hex+instance Hexable ByteString Int8 where+ hex = strict_hex+instance Hexable ByteString Int16 where+ hex = strict_hex+instance Hexable ByteString Int32 where+ hex = strict_hex+instance Hexable ByteString Int64 where+ hex = strict_hex+instance Hexable ByteString Int where+ hex = strict_hex+instance Hexable ByteString Float where+ hex = strict_hex+instance Hexable ByteString Double where+ hex = strict_hex+instance Hexable ByteString Rational where+ hex = strict_hex+instance Hexable ByteString Integer where+ hex = strict_hex++instance Hexable Lazy.ByteString Word8 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Word16 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Word32 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Word64 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Word where+ hex = lazy_hex+instance Hexable Lazy.ByteString Int8 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Int16 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Int32 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Int64 where+ hex = lazy_hex+instance Hexable Lazy.ByteString Int where+ hex = lazy_hex+instance Hexable Lazy.ByteString Float where+ hex = lazy_hex+instance Hexable Lazy.ByteString Double where+ hex = lazy_hex+instance Hexable Lazy.ByteString Rational where+ hex = lazy_hex+instance Hexable Lazy.ByteString Integer where+ hex = lazy_hex+++++hexalize :: (Num n) => n -> Word8 -> n+{-+{-# SPECIALIZE INLINE hexalize :: Word8 -> Word8 -> Word8 #-}+{-# SPECIALIZE INLINE hexalize :: Word16 -> Word8 -> Word16 #-}+{-# SPECIALIZE INLINE hexalize :: Word32 -> Word8 -> Word32 #-}+{-# SPECIALIZE INLINE hexalize :: Word64 -> Word8 -> Word64 #-}+{-# SPECIALIZE INLINE hexalize :: Word -> Word8 -> Word #-}+{-# SPECIALIZE INLINE hexalize :: Int8 -> Word8 -> Int8 #-}+{-# SPECIALIZE INLINE hexalize :: Int16 -> Word8 -> Int16 #-}+{-# SPECIALIZE INLINE hexalize :: Int32 -> Word8 -> Int32 #-}+{-# SPECIALIZE INLINE hexalize :: Int64 -> Word8 -> Int64 #-}+{-# SPECIALIZE INLINE hexalize :: Int -> Word8 -> Int #-}+{-# SPECIALIZE INLINE hexalize :: Float -> Word8 -> Float #-}+{-# SPECIALIZE INLINE hexalize :: Double -> Word8 -> Double #-}+{-# SPECIALIZE INLINE hexalize :: Rational -> Word8 -> Rational #-}+{-# SPECIALIZE INLINE hexalize :: Integer -> Word8 -> Integer #-}+ -}+hexalize acc byte+ | between byte 'a' 'f' = place_up (byte + 0x0a - c2w 'a')+ | between byte 'A' 'F' = place_up (byte + 0x0a - c2w 'A')+ | between byte '0' '9' = place_up (byte - c2w '0')+ | otherwise = acc+ where+ between b a z = b >= c2w a && byte <= c2w z+ place_up b = (0x10 * acc) + fromIntegral b++strict_hex bytes = foldl' hexalize 0 piece+ where+ piece = if pack "0x" `isPrefixOf` bytes+ then drop 2 bytes+ else bytes++lazy_hex bytes = Lazy.foldlChunks (foldl' hexalize) 0 piece+ where+ piece = if Lazy.pack "0x" `Lazy.isPrefixOf` bytes+ then Lazy.drop 2 bytes+ else bytes++
+ Data/ByteString/Nums/Careless/Int.hs view
@@ -0,0 +1,158 @@+++{-# LANGUAGE MultiParamTypeClasses+ #-}+++module Data.ByteString.Nums.Careless.Int where+++import Prelude hiding (head, tail, null)+import Data.Word+import Data.Int+import Data.Ratio+import Data.ByteString hiding (head)+import Data.ByteString.Internal+import Data.ByteString.Char8 (head)+import qualified Data.ByteString.Lazy.Char8 as Lazy+import qualified Data.ByteString.Lazy.Internal as Lazy+++++{-| Types that can be read from integer strings. Characters that are not+ decimal digits are simply skipped.+ -}+class (Num n) => Intable b n where+ int :: b -> n++instance Intable ByteString Word8 where+ int = strict_int+instance Intable ByteString Word16 where+ int = strict_int+instance Intable ByteString Word32 where+ int = strict_int+instance Intable ByteString Word64 where+ int = strict_int+instance Intable ByteString Word where+ int = strict_int+instance Intable ByteString Int8 where+ int = strict_int+instance Intable ByteString Int16 where+ int = strict_int+instance Intable ByteString Int32 where+ int = strict_int+instance Intable ByteString Int64 where+ int = strict_int+instance Intable ByteString Int where+ int = strict_int+instance Intable ByteString Float where+ int = strict_int+instance Intable ByteString Double where+ int = strict_int+instance Intable ByteString Rational where+ int = strict_int+instance Intable ByteString Integer where+ int = strict_int++instance Intable Lazy.ByteString Word8 where+ int = lazy_int+instance Intable Lazy.ByteString Word16 where+ int = lazy_int+instance Intable Lazy.ByteString Word32 where+ int = lazy_int+instance Intable Lazy.ByteString Word64 where+ int = lazy_int+instance Intable Lazy.ByteString Word where+ int = lazy_int+instance Intable Lazy.ByteString Int8 where+ int = lazy_int+instance Intable Lazy.ByteString Int16 where+ int = lazy_int+instance Intable Lazy.ByteString Int32 where+ int = lazy_int+instance Intable Lazy.ByteString Int64 where+ int = lazy_int+instance Intable Lazy.ByteString Int where+ int = lazy_int+instance Intable Lazy.ByteString Float where+ int = lazy_int+instance Intable Lazy.ByteString Double where+ int = lazy_int+instance Intable Lazy.ByteString Rational where+ int = lazy_int+instance Intable Lazy.ByteString Integer where+ int = lazy_int+++++digitize :: (Num n) => (n -> n -> n) -> n -> Word8 -> n+{-+{-# SPECIALIZE INLINE+digitize :: (Word8 -> Word8 -> Word8) -> Word8 -> Word8 -> Word8+ #-}+{-# SPECIALIZE INLINE+digitize :: (Word16 -> Word16 -> Word16) -> Word16 -> Word8 -> Word16+ #-}+{-# SPECIALIZE INLINE+digitize :: (Word32 -> Word32 -> Word32) -> Word32 -> Word8 -> Word32+ #-}+{-# SPECIALIZE INLINE+digitize :: (Word64 -> Word64 -> Word64) -> Word64 -> Word8 -> Word64+ #-}+{-# SPECIALIZE INLINE+digitize :: (Word -> Word -> Word) -> Word -> Word8 -> Word+ #-}+{-# SPECIALIZE INLINE+digitize :: (Word8 -> Word8 -> Word8) -> Word8 -> Word8 -> Word8+ #-}+{-# SPECIALIZE INLINE+digitize :: (Int16 -> Int16 -> Int16) -> Int16 -> Word8 -> Int16+ #-}+{-# SPECIALIZE INLINE+digitize :: (Int32 -> Int32 -> Int32) -> Int32 -> Word8 -> Int32+ #-}+{-# SPECIALIZE INLINE+digitize :: (Int64 -> Int64 -> Int64) -> Int64 -> Word8 -> Int64+ #-}+{-# SPECIALIZE INLINE+digitize :: (Int -> Int -> Int) -> Int -> Word8 -> Int+ #-}+{-# SPECIALIZE INLINE+digitize :: (Float -> Float -> Float) -> Float -> Word8 -> Float+ #-}+{-# SPECIALIZE INLINE+digitize :: (Double -> Double -> Double) -> Double -> Word8 -> Double+ #-}+{-# SPECIALIZE INLINE+digitize :: (Rational -> Rational -> Rational) -> Rational -> Word8 -> Rational+ #-}+{-# SPECIALIZE INLINE+digitize :: (Integer -> Integer -> Integer) -> Integer -> Word8 -> Integer+ #-}+ -}+digitize op acc byte+ | between byte '0' '9' = (acc * 10) `op` fromIntegral (byte - c2w '0')+ | otherwise = acc+ where+ between b a z = b >= c2w a && byte <= c2w z++strict_int bytes = foldl' (digitize op) 0 piece+ where+ (op, piece)+ | null bytes = ((+), empty)+ | head bytes == '-' = ((-), tail bytes)+ | head bytes == '+' = ((+), tail bytes)+ | otherwise = ((+), bytes)++lazy_int bytes = Lazy.foldlChunks (foldl' (digitize op)) 0 piece+ where+ (op, piece)+ | Lazy.null bytes = ((+), Lazy.empty)+ | Lazy.head bytes == '-' = ((-), Lazy.tail bytes)+ | Lazy.head bytes == '+' = ((+), Lazy.tail bytes)+ | otherwise = ((+), bytes)+++
+ LICENSE view
@@ -0,0 +1,28 @@++ ©2009 Jason Dusek.++ Redistribution and use in source and binary forms, with or without+ modification, are permitted provided that the following conditions are met:++ . Redistributions of source code must retain the above copyright notice,+ this list of conditions and the following disclaimer.++ . Redistributions in binary form must reproduce the above copyright notice,+ this list of conditions and the following disclaimer in the documentation+ and/or other materials provided with the distribution.++ . Names of the contributors to this software may not be used to endorse or+ promote products derived from this software without specific prior written+ permission.++ This software is provided by the contributors "as is" and any express or+ implied warranties, including, but not limited to, the implied warranties of+ merchantability and fitness for a particular purpose are disclaimed. In no+ event shall the contributors be liable for any direct, indirect, incidental,+ special, exemplary, or consequential damages (including, but not limited to,+ procurement of substitute goods or services; loss of use, data, or profits;+ or business interruption) however caused and on any theory of liability,+ whether in contract, strict liability, or tort (including negligence or+ otherwise) arising in any way out of the use of this software, even if+ advised of the possibility of such damage. +
+ README view
@@ -0,0 +1,4 @@+ +++
+ Setup.hs view
@@ -0,0 +1,4 @@+import Distribution.Simple++main = defaultMain+
+ bytestring-nums.cabal view
@@ -0,0 +1,30 @@+name : bytestring-nums+version : 0.1.0+category : Text+license : BSD3+license-file : LICENSE+author : Jason Dusek+maintainer : jason.dusek@gmail.com+stability : experimental+homepage : http://github.com/jsnx/bytestring-nums+synopsis : Parse numeric literals from ByteStrings.+description :+ Parse numeric literals from ByteStrings.+++cabal-version : >= 1.2+build-type : Simple+extra-source-files : README+++library+ build-depends : base >= 3 && <= 4 + , containers+ , bytestring >= 0.9+ exposed-modules : Data.ByteString.Nums.Careless+ Data.ByteString.Nums.Careless.Hex+ Data.ByteString.Nums.Careless.Int+ Data.ByteString.Nums.Careless.Float+ extensions : MultiParamTypeClasses+ TypeSynonymInstances+