{-# LANGUAGE CPP #-}
#if __GLASGOW_HASKELL__ >= 704
{-# OPTIONS_GHC -fsimpl-tick-factor=40000 #-}
#endif

------------------------------------------------------------------------------
-- |
-- Module:      Blaze.ByteString.Builder.Html.Word
-- Copyright:   (c) 2016 Dylan Simon
-- License:     BSD3
-- Maintainer:  https://github.com/blaze-builder
-- Stability:   stable
--
-- 'W.Write's and 'B.Builder's for serializing HTML escaped 'Word8' characters
-- and 'BS.ByteString's that have already been appropriately encoded into HTML by
-- escaping basic ASCII character references but leaving other bytes untouched.
--
------------------------------------------------------------------------------

module Blaze.ByteString.Builder.Html.Word
  ( wordHtmlEscaped
    -- * Writing HTML escaped bytes to a buffer
  , writeHtmlEscapedWord
    -- * Creating Builders from HTML escaped bytes
  , fromHtmlEscapedWord
  , fromHtmlEscapedWordList
  , fromHtmlEscapedByteString
  , fromHtmlEscapedLazyByteString
  ) where

import qualified Blaze.ByteString.Builder.Compat.Write as W
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as B
import qualified Data.ByteString.Builder.Prim as P
import           Data.ByteString.Internal (c2w)
import qualified Data.ByteString.Lazy as BSL
import           Data.Word (Word8)

{-# INLINE wordHtmlEscaped #-}
wordHtmlEscaped :: P.BoundedPrim Word8
wordHtmlEscaped :: BoundedPrim Word8
wordHtmlEscaped =
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Ord a => a -> a -> Bool
>  Char -> Word8
c2w Char
'>' ) (forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'\DEL') forall a. BoundedPrim a
P.emptyB forall a b. (a -> b) -> a -> b
$ forall a. FixedPrim a -> BoundedPrim a
P.liftFixedToBounded FixedPrim Word8
P.word8) forall a b. (a -> b) -> a -> b
$
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'<' ) (forall {a}. (Char, (Char, (Char, Char))) -> BoundedPrim a
fixed4 (Char
'&',(Char
'l',(Char
't',Char
';')))) forall a b. (a -> b) -> a -> b
$        -- &lt;
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'>' ) (forall {a}. (Char, (Char, (Char, Char))) -> BoundedPrim a
fixed4 (Char
'&',(Char
'g',(Char
't',Char
';')))) forall a b. (a -> b) -> a -> b
$        -- &gt;
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'&' ) (forall {a}. (Char, (Char, (Char, (Char, Char)))) -> BoundedPrim a
fixed5 (Char
'&',(Char
'a',(Char
'm',(Char
'p',Char
';'))))) forall a b. (a -> b) -> a -> b
$  -- &amp;
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'"' ) (forall {a}.
(Char, (Char, (Char, (Char, (Char, Char))))) -> BoundedPrim a
fixed6 (Char
'&',(Char
'q',(Char
'u',(Char
'o',(Char
't',Char
';')))))) forall a b. (a -> b) -> a -> b
$  -- &quot;
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'\'') (forall {a}. (Char, (Char, (Char, (Char, Char)))) -> BoundedPrim a
fixed5 (Char
'&',(Char
'#',(Char
'3',(Char
'9',Char
';'))))) forall a b. (a -> b) -> a -> b
$  -- &#39;
  forall a.
(a -> Bool) -> BoundedPrim a -> BoundedPrim a -> BoundedPrim a
P.condB (\Word8
c -> Word8
c forall a. Ord a => a -> a -> Bool
>= Char -> Word8
c2w Char
' ' Bool -> Bool -> Bool
|| Word8
c forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'\t' Bool -> Bool -> Bool
|| Word8
c forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'\n' Bool -> Bool -> Bool
|| Word8
c forall a. Eq a => a -> a -> Bool
== Char -> Word8
c2w Char
'\r')
        (forall a. FixedPrim a -> BoundedPrim a
P.liftFixedToBounded FixedPrim Word8
P.word8) forall a. BoundedPrim a
P.emptyB
  where
  {-# INLINE fixed4 #-}
  fixed4 :: (Char, (Char, (Char, Char))) -> BoundedPrim a
fixed4 (Char, (Char, (Char, Char)))
x = forall a. FixedPrim a -> BoundedPrim a
P.liftFixedToBounded forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const (Char, (Char, (Char, Char)))
x forall (f :: * -> *) b a. Contravariant f => (b -> a) -> f a -> f b
P.>$<
    FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8
  {-# INLINE fixed5 #-}
  fixed5 :: (Char, (Char, (Char, (Char, Char)))) -> BoundedPrim a
fixed5 (Char, (Char, (Char, (Char, Char))))
x = forall a. FixedPrim a -> BoundedPrim a
P.liftFixedToBounded forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const (Char, (Char, (Char, (Char, Char))))
x forall (f :: * -> *) b a. Contravariant f => (b -> a) -> f a -> f b
P.>$<
    FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8
  {-# INLINE fixed6 #-}
  fixed6 :: (Char, (Char, (Char, (Char, (Char, Char))))) -> BoundedPrim a
fixed6 (Char, (Char, (Char, (Char, (Char, Char)))))
x = forall a. FixedPrim a -> BoundedPrim a
P.liftFixedToBounded forall a b. (a -> b) -> a -> b
$ forall a b. a -> b -> a
const (Char, (Char, (Char, (Char, (Char, Char)))))
x forall (f :: * -> *) b a. Contravariant f => (b -> a) -> f a -> f b
P.>$<
    FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8 forall (f :: * -> *) a b. Monoidal f => f a -> f b -> f (a, b)
P.>*< FixedPrim Char
P.char8

-- | Write a HTML escaped byte to a bufffer.
writeHtmlEscapedWord :: Word8 -> W.Write
writeHtmlEscapedWord :: Word8 -> Write
writeHtmlEscapedWord = forall a. BoundedPrim a -> a -> Write
W.writePrimBounded BoundedPrim Word8
wordHtmlEscaped

-- | /O(1)./ Serialize a HTML escaped byte.
fromHtmlEscapedWord :: Word8 -> B.Builder
fromHtmlEscapedWord :: Word8 -> Builder
fromHtmlEscapedWord = forall a. BoundedPrim a -> a -> Builder
P.primBounded BoundedPrim Word8
wordHtmlEscaped

-- | /O(n)/. Serialize a HTML escaped list of bytes.
fromHtmlEscapedWordList :: [Word8] -> B.Builder
fromHtmlEscapedWordList :: [Word8] -> Builder
fromHtmlEscapedWordList = forall a. BoundedPrim a -> [a] -> Builder
P.primMapListBounded BoundedPrim Word8
wordHtmlEscaped

-- | /O(n)/. Serialize a HTML escaped 'BS.ByteString'.
fromHtmlEscapedByteString :: BS.ByteString -> B.Builder
fromHtmlEscapedByteString :: ByteString -> Builder
fromHtmlEscapedByteString = BoundedPrim Word8 -> ByteString -> Builder
P.primMapByteStringBounded BoundedPrim Word8
wordHtmlEscaped

-- | /O(n)/. Serialize a HTML escaped lazy 'BSL.ByteString'.
fromHtmlEscapedLazyByteString :: BSL.ByteString -> B.Builder
fromHtmlEscapedLazyByteString :: ByteString -> Builder
fromHtmlEscapedLazyByteString = BoundedPrim Word8 -> ByteString -> Builder
P.primMapLazyByteStringBounded BoundedPrim Word8
wordHtmlEscaped