@@ -32,6 +32,7 @@ import Control.Monad (MonadPlus(..))
3232import Data.Word (Word8)
3333import Data.ByteString (ByteString)
3434import qualified Data.ByteString as BS
35+import qualified Data.ByteString.Unsafe as BS
3536import Data.Text (Text)
3637import qualified Data.Text as T
3738import qualified Data.Text.Unsafe as T
@@ -238,6 +239,10 @@ class Monoid c => Chunk c where
238239 type ChunkElem c
239240 -- | Test if the chunk is empty.
240241 nullChunk :: c -> Bool
242+ -- | Get the head element of a non-empty chunk.
243+ unsafeChunkHead :: c -> ChunkElem c
244+ -- | Get the tail of a non-empty chunk.
245+ unsafeChunkTail :: c -> c
241246 -- | Check if the chunk has the length of at least @n@ elements.
242247 chunkLengthAtLeast :: c -> Int -> Bool
243248
@@ -245,6 +250,10 @@ instance Chunk ByteString where
245250 type ChunkElem ByteString = Word8
246251 nullChunk = BS.null
247252 {-# INLINE nullChunk #-}
253+ unsafeChunkHead = BS.unsafeHead
254+ {-# INLINE unsafeChunkHead #-}
255+ unsafeChunkTail = BS.unsafeTail
256+ {-# INLINE unsafeChunkTail #-}
248257 chunkLengthAtLeast bs n = BS.length bs >= n
249258 {-# INLINE chunkLengthAtLeast #-}
250259
@@ -252,5 +261,9 @@ instance Chunk Text where
252261 type ChunkElem Text = Char
253262 nullChunk = T.null
254263 {-# INLINE nullChunk #-}
264+ unsafeChunkHead = T.unsafeHead
265+ {-# INLINE unsafeChunkHead #-}
266+ unsafeChunkTail = T.unsafeTail
267+ {-# INLINE unsafeChunkTail #-}
255268 chunkLengthAtLeast t n = T.lengthWord16 t `quot` 2 >= n || T.length t >= n
256269 {-# INLINE chunkLengthAtLeast #-}