{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE PackageImports #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE ViewPatterns #-}
module Data.ByteString.Short.Word16 (
ShortByteString,
empty,
singleton,
pack,
unpack,
fromShort,
toShort,
snoc,
cons,
append,
last,
tail,
head,
init,
null,
length,
map,
reverse,
intercalate,
foldl,
foldl',
foldl1,
foldl1',
foldr,
foldr',
foldr1,
foldr1',
all,
any,
concat,
replicate,
unfoldr,
unfoldrN,
take,
takeEnd,
takeWhile,
takeWhileEnd,
drop,
dropEnd,
dropWhile,
dropWhileEnd,
breakEnd,
break,
span,
spanEnd,
splitAt,
split,
splitWith,
stripSuffix,
stripPrefix,
isInfixOf,
isPrefixOf,
isSuffixOf,
elem,
find,
filter,
partition,
index,
indexMaybe,
(!?),
elemIndex,
elemIndices,
count,
findIndex,
findIndices,
packCWString,
packCWStringLen,
newCWString,
useAsCWString,
useAsCWStringLen
)
where
import Data.ByteString.Short
( append, intercalate, isInfixOf, isPrefixOf, isSuffixOf, stripSuffix, stripPrefix, fromShort, toShort, concat )
import Data.ByteString.Short.Internal
import Data.Word16
#if !MIN_VERSION_base(4,13,0)
import Data.Monoid ((<>))
#endif
import Data.Bifunctor
( first, bimap )
import Prelude hiding
( all
, any
, reverse
, break
, concat
, drop
, dropWhile
, elem
, filter
, foldl
, foldl1
, foldr
, foldr1
, head
, init
, last
, length
, map
, null
, replicate
, span
, splitAt
, tail
, take
, takeWhile
)
import GHC.List (errorEmptyList)
import qualified Data.Foldable as Foldable
import Data.ByteString.Short
( ShortByteString
, empty
, null
)
import GHC.Exts
import GHC.ST ( ST )
import qualified "bytestring" 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)
{-# INLINE [1] singleton #-}
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
length :: ShortByteString -> Int
length :: ShortByteString -> Int
length = (Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) (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
{-# INLINE snoc #-}
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
{-# INLINE cons #-}
last :: ShortByteString -> Word16
last :: ShortByteString -> Word16
last = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
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)
{-# INLINE last #-}
tail :: ShortByteString -> ShortByteString
tail :: 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 -> ShortByteString
sbs
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
2 -> ShortByteString
empty
| 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
{-# INLINE tail #-}
head :: ShortByteString -> Word16
head :: ShortByteString -> Word16
head = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) -> BA -> Int -> Word16
indexWord16Array (ShortByteString -> BA
asBA ShortByteString
sbs) Int
0
{-# INLINE head #-}
init :: ShortByteString -> ShortByteString
init :: 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 -> ShortByteString
sbs
| Int
l Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
2 -> ShortByteString
empty
| 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
{-# INLINE init #-}
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 :: 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 (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 :: 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 (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
{-# INLINE [1] any #-}
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
mba -> MBA s -> Int -> ST s ()
forall s. MBA s -> Int -> ST s ()
go MBA s
mba 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 (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 (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)
{-# INLINE replicate #-}
unfoldr :: (a -> Maybe (Word16, a)) -> a -> ShortByteString
unfoldr :: (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')
{-# INLINE unfoldr #-}
unfoldrN :: Int -> (a -> Maybe (Word16, a)) -> a -> (ShortByteString, Maybe a)
unfoldrN :: Int -> (a -> Maybe (Word16, a)) -> a -> (ShortByteString, Maybe a)
unfoldrN Int
i a -> Maybe (Word16, a)
f a
x0 = ([Word16] -> ShortByteString)
-> ([Word16], Maybe a) -> (ShortByteString, Maybe a)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first [Word16] -> ShortByteString
packWord16Rev (([Word16], Maybe a) -> (ShortByteString, Maybe a))
-> ([Word16], Maybe a) -> (ShortByteString, Maybe a)
forall a b. (a -> b) -> a -> b
$ Int -> a -> [Word16] -> ([Word16], Maybe a)
forall t.
(Ord t, Num t) =>
t -> a -> [Word16] -> ([Word16], Maybe a)
go (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) a
x0 [Word16]
forall a. Monoid a => a
mempty
where
go :: t -> a -> [Word16] -> ([Word16], Maybe a)
go t
i' a
x [Word16]
words'
| t
i' t -> t -> Bool
forall a. Ord a => a -> a -> Bool
< t
0 = ([Word16]
words', 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 -> ([Word16]
words', Maybe a
forall a. Maybe a
Nothing)
Just (Word16
w, a
x') -> t -> a -> [Word16] -> ([Word16], Maybe a)
go (t
i' t -> t -> t
forall a. Num a => a -> a -> a
- t
1) a
x' (Word16
wWord16 -> [Word16] -> [Word16]
forall a. a -> [a] -> [a]
:[Word16]
words')
{-# INLINE unfoldrN #-}
take :: Int
-> ShortByteString
-> ShortByteString
take :: Int -> ShortByteString -> ShortByteString
take = \Int
n -> \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let len :: Int
len = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min (ShortByteString -> Int
length ShortByteString
sbs) (Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 Int
n)
len8 :: Int
len8 = Int
len Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
in 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
{-# INLINE take #-}
takeEnd :: Int -> ShortByteString -> ShortByteString
takeEnd :: Int -> ShortByteString -> ShortByteString
takeEnd Int
n ShortByteString
sbs
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ShortByteString -> Int
length ShortByteString
sbs = ShortByteString
sbs
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = ShortByteString
empty
| Bool
otherwise = Int -> ShortByteString -> ShortByteString
drop (ShortByteString -> Int
length ShortByteString
sbs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) ShortByteString
sbs
{-# INLINE takeEnd #-}
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
{-# INLINE [1] takeWhile #-}
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
{-# INLINE takeWhileEnd #-}
drop :: Int -> ShortByteString -> ShortByteString
drop :: Int -> ShortByteString -> ShortByteString
drop = \Int
n -> \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) ->
let len :: Int
len = ShortByteString -> Int
length ShortByteString
sbs
newLen :: Int
newLen = Int -> Int -> Int
forall a. Ord a => a -> a -> a
max Int
0 (Int
len 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)
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
>= ShortByteString -> Int
length ShortByteString
sbs -> ShortByteString
empty
| Bool
otherwise -> Int -> (forall s. MBA s -> ST s ()) -> ShortByteString
create (Int
newLen Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) ((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 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2) MBA s
mba Int
0 (Int
newLen Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
{-# INLINE drop #-}
dropEnd :: Int -> ShortByteString -> ShortByteString
dropEnd :: Int -> ShortByteString -> ShortByteString
dropEnd Int
n ShortByteString
sbs
| 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
>= ShortByteString -> Int
length ShortByteString
sbs = ShortByteString
empty
| Bool
otherwise = Int -> ShortByteString -> ShortByteString
take (ShortByteString -> Int
length ShortByteString
sbs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
n) ShortByteString
sbs
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
{-# INLINE dropWhileEnd #-}
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
{-# INLINE breakEnd #-}
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
take Int
n ShortByteString
ps, Int -> ShortByteString -> ShortByteString
drop Int
n ShortByteString
ps)
{-# INLINE [1] break #-}
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
xs) -> if
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 -> (ShortByteString
forall a. Monoid a => a
mempty, ShortByteString
xs)
| Int
n Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= ShortByteString -> Int
BS.length ShortByteString
xs Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2 -> (ShortByteString
xs, ShortByteString
forall a. Monoid a => a
mempty)
| Bool
otherwise -> (Int -> ShortByteString -> ShortByteString
take Int
n ShortByteString
xs, Int -> ShortByteString -> ShortByteString
drop Int
n ShortByteString
xs)
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 (ShortByteString -> ShortByteString
tail ShortByteString
b)
foldl :: (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl :: (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl a -> Word16 -> a
f a
v = (a -> Word16 -> a) -> a -> [Word16] -> a
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
{-# INLINE foldl #-}
foldl' :: (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl' :: (a -> Word16 -> a) -> a -> ShortByteString -> a
foldl' a -> Word16 -> a
f a
v = (a -> Word16 -> a) -> a -> [Word16] -> a
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
{-# INLINE foldl' #-}
foldr :: (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr :: (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr Word16 -> a -> a
f a
v = (Word16 -> a -> a) -> a -> [Word16] -> a
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
{-# INLINE foldr #-}
foldr' :: (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr' :: (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr' Word16 -> a -> a
k a
v = (Word16 -> a -> a) -> a -> [Word16] -> a
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
{-# INLINE foldr' #-}
foldl1 :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1 :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1 Word16 -> Word16 -> Word16
k = (Word16 -> Word16 -> Word16) -> [Word16] -> Word16
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
{-# INLINE foldl1 #-}
foldl1' :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1' :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldl1' Word16 -> Word16 -> Word16
k = (Word16 -> Word16 -> Word16) -> [Word16] -> Word16
forall a. (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 :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1 :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1 Word16 -> Word16 -> Word16
k = (Word16 -> Word16 -> Word16) -> [Word16] -> Word16
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
{-# INLINE foldr1 #-}
foldr1' :: (Word16 -> Word16 -> Word16) -> ShortByteString -> Word16
foldr1' :: (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. String -> a
errorEmptyList String
"foldr1'" else (Word16 -> Word16 -> Word16) -> Word16 -> ShortByteString -> Word16
forall a. (Word16 -> a -> a) -> a -> ShortByteString -> a
foldr' Word16 -> Word16 -> Word16
k (ShortByteString -> Word16
last ShortByteString
sbs) (ShortByteString -> ShortByteString
init ShortByteString
sbs)
index :: ShortByteString -> Int -> Word16
index :: ShortByteString -> Int -> Word16
index = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) Int
i ->
let ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
w :: Int -> Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba
in 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
length ShortByteString
sbs -> Int -> Word16
w (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)
| Bool
otherwise -> String -> Word16
forall a. HasCallStack => String -> a
error (String -> Word16) -> String -> Word16
forall a b. (a -> b) -> a -> b
$ String
"Data.ByteString.Short.Word16.index: 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
length ShortByteString
sbs) String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
")"
indexMaybe :: ShortByteString -> Int -> Maybe Word16
indexMaybe :: ShortByteString -> Int -> Maybe Word16
indexMaybe = \(ShortByteString -> ShortByteString
assertEven -> ShortByteString
sbs) Int
i ->
let ba :: BA
ba = ShortByteString -> BA
asBA ShortByteString
sbs
w :: Int -> Word16
w = BA -> Int -> Word16
indexWord16Array BA
ba
in 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
length ShortByteString
sbs -> Word16 -> Maybe Word16
forall a. a -> Maybe a
Just (Int -> Word16
w (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2))
| Bool
otherwise -> Maybe Word16
forall a. Maybe a
Nothing
{-# INLINE indexMaybe #-}
(!?) :: 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
ps -> case Word16 -> ShortByteString -> Maybe Int
elemIndex Word16
c ShortByteString
ps 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
sbs -> if
| ShortByteString -> Bool
null ShortByteString
sbs -> ShortByteString
sbs
| Bool
otherwise -> [Word16] -> ShortByteString
pack ([Word16] -> ShortByteString)
-> (ShortByteString -> [Word16])
-> ShortByteString
-> ShortByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word16 -> Bool) -> [Word16] -> [Word16]
forall a. (a -> Bool) -> [a] -> [a]
List.filter Word16 -> Bool
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 (ShortByteString -> ShortByteString)
-> ShortByteString -> ShortByteString
forall a b. (a -> b) -> a -> b
$ ShortByteString
sbs
{-# INLINE filter #-}
find :: (Word16 -> Bool) -> ShortByteString -> Maybe Word16
find :: (Word16 -> Bool) -> ShortByteString -> Maybe Word16
find Word16 -> Bool
f = (Word16 -> Bool) -> [Word16] -> Maybe Word16
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
List.find Word16 -> Bool
f ([Word16] -> Maybe Word16)
-> (ShortByteString -> [Word16]) -> ShortByteString -> Maybe 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
{-# INLINE find #-}
partition :: (Word16 -> Bool) -> ShortByteString -> (ShortByteString, ShortByteString)
partition :: (Word16 -> Bool)
-> ShortByteString -> (ShortByteString, ShortByteString)
partition Word16 -> Bool
f = \ShortByteString
s -> if
| ShortByteString -> Bool
null ShortByteString
s -> (ShortByteString
s, ShortByteString
s)
| Bool
otherwise -> ([Word16] -> ShortByteString)
-> ([Word16] -> ShortByteString)
-> ([Word16], [Word16])
-> (ShortByteString, ShortByteString)
forall (p :: * -> * -> *) a b c d.
Bifunctor p =>
(a -> b) -> (c -> d) -> p a c -> p b d
bimap [Word16] -> ShortByteString
pack [Word16] -> ShortByteString
pack (([Word16], [Word16]) -> (ShortByteString, ShortByteString))
-> (ShortByteString -> ([Word16], [Word16]))
-> ShortByteString
-> (ShortByteString, ShortByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word16 -> Bool) -> [Word16] -> ([Word16], [Word16])
forall a. (a -> Bool) -> [a] -> ([a], [a])
List.partition Word16 -> Bool
f ([Word16] -> ([Word16], [Word16]))
-> (ShortByteString -> [Word16])
-> ShortByteString
-> ([Word16], [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 (ShortByteString -> (ShortByteString, ShortByteString))
-> ShortByteString -> (ShortByteString, ShortByteString)
forall a b. (a -> b) -> a -> b
$ ShortByteString
s
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)
{-# INLINE elemIndex #-}
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)
count :: Word16 -> ShortByteString -> Int
count :: Word16 -> ShortByteString -> Int
count Word16
w = [Int] -> 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
findIndex :: (Word16 -> Bool) -> ShortByteString -> Maybe Int
findIndex :: (Word16 -> Bool) -> ShortByteString -> Maybe Int
findIndex Word16 -> Bool
k = \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. Integral a => a -> a -> a
`div` Int
2)
| 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
{-# INLINE findIndex #-}
findIndices :: (Word16 -> Bool) -> ShortByteString -> [Int]
findIndices :: (Word16 -> Bool) -> ShortByteString -> [Int]
findIndices Word16 -> Bool
k = \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. Integral a => a -> a -> a
`div` Int
2) 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
{-# INLINE [1] findIndices #-}
findIndexOrLength :: (Word16 -> Bool) -> ShortByteString -> Int
findIndexOrLength :: (Word16 -> Bool) -> ShortByteString -> Int
findIndexOrLength Word16 -> Bool
k ShortByteString
sbs = Int -> Int
go Int
0
where
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 = Int
l Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
| Word16 -> Bool
k (Int -> Word16
w Int
n) = Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2
| Bool
otherwise = Int -> Int
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
{-# INLINE findIndexOrLength #-}
findFromEndUntil :: (Word16 -> Bool) -> ShortByteString -> Int
findFromEndUntil :: (Word16 -> Bool) -> ShortByteString -> Int
findFromEndUntil Word16 -> Bool
k ShortByteString
sbs = Int -> Int
go (ShortByteString -> Int
BS.length ShortByteString
sbs Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
where
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
0 = Int
0
| Word16 -> Bool
k (Int -> Word16
w Int
n) = (Int
n Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
2) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
| Bool
otherwise = Int -> Int
go (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
{-# INLINE findFromEndUntil #-}
assertEven :: ShortByteString -> ShortByteString
assertEven :: ShortByteString -> ShortByteString
assertEven sbs :: ShortByteString
sbs@(BS.SBS ByteArray#
barr#)
| Int -> Bool
forall a. Integral a => a -> Bool
even (Int# -> Int
I# (ByteArray# -> Int#
sizeofByteArray# ByteArray#
barr#)) = ShortByteString
sbs
| Bool
otherwise = String -> ShortByteString
forall a. HasCallStack => String -> a
error (String
"Uneven number of bytes: " String -> String -> String
forall a. Semigroup a => a -> a -> a
<> Int -> String
forall a. Show a => a -> String
show (ShortByteString -> Int
BS.length ShortByteString
sbs) String -> String -> String
forall a. Semigroup a => a -> a -> a
<> String
". This is not a Word16 bytestream.")