{-# LANGUAGE MultiParamTypeClasses
           , TypeSynonymInstances
           , FlexibleInstances
  #-}


module Data.ByteString.Nums.Careless.Int where


import Prelude hiding (head, tail, null)
import Data.Word
import Data.Int
import Data.ByteString hiding (head, pack)
import Data.ByteString.Internal
import Data.ByteString.Char8 hiding (foldl')
import qualified Data.ByteString.Lazy.Char8 as Lazy
import qualified Data.ByteString.Lazy.Internal as Lazy




{-| Types that can be read from integer strings. Parses only decimal digits.
    Signed types can be read from strings that begin with a plus or minus;
    unsigned types are read from strings consisting solely of decimal digits.
 -}
class (Num n) => Intable b n where
  int                       ::  b -> n

instance Intable ByteString Word8 where
  int :: ByteString -> Word8
int                        =  forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word16 where
  int :: ByteString -> Word16
int                        =  forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word32 where
  int :: ByteString -> Word32
int                        =  forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word64 where
  int :: ByteString -> Word64
int                        =  forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word where
  int :: ByteString -> Word
int                        =  forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Int8 where
  int :: ByteString -> Int8
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int16 where
  int :: ByteString -> Int16
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int32 where
  int :: ByteString -> Int32
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int64 where
  int :: ByteString -> Int64
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int where
  int :: ByteString -> Int
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Float where
  int :: ByteString -> Float
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Double where
  int :: ByteString -> Double
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Rational where
  int :: ByteString -> Rational
int                        =  forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Integer where
  int :: ByteString -> Integer
int                        =  forall n. Num n => ByteString -> n
strict_signed

instance Intable Lazy.ByteString Word8 where
  int :: ByteString -> Word8
int                        =  forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word16 where
  int :: ByteString -> Word16
int                        =  forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word32 where
  int :: ByteString -> Word32
int                        =  forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word64 where
  int :: ByteString -> Word64
int                        =  forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word where
  int :: ByteString -> Word
int                        =  forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Int8 where
  int :: ByteString -> Int8
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int16 where
  int :: ByteString -> Int16
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int32 where
  int :: ByteString -> Int32
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int64 where
  int :: ByteString -> Int64
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int where
  int :: ByteString -> Int
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Float where
  int :: ByteString -> Float
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Double where
  int :: ByteString -> Double
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Rational where
  int :: ByteString -> Rational
int                        =  forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Integer where
  int :: ByteString -> Integer
int                        =  forall n. Num n => ByteString -> n
lazy_signed




lazy_unsigned               ::  (Num n) => Lazy.ByteString -> n
lazy_unsigned :: forall n. Num n => ByteString -> n
lazy_unsigned                =  forall a. (a -> ByteString -> a) -> a -> ByteString -> a
Lazy.foldlChunks (forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' forall n. Num n => n -> Word8 -> n
positive) n
0

lazy_signed                 ::  (Num n) => Lazy.ByteString -> n
lazy_signed :: forall n. Num n => ByteString -> n
lazy_signed ByteString
bytes
  | ByteString -> Bool
Lazy.null ByteString
bytes          =  n
0
  | ByteString -> Char
Lazy.head ByteString
bytes forall a. Eq a => a -> a -> Bool
== Char
'-'   =  forall {a}. (a -> Word8 -> a) -> a -> ByteString -> a
fold forall n. Num n => n -> Word8 -> n
negative n
0 (HasCallStack => ByteString -> ByteString
Lazy.tail ByteString
bytes)
  | ByteString -> Char
Lazy.head ByteString
bytes forall a. Eq a => a -> a -> Bool
== Char
'+'   =  forall {a}. (a -> Word8 -> a) -> a -> ByteString -> a
fold forall n. Num n => n -> Word8 -> n
positive n
0 (HasCallStack => ByteString -> ByteString
Lazy.tail ByteString
bytes)
  | Bool
otherwise                =  forall {a}. (a -> Word8 -> a) -> a -> ByteString -> a
fold forall n. Num n => n -> Word8 -> n
positive n
0 ByteString
bytes
 where
  fold :: (a -> Word8 -> a) -> a -> ByteString -> a
fold                       =  forall a. (a -> ByteString -> a) -> a -> ByteString -> a
Lazy.foldlChunks forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl'


strict_unsigned             ::  (Num n) => ByteString -> n
strict_unsigned :: forall n. Num n => ByteString -> n
strict_unsigned              =  forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' forall n. Num n => n -> Word8 -> n
positive n
0

strict_signed               ::  (Num n) => ByteString -> n
strict_signed :: forall n. Num n => ByteString -> n
strict_signed ByteString
bytes
  | ByteString -> Bool
null ByteString
bytes               =  n
0
  | ByteString -> Char
head ByteString
bytes forall a. Eq a => a -> a -> Bool
== Char
'-'        =  forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' forall n. Num n => n -> Word8 -> n
negative n
0 (HasCallStack => ByteString -> ByteString
tail ByteString
bytes)
  | ByteString -> Char
head ByteString
bytes forall a. Eq a => a -> a -> Bool
== Char
'+'        =  forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' forall n. Num n => n -> Word8 -> n
positive n
0 (HasCallStack => ByteString -> ByteString
tail ByteString
bytes)
  | Bool
otherwise                =  forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' forall n. Num n => n -> Word8 -> n
positive n
0 ByteString
bytes


positive                    ::  (Num n) => n -> Word8 -> n
positive :: forall n. Num n => n -> Word8 -> n
positive  n
acc  Word8
byte          =  (n
acc forall a. Num a => a -> a -> a
* n
10) forall a. Num a => a -> a -> a
+ forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
byte forall a. Num a => a -> a -> a
- Char -> Word8
c2w Char
'0')

negative                    ::  (Num n) => n -> Word8 -> n
negative :: forall n. Num n => n -> Word8 -> n
negative  n
acc  Word8
byte          =  (n
acc forall a. Num a => a -> a -> a
* n
10) forall a. Num a => a -> a -> a
- forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
byte forall a. Num a => a -> a -> a
- Char -> Word8
c2w Char
'0')