{-# LANGUAGE CPP, BangPatterns #-}
#if __GLASGOW_HASKELL__ >= 701
{-# LANGUAGE Trustworthy #-}
#endif
module Data.ByteString.Lazy.UTF8
( B.ByteString
, decode
, replacement_char
, uncons
, splitAt
, take
, drop
, span
, break
, fromString
, toString
, foldl
, foldr
, length
, lines
, lines'
) where
import Data.Bits
import Data.Word
import Data.Int
import Foreign.Storable
import Foreign.Ptr
import Foreign.ForeignPtr
import Data.Char (ord)
import Control.Exception (assert)
import qualified Data.ByteString.Lazy as B
import qualified Data.ByteString.Lazy.Internal as B
import qualified Data.ByteString.Internal as S
import Prelude hiding (take,drop,splitAt,span,break,foldr,foldl,length,lines)
import Codec.Binary.UTF8.Generic (buncons)
#if MIN_VERSION_base(4,4,0)
import System.IO.Unsafe (unsafeDupablePerformIO)
#else
import GHC.IO (unsafeDupablePerformIO)
#endif
fromString :: String -> B.ByteString
fromString :: String -> ByteString
fromString [] = ByteString
B.empty
fromString xs0 :: String
xs0 = Int -> String -> ByteString
packChunks 32 String
xs0
where
packChunks :: Int -> String -> ByteString
packChunks n :: Int
n xs :: String
xs = case Int -> String -> (ByteString, String)
packUptoLenBytes Int
n String
xs of
(bs :: ByteString
bs, [] ) -> ByteString -> ByteString -> ByteString
B.chunk ByteString
bs ByteString
B.Empty
(bs :: ByteString
bs, xs' :: String
xs') -> ByteString -> ByteString -> ByteString
B.Chunk ByteString
bs (Int -> String -> ByteString
packChunks (Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* 2) Int
B.smallChunkSize) String
xs')
packUptoLenBytes :: Int -> String -> (S.ByteString, String)
packUptoLenBytes :: Int -> String -> (ByteString, String)
packUptoLenBytes len :: Int
len xs :: String
xs = Int -> (Ptr Word8 -> IO (Int, String)) -> (ByteString, String)
forall a. Int -> (Ptr Word8 -> IO (Int, a)) -> (ByteString, a)
unsafeCreateUptoN' Int
len ((Ptr Word8 -> IO (Int, String)) -> (ByteString, String))
-> (Ptr Word8 -> IO (Int, String)) -> (ByteString, String)
forall a b. (a -> b) -> a -> b
$ \ptr :: Ptr Word8
ptr -> do
(end :: Ptr Word8
end, xs' :: String
xs') <- Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go Ptr Word8
ptr (Ptr Word8
ptr Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
`plusPtr` (Int
lenInt -> Int -> Int
forall a. Num a => a -> a -> a
-4)) String
xs
(Int, String) -> IO (Int, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
end Ptr Word8 -> Ptr Word8 -> Int
forall a b. Ptr a -> Ptr b -> Int
`minusPtr` Ptr Word8
ptr, String
xs')
go :: Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go :: Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go !Ptr Word8
ptr !Ptr Word8
end xs :: String
xs | Ptr Word8
ptr Ptr Word8 -> Ptr Word8 -> Bool
forall a. Ord a => a -> a -> Bool
> Ptr Word8
end = (Ptr Word8, String) -> IO (Ptr Word8, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
ptr, String
xs)
go !Ptr Word8
ptr !Ptr Word8
_ [] = (Ptr Word8, String) -> IO (Ptr Word8, String)
forall (m :: * -> *) a. Monad m => a -> m a
return (Ptr Word8
ptr, [])
go !Ptr Word8
ptr !Ptr Word8
end (x :: Char
x:xs :: String
xs)
| Char
x Char -> Char -> Bool
forall a. Ord a => a -> a -> Bool
<= '\x7f' = Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Char -> Word8
S.c2w Char
x) IO () -> IO (Ptr Word8, String) -> IO (Ptr Word8, String)
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
ptr 1) Ptr Word8
end String
xs
| Bool
otherwise = case Char -> Int
ord Char
x of
oc :: Int
oc | Int
oc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0x7ff -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0xc0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
oc Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 6)
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 1 (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0x80 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
oc Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3f
Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
ptr 2) Ptr Word8
end String
xs
| Int
oc Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= 0xffff -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0xe0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
oc Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 12)
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 1 (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0x80 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ((Int
oc Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3f)
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 2 (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0x80 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
oc Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3f
Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
ptr 3) Ptr Word8
end String
xs
| Bool
otherwise -> do
Ptr Word8 -> Word8 -> IO ()
forall a. Storable a => Ptr a -> a -> IO ()
poke Ptr Word8
ptr (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0xf0 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
oc Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 18)
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 1 (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0x80 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ((Int
oc Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 12) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3f)
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 2 (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0x80 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ((Int
oc Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` 6) Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3f)
Ptr Word8 -> Int -> Word8 -> IO ()
forall a. Storable a => Ptr a -> Int -> a -> IO ()
pokeElemOff Ptr Word8
ptr 3 (Word8 -> IO ()) -> Word8 -> IO ()
forall a b. (a -> b) -> a -> b
$ Int -> Word8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Word8) -> Int -> Word8
forall a b. (a -> b) -> a -> b
$ 0x80 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
oc Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. 0x3f
Ptr Word8 -> Ptr Word8 -> String -> IO (Ptr Word8, String)
go (Ptr Word8 -> Int -> Ptr Word8
forall a b. Ptr a -> Int -> Ptr b
plusPtr Ptr Word8
ptr 4) Ptr Word8
end String
xs
toString :: B.ByteString -> String
toString :: ByteString -> String
toString bs :: ByteString
bs = (Char -> String -> String) -> String -> ByteString -> String
forall a. (Char -> a -> a) -> a -> ByteString -> a
foldr (:) [] ByteString
bs
replacement_char :: Char
replacement_char :: Char
replacement_char = '\xfffd'
decode :: B.ByteString -> Maybe (Char,Int64)
decode :: ByteString -> Maybe (Char, Int64)
decode bs :: ByteString
bs = do (c :: Word8
c,cs :: ByteString
cs) <- ByteString -> Maybe (Word8, ByteString)
forall b s. UTF8Bytes b s => b -> Maybe (Word8, b)
buncons ByteString
bs
(Char, Int64) -> Maybe (Char, Int64)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ByteString -> (Char, Int64)
choose (Word8 -> Int
forall a. Enum a => a -> Int
fromEnum Word8
c) ByteString
cs)
where
choose :: Int -> B.ByteString -> (Char, Int64)
choose :: Int -> ByteString -> (Char, Int64)
choose c :: Int
c cs :: ByteString
cs
| Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0x80 = (Int -> Char
forall a. Enum a => Int -> a
toEnum (Int -> Char) -> Int -> Char
forall a b. (a -> b) -> a -> b
$ Int -> Int
forall a. Enum a => a -> Int
fromEnum Int
c, 1)
| Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0xc0 = (Char
replacement_char, 1)
| Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0xe0 = Int -> ByteString -> (Char, Int64)
bytes2 (Int -> Int -> Int
mask Int
c 0x1f) ByteString
cs
| Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0xf0 = Int -> ByteString -> (Char, Int64)
bytes3 (Int -> Int -> Int
mask Int
c 0x0f) ByteString
cs
| Int
c Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0xf8 = Int -> ByteString -> (Char, Int64)
bytes4 (Int -> Int -> Int
mask Int
c 0x07) ByteString
cs
| Bool
otherwise = (Char
replacement_char, 1)
mask :: Int -> Int -> Int
mask :: Int -> Int -> Int
mask c :: Int
c m :: Int
m = Int -> Int
forall a. Enum a => a -> Int
fromEnum (Int
c Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
m)
combine :: Int -> Word8 -> Int
combine :: Int -> Word8 -> Int
combine acc :: Int
acc r :: Word8
r = Int -> Int -> Int
forall a. Bits a => a -> Int -> a
shiftL Int
acc 6 Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Word8 -> Int
forall a. Enum a => a -> Int
fromEnum (Word8
r Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0x3f)
follower :: Int -> Word8 -> Maybe Int
follower :: Int -> Word8 -> Maybe Int
follower acc :: Int
acc r :: Word8
r | Word8
r Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. 0xc0 Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== 0x80 = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int -> Word8 -> Int
combine Int
acc Word8
r)
follower _ _ = Maybe Int
forall a. Maybe a
Nothing
{-# INLINE get_follower #-}
get_follower :: Int -> B.ByteString -> Maybe (Int, B.ByteString)
get_follower :: Int -> ByteString -> Maybe (Int, ByteString)
get_follower acc :: Int
acc cs :: ByteString
cs = do (x :: Word8
x,xs :: ByteString
xs) <- ByteString -> Maybe (Word8, ByteString)
forall b s. UTF8Bytes b s => b -> Maybe (Word8, b)
buncons ByteString
cs
Int
acc1 <- Int -> Word8 -> Maybe Int
follower Int
acc Word8
x
(Int, ByteString) -> Maybe (Int, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
acc1,ByteString
xs)
bytes2 :: Int -> B.ByteString -> (Char, Int64)
bytes2 :: Int -> ByteString -> (Char, Int64)
bytes2 c :: Int
c cs :: ByteString
cs = case Int -> ByteString -> Maybe (Int, ByteString)
get_follower Int
c ByteString
cs of
Just (d :: Int
d, _) | Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0x80 -> (Int -> Char
forall a. Enum a => Int -> a
toEnum Int
d, 2)
| Bool
otherwise -> (Char
replacement_char, 1)
_ -> (Char
replacement_char, 1)
bytes3 :: Int -> B.ByteString -> (Char, Int64)
bytes3 :: Int -> ByteString -> (Char, Int64)
bytes3 c :: Int
c cs :: ByteString
cs =
case Int -> ByteString -> Maybe (Int, ByteString)
get_follower Int
c ByteString
cs of
Just (d1 :: Int
d1, cs1 :: ByteString
cs1) ->
case Int -> ByteString -> Maybe (Int, ByteString)
get_follower Int
d1 ByteString
cs1 of
Just (d :: Int
d, _) | (Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0x800 Bool -> Bool -> Bool
&& Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0xd800) Bool -> Bool -> Bool
||
(Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> 0xdfff Bool -> Bool -> Bool
&& Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0xfffe) -> (Int -> Char
forall a. Enum a => Int -> a
toEnum Int
d, 3)
| Bool
otherwise -> (Char
replacement_char, 3)
_ -> (Char
replacement_char, 2)
_ -> (Char
replacement_char, 1)
bytes4 :: Int -> B.ByteString -> (Char, Int64)
bytes4 :: Int -> ByteString -> (Char, Int64)
bytes4 c :: Int
c cs :: ByteString
cs =
case Int -> ByteString -> Maybe (Int, ByteString)
get_follower Int
c ByteString
cs of
Just (d1 :: Int
d1, cs1 :: ByteString
cs1) ->
case Int -> ByteString -> Maybe (Int, ByteString)
get_follower Int
d1 ByteString
cs1 of
Just (d2 :: Int
d2, cs2 :: ByteString
cs2) ->
case Int -> ByteString -> Maybe (Int, ByteString)
get_follower Int
d2 ByteString
cs2 of
Just (d :: Int
d,_) | Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= 0x10000 Bool -> Bool -> Bool
&& Int
d Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< 0x110000 -> (Int -> Char
forall a. Enum a => Int -> a
toEnum Int
d, 4)
| Bool
otherwise -> (Char
replacement_char, 4)
_ -> (Char
replacement_char, 3)
_ -> (Char
replacement_char, 2)
_ -> (Char
replacement_char, 1)
{-# INLINE decode #-}
splitAt :: Int64 -> B.ByteString -> (B.ByteString,B.ByteString)
splitAt :: Int64 -> ByteString -> (ByteString, ByteString)
splitAt x :: Int64
x bs :: ByteString
bs = Int64 -> Int64 -> ByteString -> (ByteString, ByteString)
forall t.
(Ord t, Num t) =>
Int64 -> t -> ByteString -> (ByteString, ByteString)
loop 0 Int64
x ByteString
bs
where loop :: Int64 -> t -> ByteString -> (ByteString, ByteString)
loop !Int64
a n :: t
n _ | t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt Int64
a ByteString
bs
loop !Int64
a n :: t
n bs1 :: ByteString
bs1 = case ByteString -> Maybe (Char, Int64)
decode ByteString
bs1 of
Just (_,y :: Int64
y) -> Int64 -> t -> ByteString -> (ByteString, ByteString)
loop (Int64
aInt64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+Int64
y) (t
nt -> t -> t
forall a. Num a => a -> a -> a
-1) (Int64 -> ByteString -> ByteString
B.drop Int64
y ByteString
bs1)
Nothing -> (ByteString
bs, ByteString
B.empty)
take :: Int64 -> B.ByteString -> B.ByteString
take :: Int64 -> ByteString -> ByteString
take x :: Int64
x bs :: ByteString
bs = Int64 -> Int64 -> ByteString -> ByteString
forall t. (Ord t, Num t) => Int64 -> t -> ByteString -> ByteString
loop 0 Int64
x ByteString
bs
where loop :: Int64 -> t -> ByteString -> ByteString
loop !Int64
a n :: t
n _ | t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = Int64 -> ByteString -> ByteString
B.take Int64
a ByteString
bs
loop !Int64
a n :: t
n bs1 :: ByteString
bs1 = case ByteString -> Maybe (Char, Int64)
decode ByteString
bs1 of
Just (_,y :: Int64
y) -> Int64 -> t -> ByteString -> ByteString
loop (Int64
aInt64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+Int64
y) (t
nt -> t -> t
forall a. Num a => a -> a -> a
-1) (Int64 -> ByteString -> ByteString
B.drop Int64
y ByteString
bs1)
Nothing -> ByteString
bs
drop :: Int64 -> B.ByteString -> B.ByteString
drop :: Int64 -> ByteString -> ByteString
drop x :: Int64
x bs :: ByteString
bs = Int64 -> Int64 -> ByteString -> ByteString
forall t. (Ord t, Num t) => Int64 -> t -> ByteString -> ByteString
loop 0 Int64
x ByteString
bs
where loop :: Int64 -> t -> ByteString -> ByteString
loop !Int64
a n :: t
n _ | t
n t -> t -> Bool
forall a. Ord a => a -> a -> Bool
<= 0 = Int64 -> ByteString -> ByteString
B.drop Int64
a ByteString
bs
loop !Int64
a n :: t
n bs1 :: ByteString
bs1 = case ByteString -> Maybe (Char, Int64)
decode ByteString
bs1 of
Just (_,y :: Int64
y) -> Int64 -> t -> ByteString -> ByteString
loop (Int64
aInt64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+Int64
y) (t
nt -> t -> t
forall a. Num a => a -> a -> a
-1) (Int64 -> ByteString -> ByteString
B.drop Int64
y ByteString
bs1)
Nothing -> ByteString
B.empty
span :: (Char -> Bool) -> B.ByteString -> (B.ByteString, B.ByteString)
span :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
span p :: Char -> Bool
p bs :: ByteString
bs = Int64 -> ByteString -> (ByteString, ByteString)
loop 0 ByteString
bs
where loop :: Int64 -> ByteString -> (ByteString, ByteString)
loop a :: Int64
a cs :: ByteString
cs = case ByteString -> Maybe (Char, Int64)
decode ByteString
cs of
Just (c :: Char
c,n :: Int64
n) | Char -> Bool
p Char
c -> Int64 -> ByteString -> (ByteString, ByteString)
loop (Int64
aInt64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+Int64
n) (Int64 -> ByteString -> ByteString
B.drop Int64
n ByteString
cs)
_ -> Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt Int64
a ByteString
bs
break :: (Char -> Bool) -> B.ByteString -> (B.ByteString, B.ByteString)
break :: (Char -> Bool) -> ByteString -> (ByteString, ByteString)
break p :: Char -> Bool
p bs :: ByteString
bs = (Char -> Bool) -> ByteString -> (ByteString, ByteString)
span (Bool -> Bool
not (Bool -> Bool) -> (Char -> Bool) -> Char -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Char -> Bool
p) ByteString
bs
uncons :: B.ByteString -> Maybe (Char,B.ByteString)
uncons :: ByteString -> Maybe (Char, ByteString)
uncons bs :: ByteString
bs = do (c :: Char
c,n :: Int64
n) <- ByteString -> Maybe (Char, Int64)
decode ByteString
bs
(Char, ByteString) -> Maybe (Char, ByteString)
forall (m :: * -> *) a. Monad m => a -> m a
return (Char
c, Int64 -> ByteString -> ByteString
B.drop Int64
n ByteString
bs)
foldr :: (Char -> a -> a) -> a -> B.ByteString -> a
foldr :: (Char -> a -> a) -> a -> ByteString -> a
foldr cons :: Char -> a -> a
cons nil :: a
nil cs :: ByteString
cs = case ByteString -> Maybe (Char, ByteString)
uncons ByteString
cs of
Just (a :: Char
a,as :: ByteString
as) -> Char -> a -> a
cons Char
a ((Char -> a -> a) -> a -> ByteString -> a
forall a. (Char -> a -> a) -> a -> ByteString -> a
foldr Char -> a -> a
cons a
nil ByteString
as)
Nothing -> a
nil
foldl :: (a -> Char -> a) -> a -> B.ByteString -> a
foldl :: (a -> Char -> a) -> a -> ByteString -> a
foldl add :: a -> Char -> a
add acc :: a
acc cs :: ByteString
cs = case ByteString -> Maybe (Char, ByteString)
uncons ByteString
cs of
Just (a :: Char
a,as :: ByteString
as) -> let v :: a
v = a -> Char -> a
add a
acc Char
a
in a -> a -> a
forall a b. a -> b -> b
seq a
v ((a -> Char -> a) -> a -> ByteString -> a
forall a. (a -> Char -> a) -> a -> ByteString -> a
foldl a -> Char -> a
add a
v ByteString
as)
Nothing -> a
acc
length :: B.ByteString -> Int
length :: ByteString -> Int
length b :: ByteString
b = Int -> ByteString -> Int
forall p. Num p => p -> ByteString -> p
loop 0 ByteString
b
where loop :: p -> ByteString -> p
loop n :: p
n xs :: ByteString
xs = case ByteString -> Maybe (Char, Int64)
decode ByteString
xs of
Just (_,m :: Int64
m) -> p -> ByteString -> p
loop (p
np -> p -> p
forall a. Num a => a -> a -> a
+1) (Int64 -> ByteString -> ByteString
B.drop Int64
m ByteString
xs)
Nothing -> p
n
lines :: B.ByteString -> [B.ByteString]
lines :: ByteString -> [ByteString]
lines bs :: ByteString
bs | ByteString -> Bool
B.null ByteString
bs = []
lines bs :: ByteString
bs = case Word8 -> ByteString -> Maybe Int64
B.elemIndex 10 ByteString
bs of
Just x :: Int64
x -> let (xs :: ByteString
xs,ys :: ByteString
ys) = Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt Int64
x ByteString
bs
in ByteString
xs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
lines (ByteString -> ByteString
B.tail ByteString
ys)
Nothing -> [ByteString
bs]
lines' :: B.ByteString -> [B.ByteString]
lines' :: ByteString -> [ByteString]
lines' bs :: ByteString
bs | ByteString -> Bool
B.null ByteString
bs = []
lines' bs :: ByteString
bs = case Word8 -> ByteString -> Maybe Int64
B.elemIndex 10 ByteString
bs of
Just x :: Int64
x -> let (xs :: ByteString
xs,ys :: ByteString
ys) = Int64 -> ByteString -> (ByteString, ByteString)
B.splitAt (Int64
xInt64 -> Int64 -> Int64
forall a. Num a => a -> a -> a
+1) ByteString
bs
in ByteString
xs ByteString -> [ByteString] -> [ByteString]
forall a. a -> [a] -> [a]
: ByteString -> [ByteString]
lines' ByteString
ys
Nothing -> [ByteString
bs]
unsafeCreateUptoN' :: Int -> (Ptr Word8 -> IO (Int, a)) -> (S.ByteString, a)
unsafeCreateUptoN' :: Int -> (Ptr Word8 -> IO (Int, a)) -> (ByteString, a)
unsafeCreateUptoN' l :: Int
l f :: Ptr Word8 -> IO (Int, a)
f = IO (ByteString, a) -> (ByteString, a)
forall a. IO a -> a
unsafeDupablePerformIO (Int -> (Ptr Word8 -> IO (Int, a)) -> IO (ByteString, a)
forall a. Int -> (Ptr Word8 -> IO (Int, a)) -> IO (ByteString, a)
createUptoN' Int
l Ptr Word8 -> IO (Int, a)
f)
{-# INLINE unsafeCreateUptoN' #-}
createUptoN' :: Int -> (Ptr Word8 -> IO (Int, a)) -> IO (S.ByteString, a)
createUptoN' :: Int -> (Ptr Word8 -> IO (Int, a)) -> IO (ByteString, a)
createUptoN' l :: Int
l f :: Ptr Word8 -> IO (Int, a)
f = do
ForeignPtr Word8
fp <- Int -> IO (ForeignPtr Word8)
forall a. Int -> IO (ForeignPtr a)
S.mallocByteString Int
l
(l' :: Int
l', res :: a
res) <- ForeignPtr Word8 -> (Ptr Word8 -> IO (Int, a)) -> IO (Int, a)
forall a b. ForeignPtr a -> (Ptr a -> IO b) -> IO b
withForeignPtr ForeignPtr Word8
fp ((Ptr Word8 -> IO (Int, a)) -> IO (Int, a))
-> (Ptr Word8 -> IO (Int, a)) -> IO (Int, a)
forall a b. (a -> b) -> a -> b
$ \p :: Ptr Word8
p -> Ptr Word8 -> IO (Int, a)
f Ptr Word8
p
Bool -> IO (ByteString, a) -> IO (ByteString, a)
forall a. (?callStack::CallStack) => Bool -> a -> a
assert (Int
l' Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
l) (IO (ByteString, a) -> IO (ByteString, a))
-> IO (ByteString, a) -> IO (ByteString, a)
forall a b. (a -> b) -> a -> b
$ (ByteString, a) -> IO (ByteString, a)
forall (m :: * -> *) a. Monad m => a -> m a
return (ForeignPtr Word8 -> Int -> Int -> ByteString
S.PS ForeignPtr Word8
fp 0 Int
l', a
res)
{-# INLINE createUptoN' #-}