{-# LANGUAGE CPP #-}

-- | Some helpers functions.
module Network.Wai.Util (
    dropWhileEnd,
    splitCommas,
    trimWS,
) where

import qualified Data.ByteString as S
import Data.Word8 (Word8, _comma, _space)

-- | Used to split a header value which is a comma separated list
splitCommas :: S.ByteString -> [S.ByteString]
splitCommas :: ByteString -> [ByteString]
splitCommas = (ByteString -> ByteString) -> [ByteString] -> [ByteString]
forall a b. (a -> b) -> [a] -> [b]
map ByteString -> ByteString
trimWS ([ByteString] -> [ByteString])
-> (ByteString -> [ByteString]) -> ByteString -> [ByteString]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Word8 -> ByteString -> [ByteString]
S.split Word8
_comma

-- Trim whitespace
trimWS :: S.ByteString -> S.ByteString
trimWS :: ByteString -> ByteString
trimWS = (Word8 -> Bool) -> ByteString -> ByteString
dropWhileEnd (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_space) (ByteString -> ByteString)
-> (ByteString -> ByteString) -> ByteString -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Word8 -> Bool) -> ByteString -> ByteString
S.dropWhile (Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
_space)

-- | Dropping all 'Word8's from the end that satisfy the predicate.
dropWhileEnd :: (Word8 -> Bool) -> S.ByteString -> S.ByteString
#if MIN_VERSION_bytestring(0,10,12)
dropWhileEnd :: (Word8 -> Bool) -> ByteString -> ByteString
dropWhileEnd = (Word8 -> Bool) -> ByteString -> ByteString
S.dropWhileEnd
#else
dropWhileEnd p = fst . S.spanEnd p
#endif