{-# 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
class (Num n) => Intable b n where
int :: b -> n
instance Intable ByteString Word8 where
int :: ByteString -> Word8
int = ByteString -> Word8
forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word16 where
int :: ByteString -> Word16
int = ByteString -> Word16
forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word32 where
int :: ByteString -> Word32
int = ByteString -> Word32
forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word64 where
int :: ByteString -> Word64
int = ByteString -> Word64
forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Word where
int :: ByteString -> Word
int = ByteString -> Word
forall n. Num n => ByteString -> n
strict_unsigned
instance Intable ByteString Int8 where
int :: ByteString -> Int8
int = ByteString -> Int8
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int16 where
int :: ByteString -> Int16
int = ByteString -> Int16
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int32 where
int :: ByteString -> Int32
int = ByteString -> Int32
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int64 where
int :: ByteString -> Int64
int = ByteString -> Int64
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Int where
int :: ByteString -> Int
int = ByteString -> Int
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Float where
int :: ByteString -> Float
int = ByteString -> Float
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Double where
int :: ByteString -> Double
int = ByteString -> Double
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Rational where
int :: ByteString -> Rational
int = ByteString -> Rational
forall n. Num n => ByteString -> n
strict_signed
instance Intable ByteString Integer where
int :: ByteString -> Integer
int = ByteString -> Integer
forall n. Num n => ByteString -> n
strict_signed
instance Intable Lazy.ByteString Word8 where
int :: ByteString -> Word8
int = ByteString -> Word8
forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word16 where
int :: ByteString -> Word16
int = ByteString -> Word16
forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word32 where
int :: ByteString -> Word32
int = ByteString -> Word32
forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word64 where
int :: ByteString -> Word64
int = ByteString -> Word64
forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Word where
int :: ByteString -> Word
int = ByteString -> Word
forall n. Num n => ByteString -> n
lazy_unsigned
instance Intable Lazy.ByteString Int8 where
int :: ByteString -> Int8
int = ByteString -> Int8
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int16 where
int :: ByteString -> Int16
int = ByteString -> Int16
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int32 where
int :: ByteString -> Int32
int = ByteString -> Int32
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int64 where
int :: ByteString -> Int64
int = ByteString -> Int64
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Int where
int :: ByteString -> Int
int = ByteString -> Int
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Float where
int :: ByteString -> Float
int = ByteString -> Float
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Double where
int :: ByteString -> Double
int = ByteString -> Double
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Rational where
int :: ByteString -> Rational
int = ByteString -> Rational
forall n. Num n => ByteString -> n
lazy_signed
instance Intable Lazy.ByteString Integer where
int :: ByteString -> Integer
int = ByteString -> Integer
forall n. Num n => ByteString -> n
lazy_signed
lazy_unsigned :: (Num n) => Lazy.ByteString -> n
lazy_unsigned :: ByteString -> n
lazy_unsigned = (n -> ByteString -> n) -> n -> ByteString -> n
forall a. (a -> ByteString -> a) -> a -> ByteString -> a
Lazy.foldlChunks ((n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
positive) n
0
lazy_signed :: (Num n) => Lazy.ByteString -> n
lazy_signed :: ByteString -> n
lazy_signed ByteString
bytes
| ByteString -> Bool
Lazy.null ByteString
bytes = n
0
| ByteString -> Char
Lazy.head ByteString
bytes Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
fold n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
negative n
0 (ByteString -> ByteString
Lazy.tail ByteString
bytes)
| ByteString -> Char
Lazy.head ByteString
bytes Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'+' = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
fold n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
positive n
0 (ByteString -> ByteString
Lazy.tail ByteString
bytes)
| Bool
otherwise = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
fold n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
positive n
0 ByteString
bytes
where
fold :: (a -> Word8 -> a) -> a -> ByteString -> a
fold = (a -> ByteString -> a) -> a -> ByteString -> a
forall a. (a -> ByteString -> a) -> a -> ByteString -> a
Lazy.foldlChunks ((a -> ByteString -> a) -> a -> ByteString -> a)
-> ((a -> Word8 -> a) -> a -> ByteString -> a)
-> (a -> Word8 -> a)
-> a
-> ByteString
-> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> Word8 -> a) -> a -> ByteString -> a
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl'
strict_unsigned :: (Num n) => ByteString -> n
strict_unsigned :: ByteString -> n
strict_unsigned = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
positive n
0
strict_signed :: (Num n) => ByteString -> n
strict_signed :: ByteString -> n
strict_signed ByteString
bytes
| ByteString -> Bool
null ByteString
bytes = n
0
| ByteString -> Char
head ByteString
bytes Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'-' = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
negative n
0 (ByteString -> ByteString
tail ByteString
bytes)
| ByteString -> Char
head ByteString
bytes Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'+' = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
positive n
0 (ByteString -> ByteString
tail ByteString
bytes)
| Bool
otherwise = (n -> Word8 -> n) -> n -> ByteString -> n
forall a. (a -> Word8 -> a) -> a -> ByteString -> a
foldl' n -> Word8 -> n
forall n. Num n => n -> Word8 -> n
positive n
0 ByteString
bytes
positive :: (Num n) => n -> Word8 -> n
positive :: n -> Word8 -> n
positive n
acc Word8
byte = (n
acc n -> n -> n
forall a. Num a => a -> a -> a
* n
10) n -> n -> n
forall a. Num a => a -> a -> a
+ Word8 -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
byte Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
c2w Char
'0')
negative :: (Num n) => n -> Word8 -> n
negative :: n -> Word8 -> n
negative n
acc Word8
byte = (n
acc n -> n -> n
forall a. Num a => a -> a -> a
* n
10) n -> n -> n
forall a. Num a => a -> a -> a
- Word8 -> n
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word8
byte Word8 -> Word8 -> Word8
forall a. Num a => a -> a -> a
- Char -> Word8
c2w Char
'0')