module Data.BAM.Version1_6.Read.Parser.BAM.Alignment.Internal (
decodeSeqField
, dropUpTo
, takeUpTo
) where
import Data.Bits
import Data.ByteString as DB hiding (concatMap)
import Data.Word
decodeSeqField :: DB.ByteString
-> [Word8]
decodeSeqField :: ByteString -> [Word8]
decodeSeqField ByteString
bytes =
(Word8 -> [Word8]) -> [Word8] -> [Word8]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap Word8 -> [Word8]
decodeByte (ByteString -> [Word8]
DB.unpack ByteString
bytes)
where
decodeByte :: Word8
-> [Word8]
decodeByte :: Word8 -> [Word8]
decodeByte Word8
byte =
[ Word8
byte Word8 -> Int -> Word8
forall a. Bits a => a -> Int -> a
`shiftR` Int
4
, Word8
byte Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
0x0F
]
dropUpTo :: Word8
-> ByteString
-> ByteString
dropUpTo :: Word8 -> ByteString -> ByteString
dropUpTo Word8
byte ByteString
bs =
let (ByteString
_,ByteString
suffix) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
DB.break (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
byte) ByteString
bs
in case ByteString -> Maybe (Word8, ByteString)
DB.uncons ByteString
suffix of
Maybe (Word8, ByteString)
Nothing ->
ByteString
DB.empty
Just (Word8, ByteString)
_ ->
ByteString
suffix
takeUpTo :: Word8
-> ByteString
-> ByteString
takeUpTo :: Word8 -> ByteString -> ByteString
takeUpTo Word8
byte
ByteString
bs =
let (ByteString
prefix,ByteString
suffix) = (Word8 -> Bool) -> ByteString -> (ByteString, ByteString)
DB.break (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
byte) ByteString
bs
in case ByteString -> Maybe (Word8, ByteString)
DB.uncons ByteString
suffix of
Maybe (Word8, ByteString)
Nothing ->
ByteString
prefix
Just (Word8
_,ByteString
_) ->
[ByteString] -> ByteString
DB.concat [ ByteString
prefix
, Word8 -> ByteString
DB.singleton Word8
byte
]