{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TupleSections #-}
{-# OPTIONS_GHC -fno-warn-name-shadowing -fexpose-all-unfoldings #-}
module System.OsString.Data.ByteString.Short.Word16 (
ShortByteString(..),
empty,
singleton,
pack,
unpack,
fromShort,
toShort,
snoc,
cons,
append,
last,
tail,
uncons,
uncons2,
head,
init,
unsnoc,
null,
length,
numWord16,
map,
reverse,
intercalate,
foldl,
foldl',
foldl1,
foldl1',
foldr,
foldr',
foldr1,
foldr1',
all,
any,
concat,
replicate,
unfoldr,
unfoldrN,
take,
takeEnd,
takeWhileEnd,
takeWhile,
drop,
dropEnd,
dropWhile,
dropWhileEnd,
breakEnd,
break,
span,
spanEnd,
splitAt,
split,
splitWith,
stripSuffix,
stripPrefix,
isInfixOf,
isPrefixOf,
isSuffixOf,
breakSubstring,
elem,
find,
filter,
partition,
index,
indexMaybe,
(!?),
elemIndex,
elemIndices,
count,
findIndex,
findIndices,
packCWString,
packCWStringLen,
newCWString,
useAsCWString,
useAsCWStringLen
)
where
import System.OsString.Data.ByteString.Short ( append, intercalate, concat, stripSuffix, stripPrefix, isPrefixOf, isSuffixOf, length, empty, null, ShortByteString(..), fromShort, toShort )
import System.OsString.Data.ByteString.Short.Internal
import Data.Bits
( shiftR
)
import Data.Word
import Prelude hiding
( Foldable(..)
, all
, any
, reverse
, break
, concat
, drop
, dropWhile
, filter
, head
, init
, last
, map
, replicate
, span
, splitAt
, tail
, take
, takeWhile
)
import qualified Data.Foldable as Foldable
import GHC.ST ( ST )
import GHC.Stack ( HasCallStack )
import GHC.Exts ( inline )
import qualified Data.ByteString.Short.Internal as BS
import qualified Data.List as List
singleton :: Word16 -> ShortByteString
singleton :: Word16 -> ShortByteString
singleton = \Word16
w -> Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
2 (\MBA s
mba -> MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
0 Word16
w)
pack :: [Word16] -> ShortByteString
pack :: [Word16] -> ShortByteString
pack = [Word16] -> ShortByteString
packWord16
unpack :: ShortByteString -> [Word16]
unpack :: ShortByteString -> [Word16]
unpack = ShortByteString -> [Word16]
unpackWord16 (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
numWord16 :: ShortByteString -> Int
numWord16 :: ShortByteString -> Int
numWord16 = (Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
1) (Int -> Int) -> (ShortByteString -> Int) -> ShortByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> Int
BS.length (ShortByteString -> Int)
-> (ShortByteString -> ShortByteString) -> ShortByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
infixr 5 `cons`
infixl 5 `snoc`
snoc :: ShortByteString -> Word16 -> ShortByteString
snoc :: ShortByteString -> Word16 -> ShortByteString
snoc = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) Word16
c -> let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2
in Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> do
BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
0 Int
l
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
l Word16
c
cons :: Word16 -> ShortByteString -> ShortByteString
cons :: Word16 -> ShortByteString -> ShortByteString
cons Word16
c = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2
in Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> do
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
0 Word16
c
BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
2 Int
l
last :: HasCallStack => ShortByteString -> Word16
last :: HasCallStack => ShortByteString -> Word16
last = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> case ShortByteString -> Bool
null ShortByteString
sbs of
Bool
True -> String -> Word16
forall a. HasCallStack => String -> a
errorEmptySBS String
"last"
Bool
False -> BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) (ShortByteString -> Int
BS.length ShortByteString
sbs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
tail :: HasCallStack => ShortByteString -> ShortByteString
tail :: HasCallStack => ShortByteString -> ShortByteString
tail = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
in if
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> String -> ShortByteString
forall a. HasCallStack => String -> a
errorEmptySBS String
"tail"
| Bool
otherwise -> Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
2 MBA s
mba Int
0 Int
nl
uncons :: ShortByteString -> Maybe (Word16, ShortByteString)
uncons :: ShortByteString -> Maybe (Word16, ShortByteString)
uncons = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
in if | Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> Maybe (Word16, ShortByteString)
forall a. Maybe a
Nothing
| Bool
otherwise -> let h :: Word16
h = BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0
t :: ShortByteString
t = Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
2 MBA s
mba Int
0 Int
nl
in (Word16, ShortByteString) -> Maybe (Word16, ShortByteString)
forall a. a -> Maybe a
Just (Word16
h, ShortByteString
t)
uncons2 :: ShortByteString -> Maybe (Word16, Word16, ShortByteString)
uncons2 :: ShortByteString -> Maybe (Word16, Word16, ShortByteString)
uncons2 = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4
in if | Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
2 -> Maybe (Word16, Word16, ShortByteString)
forall a. Maybe a
Nothing
| Bool
otherwise -> let h :: Word16
h = BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0
h' :: Word16
h' = BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) Int
2
t :: ShortByteString
t = Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
4 MBA s
mba Int
0 Int
nl
in (Word16, Word16, ShortByteString)
-> Maybe (Word16, Word16, ShortByteString)
forall a. a -> Maybe a
Just (Word16
h, Word16
h', ShortByteString
t)
head :: HasCallStack => ShortByteString -> Word16
head :: HasCallStack => ShortByteString -> Word16
head = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> case ShortByteString -> Bool
null ShortByteString
sbs of
Bool
True -> String -> Word16
forall a. HasCallStack => String -> a
errorEmptySBS String
"last"
Bool
False -> BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0
init :: HasCallStack => ShortByteString -> ShortByteString
init :: HasCallStack => ShortByteString -> ShortByteString
init = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
in if
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> String -> ShortByteString
forall a. HasCallStack => String -> a
errorEmptySBS String
"tail"
| Bool
otherwise -> Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
0 Int
nl
unsnoc :: ShortByteString -> Maybe (ShortByteString, Word16)
unsnoc :: ShortByteString -> Maybe (ShortByteString, Word16)
unsnoc = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2
in if | Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> Maybe (ShortByteString, Word16)
forall a. Maybe a
Nothing
| Bool
otherwise -> let l' :: Word16
l' = BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) (Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
i :: ShortByteString
i = Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
0 Int
nl
in (ShortByteString, Word16) -> Maybe (ShortByteString, Word16)
forall a. a -> Maybe a
Just (ShortByteString
i, Word16
l')
map :: (Word16 -> Word16) -> ShortByteString -> ShortByteString
map :: (Word16 -> Word16) -> ShortByteString -> ShortByteString
map Word16 -> Word16
f = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
in Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
l (\MBA s
mba -> BA -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> MBA s -> Int -> Int -> ST s ()
go BA
ba MBA s
mba Int
0 Int
l)
where
go :: BA -> MBA s -> Int -> Int -> ST s ()
go :: forall s. BA -> MBA s -> Int -> Int -> ST s ()
go !BA
ba !MBA s
mba !Int
i !Int
l
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do
let w :: Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba Int
i
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
i (Word16 -> Word16
f Word16
w)
BA -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> MBA s -> Int -> Int -> ST s ()
go BA
ba MBA s
mba (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) Int
l
reverse :: ShortByteString -> ShortByteString
reverse :: ShortByteString -> ShortByteString
reverse = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
in Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
l (\MBA s
mba -> BA -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> MBA s -> Int -> Int -> ST s ()
go BA
ba MBA s
mba Int
0 Int
l)
where
go :: BA -> MBA s -> Int -> Int -> ST s ()
go :: forall s. BA -> MBA s -> Int -> Int -> ST s ()
go !BA
ba !MBA s
mba !Int
i !Int
l
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
| Bool
otherwise = do
let w :: Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba Int
i
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba (Int
l Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) Word16
w
BA -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> MBA s -> Int -> Int -> ST s ()
go BA
ba MBA s
mba (Int
iInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) Int
l
all :: (Word16 -> Bool) -> ShortByteString -> Bool
all :: (Word16 -> Bool) -> ShortByteString -> Bool
all Word16 -> Bool
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
w :: Int -> Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba
go :: Int -> Bool
go !Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = Bool
True
| Bool
otherwise = Word16 -> Bool
k (Int -> Word16
w Int
n) Bool -> Bool -> Bool
&& Int -> Bool
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
in Int -> Bool
go Int
0
any :: (Word16 -> Bool) -> ShortByteString -> Bool
any :: (Word16 -> Bool) -> ShortByteString -> Bool
any Word16 -> Bool
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
w :: Int -> Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba
go :: Int -> Bool
go !Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = Bool
False
| Bool
otherwise = Word16 -> Bool
k (Int -> Word16
w Int
n) Bool -> Bool -> Bool
|| Int -> Bool
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
in Int -> Bool
go Int
0
replicate :: Int -> Word16 -> ShortByteString
replicate :: Int -> Word16 -> ShortByteString
replicate Int
w Word16
c
| Int
w Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ShortByteString
empty
| Bool
otherwise = Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create (Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) (MBA s -> Int -> ST s ()
forall {s}. MBA s -> Int -> ST s ()
`go` Int
0)
where
go :: MBA s -> Int -> ST s ()
go MBA s
mba Int
ix
| Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 Bool -> Bool -> Bool
|| Int
ix Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2 = () -> ST s ()
forall a. a -> ST s a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
| Bool
otherwise = MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
ix Word16
c ST s () -> ST s () -> ST s ()
forall a b. ST s a -> ST s b -> ST s b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> MBA s -> Int -> ST s ()
go MBA s
mba (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
unfoldr :: (a -> Maybe (Word16, a)) -> a -> ShortByteString
unfoldr :: forall a. (a -> Maybe (Word16, a)) -> a -> ShortByteString
unfoldr a -> Maybe (Word16, a)
f a
x0 = [Word16] -> ShortByteString
packWord16Rev ([Word16] -> ShortByteString) -> [Word16] -> ShortByteString
forall a b. (a -> b) -> a -> b
$ a -> [Word16] -> [Word16]
go a
x0 [Word16]
forall a. Monoid a => a
mempty
where
go :: a -> [Word16] -> [Word16]
go a
x [Word16]
words' = case a -> Maybe (Word16, a)
f a
x of
Maybe (Word16, a)
Nothing -> [Word16]
words'
Just (Word16
w, a
x') -> a -> [Word16] -> [Word16]
go a
x' (Word16
wWord16 -> [Word16] -> [Word16]
forall a. a -> [a] -> [a]
:[Word16]
words')
unfoldrN :: forall a.
Int
-> (a -> Maybe (Word16, a))
-> a
-> (ShortByteString, Maybe a)
unfoldrN :: forall a.
Int -> (a -> Maybe (Word16, a)) -> a -> (ShortByteString, Maybe a)
unfoldrN Int
i a -> Maybe (Word16, a)
f = \a
x0 ->
if | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
0 -> (ShortByteString
empty, a -> Maybe a
forall a. a -> Maybe a
Just a
x0)
| Bool
otherwise -> Int
-> (forall s. MBA s -> ST s (Int, Maybe a))
-> (ShortByteString, Maybe a)
forall a.
Int -> (forall s. MBA s -> ST s (Int, a)) -> (ShortByteString, a)
createAndTrim (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) ((forall s. MBA s -> ST s (Int, Maybe a))
-> (ShortByteString, Maybe a))
-> (forall s. MBA s -> ST s (Int, Maybe a))
-> (ShortByteString, Maybe a)
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> MBA s -> a -> Int -> ST s (Int, Maybe a)
forall s. MBA s -> a -> Int -> ST s (Int, Maybe a)
go MBA s
mba a
x0 Int
0
where
go :: forall s. MBA s -> a -> Int -> ST s (Int, Maybe a)
go :: forall s. MBA s -> a -> Int -> ST s (Int, Maybe a)
go !MBA s
mba !a
x !Int
n = a -> Int -> ST s (Int, Maybe a)
go' a
x Int
n
where
go' :: a -> Int -> ST s (Int, Maybe a)
go' :: a -> Int -> ST s (Int, Maybe a)
go' !a
x' !Int
n'
| Int
n' Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2 = (Int, Maybe a) -> ST s (Int, Maybe a)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n', a -> Maybe a
forall a. a -> Maybe a
Just a
x')
| Bool
otherwise = case a -> Maybe (Word16, a)
f a
x' of
Maybe (Word16, a)
Nothing -> (Int, Maybe a) -> ST s (Int, Maybe a)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
n', Maybe a
forall a. Maybe a
Nothing)
Just (Word16
w, a
x'') -> do
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
n' Word16
w
a -> Int -> ST s (Int, Maybe a)
go' a
x'' (Int
n'Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2)
take :: Int
-> ShortByteString
-> ShortByteString
take :: Int -> ShortByteString -> ShortByteString
take = \Int
n (ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let sl :: Int
sl = ShortByteString -> Int
numWord16 ShortByteString
sbs
len8 :: Int
len8 = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
in if | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sl -> ShortByteString
sbs
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> ShortByteString
empty
| Bool
otherwise ->
Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
len8 ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
0 Int
len8
takeEnd :: Int
-> ShortByteString
-> ShortByteString
takeEnd :: Int -> ShortByteString -> ShortByteString
takeEnd Int
n = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let sl :: Int
sl = ShortByteString -> Int
BS.length ShortByteString
sbs
n2 :: Int
n2 = Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
in if | Int
n2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sl -> ShortByteString
sbs
| Int
n2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> ShortByteString
empty
| Bool
otherwise -> Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
n2 ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
sl Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n2)) MBA s
mba Int
0 Int
n2
takeWhile :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
takeWhile :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
takeWhile Word16 -> Bool
f ShortByteString
ps = Int -> ShortByteString -> ShortByteString
take ((Word16 -> Bool) -> ShortByteString -> Int
findIndexOrLength (Bool -> Bool
not (Bool -> Bool) -> (Word16 -> Bool) -> Word16 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Bool
f) ShortByteString
ps) ShortByteString
ps
takeWhileEnd :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
takeWhileEnd :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
takeWhileEnd Word16 -> Bool
f ShortByteString
ps = Int -> ShortByteString -> ShortByteString
drop ((Word16 -> Bool) -> ShortByteString -> Int
findFromEndUntil (Bool -> Bool
not (Bool -> Bool) -> (Word16 -> Bool) -> Word16 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Bool
f) ShortByteString
ps) ShortByteString
ps
drop :: Int
-> ShortByteString
-> ShortByteString
drop :: Int -> ShortByteString -> ShortByteString
drop = \Int
n' (ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let len :: Int
len = ShortByteString -> Int
BS.length ShortByteString
sbs
n :: Int
n = Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
in if | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> ShortByteString
sbs
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
len -> ShortByteString
empty
| Bool
otherwise ->
let newLen :: Int
newLen = Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n
in Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
newLen ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
n MBA s
mba Int
0 Int
newLen
dropEnd :: Int
-> ShortByteString
-> ShortByteString
dropEnd :: Int -> ShortByteString -> ShortByteString
dropEnd Int
n' = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let sl :: Int
sl = ShortByteString -> Int
BS.length ShortByteString
sbs
nl :: Int
nl = Int
sl Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n
n :: Int
n = Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
in if | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
sl -> ShortByteString
empty
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> ShortByteString
sbs
| Bool
otherwise -> Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
nl ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
0 Int
nl
dropWhile :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
dropWhile :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
dropWhile Word16 -> Bool
f = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
ps) -> Int -> ShortByteString -> ShortByteString
drop ((Word16 -> Bool) -> ShortByteString -> Int
findIndexOrLength (Bool -> Bool
not (Bool -> Bool) -> (Word16 -> Bool) -> Word16 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Bool
f) ShortByteString
ps) ShortByteString
ps
dropWhileEnd :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
dropWhileEnd :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
dropWhileEnd Word16 -> Bool
f = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
ps) -> Int -> ShortByteString -> ShortByteString
take ((Word16 -> Bool) -> ShortByteString -> Int
findFromEndUntil (Bool -> Bool
not (Bool -> Bool) -> (Word16 -> Bool) -> Word16 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Bool
f) ShortByteString
ps) ShortByteString
ps
breakEnd :: (Word16 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)
breakEnd :: (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
breakEnd Word16 -> Bool
p = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> Int -> ShortByteString -> (ShortByteString, ShortByteString)
splitAt ((Word16 -> Bool) -> ShortByteString -> Int
findFromEndUntil Word16 -> Bool
p ShortByteString
sbs) ShortByteString
sbs
break :: (Word16 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)
break :: (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
break = \Word16 -> Bool
p (ShortByteString -> ShortByteString
assertEven -> ShortByteString
ps) -> case (Word16 -> Bool) -> ShortByteString -> Int
findIndexOrLength Word16 -> Bool
p ShortByteString
ps of Int
n -> Int -> ShortByteString -> (ShortByteString, ShortByteString)
splitAt Int
n ShortByteString
ps
span :: (Word16 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)
span :: (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
span Word16 -> Bool
p = (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
break (Bool -> Bool
not (Bool -> Bool) -> (Word16 -> Bool) -> Word16 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> Bool
p) (ShortByteString -> (ShortByteString, ShortByteString))
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> (ShortByteString, ShortByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
spanEnd :: (Word16 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)
spanEnd :: (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
spanEnd Word16 -> Bool
p = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
ps) -> Int -> ShortByteString -> (ShortByteString, ShortByteString)
splitAt ((Word16 -> Bool) -> ShortByteString -> Int
findFromEndUntil (Bool -> Bool
not(Bool -> Bool) -> (Word16 -> Bool) -> Word16 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
.Word16 -> Bool
p) ShortByteString
ps) ShortByteString
ps
splitAt :: Int
-> ShortByteString
-> (ShortByteString, ShortByteString)
splitAt :: Int -> ShortByteString -> (ShortByteString, ShortByteString)
splitAt Int
n' = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> if
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> (ShortByteString
empty, ShortByteString
sbs)
| Bool
otherwise ->
let slen :: Int
slen = ShortByteString -> Int
BS.length ShortByteString
sbs
in if | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ShortByteString -> Int
BS.length ShortByteString
sbs -> (ShortByteString
sbs, ShortByteString
empty)
| Bool
otherwise ->
let llen :: Int
llen = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
slen (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n)
rlen :: Int
rlen = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
slen Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n)
lsbs :: ShortByteString
lsbs = Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
llen ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0 MBA s
mba Int
0 Int
llen
rsbs :: ShortByteString
rsbs = Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create Int
rlen ((forall s. MBA s -> ST s ()) -> ShortByteString)
-> (forall s. MBA s -> ST s ()) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> BA -> Int -> MBA s -> Int -> Int -> ST s ()
forall s. BA -> Int -> MBA s -> Int -> Int -> ST s ()
copyByteArray (ShortByteString -> BA
asBA ShortByteString
sbs) Int
n MBA s
mba Int
0 Int
rlen
in (ShortByteString
lsbs, ShortByteString
rsbs)
where
n :: Int
n = Int
n' Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
split :: Word16 -> ShortByteString -> [ShortByteString]
split :: Word16 -> ShortByteString -> [ShortByteString]
split Word16
w = (Word16 -> Bool) -> ShortByteString -> [ShortByteString]
splitWith (Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
== Word16
w) (ShortByteString -> [ShortByteString])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [ShortByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
splitWith :: (Word16 -> Bool) -> ShortByteString -> [ShortByteString]
splitWith :: (Word16 -> Bool) -> ShortByteString -> [ShortByteString]
splitWith Word16 -> Bool
p = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> if
| ShortByteString -> Bool
BS.null ShortByteString
sbs -> []
| Bool
otherwise -> ShortByteString -> [ShortByteString]
go ShortByteString
sbs
where
go :: ShortByteString -> [ShortByteString]
go ShortByteString
sbs'
| ShortByteString -> Bool
BS.null ShortByteString
sbs' = [ShortByteString
forall a. Monoid a => a
mempty]
| Bool
otherwise =
case (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
break Word16 -> Bool
p ShortByteString
sbs' of
(ShortByteString
a, ShortByteString
b)
| ShortByteString -> Bool
BS.null ShortByteString
b -> [ShortByteString
a]
| Bool
otherwise -> ShortByteString
a ShortByteString -> [ShortByteString] -> [ShortByteString]
forall a. a -> [a] -> [a]
: ShortByteString -> [ShortByteString]
go (HasCallStack => ShortByteString -> ShortByteString
ShortByteString -> ShortByteString
tail ShortByteString
b)
isInfixOf :: ShortByteString -> ShortByteString -> Bool
isInfixOf :: ShortByteString -> ShortByteString -> Bool
isInfixOf ShortByteString
sbs = \ShortByteString
s -> ShortByteString -> Bool
null ShortByteString
sbs Bool -> Bool -> Bool
|| Bool -> Bool
not (ShortByteString -> Bool
null (ShortByteString -> Bool) -> ShortByteString -> Bool
forall a b. (a -> b) -> a -> b
$ (ShortByteString, ShortByteString) -> ShortByteString
forall a b. (a, b) -> b
snd ((ShortByteString, ShortByteString) -> ShortByteString)
-> (ShortByteString, ShortByteString) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ (ShortByteString
-> ShortByteString -> (ShortByteString, ShortByteString))
-> ShortByteString
-> ShortByteString
-> (ShortByteString, ShortByteString)
forall a. a -> a
GHC.Exts.inline ShortByteString
-> ShortByteString -> (ShortByteString, ShortByteString)
breakSubstring ShortByteString
sbs ShortByteString
s)
breakSubstring :: ShortByteString
-> ShortByteString
-> (ShortByteString, ShortByteString)
breakSubstring :: ShortByteString
-> ShortByteString -> (ShortByteString, ShortByteString)
breakSubstring bPat :: ShortByteString
bPat@(ShortByteString -> BA
asBA -> BA
pat) bInp :: ShortByteString
bInp@(ShortByteString -> BA
asBA -> BA
inp) = Int -> (ShortByteString, ShortByteString)
go Int
0
where
lpat :: Int
lpat = ShortByteString -> Int
BS.length ShortByteString
bPat
linp :: Int
linp = ShortByteString -> Int
BS.length ShortByteString
bInp
go :: Int -> (ShortByteString, ShortByteString)
go Int
ix
| let ix' :: Int
ix' = Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
, Int
linp Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
ix' Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lpat =
if | BA -> Int -> BA -> Int -> Int -> Int
compareByteArraysOff BA
pat Int
0 BA
inp Int
ix' Int
lpat Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
0 -> Int -> ShortByteString -> (ShortByteString, ShortByteString)
splitAt Int
ix ShortByteString
bInp
| Bool
otherwise -> Int -> (ShortByteString, ShortByteString)
go (Int
ix Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
| Bool
otherwise
= (ShortByteString
bInp, ShortByteString
forall a. Monoid a => a
mempty)
foldl :: (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl :: forall a. (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl a -> Word16 -> a
f a
v = (a -> Word16 -> a) -> a -> [Word16] -> a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl a -> Word16 -> a
f a
v ([Word16] -> a)
-> (ShortByteString -> [Word16]) -> ShortByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldl' :: (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl' :: forall a. (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl' a -> Word16 -> a
f a
v = (a -> Word16 -> a) -> a -> [Word16] -> a
forall b a. (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
List.foldl' a -> Word16 -> a
f a
v ([Word16] -> a)
-> (ShortByteString -> [Word16]) -> ShortByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldr :: (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr :: forall a. (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr Word16 -> a -> a
f a
v = (Word16 -> a -> a) -> a -> [Word16] -> a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
List.foldr Word16 -> a -> a
f a
v ([Word16] -> a)
-> (ShortByteString -> [Word16]) -> ShortByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldr' :: (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr' :: forall a. (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr' Word16 -> a -> a
k a
v = (Word16 -> a -> a) -> a -> [Word16] -> a
forall a b. (a -> b -> b) -> b -> [a] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
Foldable.foldr' Word16 -> a -> a
k a
v ([Word16] -> a)
-> (ShortByteString -> [Word16]) -> ShortByteString -> a
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldl1 :: HasCallStack => (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1 :: HasCallStack =>
(Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1 Word16 -> Word16 -> Word16
k = (Word16 -> Word16 -> Word16) -> [Word16] -> Word16
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
List.foldl1 Word16 -> Word16 -> Word16
k ([Word16] -> Word16)
-> (ShortByteString -> [Word16]) -> ShortByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldl1' :: HasCallStack => (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1' :: HasCallStack =>
(Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1' Word16 -> Word16 -> Word16
k = (Word16 -> Word16 -> Word16) -> [Word16] -> Word16
forall a. HasCallStack => (a -> a -> a) -> [a] -> a
List.foldl1' Word16 -> Word16 -> Word16
k ([Word16] -> Word16)
-> (ShortByteString -> [Word16]) -> ShortByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldr1 :: HasCallStack => (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1 :: HasCallStack =>
(Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1 Word16 -> Word16 -> Word16
k = (Word16 -> Word16 -> Word16) -> [Word16] -> Word16
forall a. (a -> a -> a) -> [a] -> a
forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
List.foldr1 Word16 -> Word16 -> Word16
k ([Word16] -> Word16)
-> (ShortByteString -> [Word16]) -> ShortByteString -> Word16
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> [Word16]
unpack (ShortByteString -> [Word16])
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> [Word16]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
foldr1' :: HasCallStack => (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1' :: HasCallStack =>
(Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1' Word16 -> Word16 -> Word16
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> if ShortByteString -> Bool
null ShortByteString
sbs then String -> Word16
forall a. HasCallStack => String -> a
errorEmptySBS String
"foldr1'" else (Word16 -> Word16 -> Word16) -> Word16 -> ShortByteString -> Word16
forall a. (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr' Word16 -> Word16 -> Word16
k (HasCallStack => ShortByteString -> Word16
ShortByteString -> Word16
last ShortByteString
sbs) (HasCallStack => ShortByteString -> ShortByteString
ShortByteString -> ShortByteString
init ShortByteString
sbs)
index :: HasCallStack
=> ShortByteString
-> Int
-> Word16
index :: HasCallStack => ShortByteString -> Int -> Word16
index = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) Int
i -> if
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ShortByteString -> Int
numWord16 ShortByteString
sbs -> ShortByteString -> Int -> Word16
unsafeIndex ShortByteString
sbs Int
i
| Bool
otherwise -> ShortByteString -> Int -> Word16
forall a. HasCallStack => ShortByteString -> Int -> a
indexError ShortByteString
sbs Int
i
indexMaybe :: ShortByteString
-> Int
-> Maybe Word16
indexMaybe :: ShortByteString -> Int -> Maybe Word16
indexMaybe = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) Int
i -> if
| Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
0 Bool -> Bool -> Bool
&& Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< ShortByteString -> Int
numWord16 ShortByteString
sbs -> Word16 -> Maybe Word16
forall a. a -> Maybe a
Just (Word16 -> Maybe Word16) -> Word16 -> Maybe Word16
forall a b. (a -> b) -> a -> b
$! ShortByteString -> Int -> Word16
unsafeIndex ShortByteString
sbs Int
i
| Bool
otherwise -> Maybe Word16
forall a. Maybe a
Nothing
{-# INLINE indexMaybe #-}
unsafeIndex :: ShortByteString
-> Int
-> Word16
unsafeIndex :: ShortByteString -> Int -> Word16
unsafeIndex ShortByteString
sbs Int
i = BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
indexError :: HasCallStack => ShortByteString -> Int -> a
indexError :: forall a. HasCallStack => ShortByteString -> Int -> a
indexError ShortByteString
sbs Int
i =
String -> String -> a
forall a. HasCallStack => String -> String -> a
moduleError String
"index" (String -> a) -> String -> a
forall a b. (a -> b) -> a -> b
$ String
"error in array index: " String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show Int
i
String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" not in range [0.." String -> String -> String
forall a. [a] -> [a] -> [a]
++ Int -> String
forall a. Show a => a -> String
show (ShortByteString -> Int
numWord16 ShortByteString
sbs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"]"
(!?) :: ShortByteString
-> Int
-> Maybe Word16
!? :: ShortByteString -> Int -> Maybe Word16
(!?) = ShortByteString -> Int -> Maybe Word16
indexMaybe
{-# INLINE (!?) #-}
elem :: Word16 -> ShortByteString -> Bool
elem :: Word16 -> ShortByteString -> Bool
elem Word16
c = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> case Word16 -> ShortByteString -> Maybe Int
elemIndex Word16
c ShortByteString
sbs of Maybe Int
Nothing -> Bool
False ; Maybe Int
_ -> Bool
True
filter :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
filter :: (Word16 -> Bool) -> ShortByteString -> ShortByteString
filter Word16 -> Bool
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
in if | Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> ShortByteString
sbs
| Bool
otherwise -> Int -> (forall s. MBA s -> ST s Int) -> ShortByteString
createAndTrim' Int
l ((forall s. MBA s -> ST s Int) -> ShortByteString)
-> (forall s. MBA s -> ST s Int) -> ShortByteString
forall a b. (a -> b) -> a -> b
$ \MBA s
mba -> MBA s -> BA -> Int -> ST s Int
forall s. MBA s -> BA -> Int -> ST s Int
go MBA s
mba (ShortByteString -> BA
asBA ShortByteString
sbs) Int
l
where
go :: forall s. MBA s
-> BA
-> Int
-> ST s Int
go :: forall s. MBA s -> BA -> Int -> ST s Int
go !MBA s
mba BA
ba !Int
l = Int -> Int -> ST s Int
go' Int
0 Int
0
where
go' :: Int
-> Int
-> ST s Int
go' :: Int -> Int -> ST s Int
go' !Int
br !Int
bw
| Int
br Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = Int -> ST s Int
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return Int
bw
| Bool
otherwise = do
let w :: Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba Int
br
if Word16 -> Bool
k Word16
w
then do
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba Int
bw Word16
w
Int -> Int -> ST s Int
go' (Int
brInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) (Int
bwInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2)
else
Int -> Int -> ST s Int
go' (Int
brInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) Int
bw
find :: (Word16 -> Bool) -> ShortByteString -> Maybe Word16
find :: (Word16 -> Bool) -> ShortByteString -> Maybe Word16
find Word16 -> Bool
f = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> case (Word16 -> Bool) -> ShortByteString -> Maybe Int
findIndex Word16 -> Bool
f ShortByteString
sbs of
Just Int
n -> Word16 -> Maybe Word16
forall a. a -> Maybe a
Just (ShortByteString
sbs HasCallStack => ShortByteString -> Int -> Word16
ShortByteString -> Int -> Word16
`index` Int
n)
Maybe Int
_ -> Maybe Word16
forall a. Maybe a
Nothing
partition :: (Word16 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)
partition :: (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
partition Word16 -> Bool
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
in if | Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> (ShortByteString
sbs, ShortByteString
sbs)
| Bool
otherwise -> Int
-> (forall s. MBA s -> MBA s -> ST s (Int, Int))
-> (ShortByteString, ShortByteString)
createAndTrim'' Int
l ((forall s. MBA s -> MBA s -> ST s (Int, Int))
-> (ShortByteString, ShortByteString))
-> (forall s. MBA s -> MBA s -> ST s (Int, Int))
-> (ShortByteString, ShortByteString)
forall a b. (a -> b) -> a -> b
$ \MBA s
mba1 MBA s
mba2 -> MBA s -> MBA s -> BA -> Int -> ST s (Int, Int)
forall s. MBA s -> MBA s -> BA -> Int -> ST s (Int, Int)
go MBA s
mba1 MBA s
mba2 (ShortByteString -> BA
asBA ShortByteString
sbs) Int
l
where
go :: forall s.
MBA s
-> MBA s
-> BA
-> Int
-> ST s (Int, Int)
go :: forall s. MBA s -> MBA s -> BA -> Int -> ST s (Int, Int)
go !MBA s
mba1 !MBA s
mba2 BA
ba !Int
l = Int -> Int -> ST s (Int, Int)
go' Int
0 Int
0
where
go' :: Int
-> Int
-> ST s (Int, Int)
go' :: Int -> Int -> ST s (Int, Int)
go' !Int
br !Int
bw1
| Int
br Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = (Int, Int) -> ST s (Int, Int)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Int
bw1, Int
br Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
bw1)
| Bool
otherwise = do
let w :: Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba Int
br
if Word16 -> Bool
k Word16
w
then do
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba1 Int
bw1 Word16
w
Int -> Int -> ST s (Int, Int)
go' (Int
brInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) (Int
bw1Int -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2)
else do
MBA s -> Int -> Word16 -> ST s ()
forall s. MBA s -> Int -> Word16 -> ST s ()
writeWord16Array MBA s
mba2 (Int
br Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
bw1) Word16
w
Int -> Int -> ST s (Int, Int)
go' (Int
brInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
2) Int
bw1
elemIndex :: Word16
-> ShortByteString
-> Maybe Int
elemIndex :: Word16 -> ShortByteString -> Maybe Int
elemIndex Word16
k = (Word16 -> Bool) -> ShortByteString -> Maybe Int
findIndex (Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
==Word16
k) (ShortByteString -> Maybe Int)
-> (ShortByteString -> ShortByteString)
-> ShortByteString
-> Maybe Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
elemIndices :: Word16 -> ShortByteString -> [Int]
elemIndices :: Word16 -> ShortByteString -> [Int]
elemIndices Word16
k = (Word16 -> Bool) -> ShortByteString -> [Int]
findIndices (Word16 -> Word16 -> Bool
forall a. Eq a => a -> a -> Bool
==Word16
k) (ShortByteString -> [Int])
-> (ShortByteString -> ShortByteString) -> ShortByteString -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
count :: Word16 -> ShortByteString -> Int
count :: Word16 -> ShortByteString -> Int
count Word16
w = [Int] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
List.length ([Int] -> Int)
-> (ShortByteString -> [Int]) -> ShortByteString -> Int
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word16 -> ShortByteString -> [Int]
elemIndices Word16
w (ShortByteString -> [Int])
-> (ShortByteString -> ShortByteString) -> ShortByteString -> [Int]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ShortByteString -> ShortByteString
assertEven
findIndex :: (Word16 -> Bool) -> ShortByteString -> Maybe Int
findIndex :: (Word16 -> Bool) -> ShortByteString -> Maybe Int
findIndex Word16 -> Bool
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
w :: Int -> Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba
go :: Int -> Maybe Int
go !Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = Maybe Int
forall a. Maybe a
Nothing
| Word16 -> Bool
k (Int -> Word16
w Int
n) = Int -> Maybe Int
forall a. a -> Maybe a
Just (Int
n Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
1)
| Bool
otherwise = Int -> Maybe Int
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
in Int -> Maybe Int
go Int
0
findIndices :: (Word16 -> Bool) -> ShortByteString -> [Int]
findIndices :: (Word16 -> Bool) -> ShortByteString -> [Int]
findIndices Word16 -> Bool
k = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let l :: Int
l = ShortByteString -> Int
BS.length ShortByteString
sbs
ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
w :: Int -> Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba
go :: Int -> [Int]
go !Int
n | Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
l = []
| Word16 -> Bool
k (Int -> Word16
w Int
n) = (Int
n Int -> Int -> Int
forall a. Bits a => a -> Int -> a
`shiftR` Int
1) Int -> [Int] -> [Int]
forall a. a -> [a] -> [a]
: Int -> [Int]
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
| Bool
otherwise = Int -> [Int]
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
in Int -> [Int]
go Int
0