module Data.Binary.Parser.Char8 where
import Control.Applicative
import qualified Data.Binary.Get as BG
import Data.Binary.Get.Internal
import qualified Data.Binary.Parser.Word8 as W
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.ByteString.Internal (c2w, w2c)
import qualified Data.ByteString.Unsafe as B
import Prelude hiding (takeWhile)
peekMaybe :: Get (Maybe Char)
peekMaybe :: Get (Maybe Char)
peekMaybe = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Word8 -> Char
w2c forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get (Maybe Word8)
W.peekMaybe
{-# INLINE peekMaybe #-}
peek :: Get Char
peek :: Get Char
peek = Word8 -> Char
w2c forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
W.peek
{-# INLINE peek #-}
satisfy :: (Char -> Bool) -> Get Char
satisfy :: (Char -> Bool) -> Get Char
satisfy Char -> Bool
p = Word8 -> Char
w2c forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Word8 -> Bool) -> Get Word8
W.satisfy (Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE satisfy #-}
satisfyWith :: (Char -> a) -> (a -> Bool) -> Get a
satisfyWith :: forall a. (Char -> a) -> (a -> Bool) -> Get a
satisfyWith Char -> a
f = forall a. (Word8 -> a) -> (a -> Bool) -> Get a
W.satisfyWith (Char -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE satisfyWith #-}
char :: Char -> Get ()
char :: Char -> Get ()
char Char
c = Word8 -> Get ()
W.word8 (Char -> Word8
c2w Char
c)
{-# INLINE char #-}
anyChar :: Get Char
anyChar :: Get Char
anyChar = Word8 -> Char
w2c forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get Word8
BG.getWord8
{-# INLINE anyChar #-}
skipChar :: (Char -> Bool) -> Get ()
skipChar :: (Char -> Bool) -> Get ()
skipChar Char -> Bool
p = (Word8 -> Bool) -> Get ()
W.skipWord8 (Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE skipChar #-}
takeTill :: (Char -> Bool) -> Get ByteString
takeTill :: (Char -> Bool) -> Get ByteString
takeTill Char -> Bool
p = (Word8 -> Bool) -> Get ByteString
W.takeTill (Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE takeTill #-}
takeWhile :: (Char -> Bool) -> Get ByteString
takeWhile :: (Char -> Bool) -> Get ByteString
takeWhile Char -> Bool
p = (Word8 -> Bool) -> Get ByteString
W.takeWhile (Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE takeWhile #-}
takeWhile1 :: (Char -> Bool) -> Get ByteString
takeWhile1 :: (Char -> Bool) -> Get ByteString
takeWhile1 Char -> Bool
p = (Word8 -> Bool) -> Get ByteString
W.takeWhile1 (Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE takeWhile1 #-}
skipWhile :: (Char -> Bool) -> Get ()
skipWhile :: (Char -> Bool) -> Get ()
skipWhile Char -> Bool
p = (Word8 -> Bool) -> Get ()
W.skipWhile (Char -> Bool
p forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> Char
w2c)
{-# INLINE skipWhile #-}
stringCI :: ByteString -> Get ByteString
stringCI :: ByteString -> Get ByteString
stringCI ByteString
bs = do
let l :: Int
l = ByteString -> Int
B.length ByteString
bs
Int -> Get ()
ensureN Int
l
ByteString
bs' <- Int -> ByteString -> ByteString
B.unsafeTake Int
l forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Get ByteString
get
if (Word8 -> Word8) -> ByteString -> ByteString
B.map forall {a}. (Ord a, Num a) => a -> a
toLower ByteString
bs' forall a. Eq a => a -> a -> Bool
== (Word8 -> Word8) -> ByteString -> ByteString
B.map forall {a}. (Ord a, Num a) => a -> a
toLower ByteString
bs
then ByteString -> Get ()
put (Int -> ByteString -> ByteString
B.unsafeDrop Int
l ByteString
bs') forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall (m :: * -> *) a. Monad m => a -> m a
return ByteString
bs'
else forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
"stringCI"
where
toLower :: a -> a
toLower a
w | a
w forall a. Ord a => a -> a -> Bool
>= a
65 Bool -> Bool -> Bool
&& a
w forall a. Ord a => a -> a -> Bool
<= a
90 = a
w forall a. Num a => a -> a -> a
+ a
32
| Bool
otherwise = a
w
{-# INLINE stringCI #-}
isSpace :: Char -> Bool
isSpace :: Char -> Bool
isSpace Char
c = (Char
c forall a. Eq a => a -> a -> Bool
== Char
' ') Bool -> Bool -> Bool
|| (Char
'\t' forall a. Ord a => a -> a -> Bool
<= Char
c Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'\r')
{-# INLINE isSpace #-}
isDigit :: Char -> Bool
isDigit :: Char -> Bool
isDigit Char
c = Char
c forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9'
{-# INLINE isDigit #-}
isHexDigit :: Char -> Bool
isHexDigit :: Char -> Bool
isHexDigit Char
c = (Char
c forall a. Ord a => a -> a -> Bool
>= Char
'0' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'9') Bool -> Bool -> Bool
|| (Char
c forall a. Ord a => a -> a -> Bool
>= Char
'a' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'f') Bool -> Bool -> Bool
|| (Char
c forall a. Ord a => a -> a -> Bool
>= Char
'A' Bool -> Bool -> Bool
&& Char
c forall a. Ord a => a -> a -> Bool
<= Char
'F')
{-# INLINE isHexDigit #-}
isHorizontalSpace :: Char -> Bool
isHorizontalSpace :: Char -> Bool
isHorizontalSpace Char
c = Char
c forall a. Eq a => a -> a -> Bool
== Char
' ' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\t'
{-# INLINE isHorizontalSpace #-}
isEndOfLine :: Char -> Bool
isEndOfLine :: Char -> Bool
isEndOfLine Char
c = Char
c forall a. Eq a => a -> a -> Bool
== Char
'\r' Bool -> Bool -> Bool
|| Char
c forall a. Eq a => a -> a -> Bool
== Char
'\n'
{-# INLINE isEndOfLine #-}