1 module Data.ByteString.Builder.Escape
    2   ( escapeLazyByteStringCharsWith
    3   , escapeTextWith
    4   ) where
    5 
    6 -- import qualified Data.ByteString as S
    7 import qualified Data.ByteString.Lazy as L
    8 import Data.ByteString.Builder (Builder)
    9 import Data.ByteString.Builder.Prim
   10 import Data.ByteString.Internal (c2w)
   11 import Data.Text (Text)
   12 import Data.Text.Encoding (encodeUtf8BuilderEscaped)
   13 import Data.Word (Word8)                                                                                 
   14 
   15 word8EscapedWith :: Word8 -> (Word8 -> Bool) -> BoundedPrim Word8
   16 word8EscapedWith e c = condB c (liftFixedToBounded $ (,) e >$< word8 >*< word8) (liftFixedToBounded word8)
   17 
   18 char8EscapedWith :: Char -> [Char] -> BoundedPrim Word8
   19 char8EscapedWith e c = word8EscapedWith (c2w e) (`elem` w) where w = map c2w c
   20 
   21 {-
   22 escapeByteStringWith :: Word8 -> (Word8 -> Bool) -> S.ByteString -> Builder      
   23 escapeByteStringWith e c = primMapByteStringBounded (word8EscapedWith e c)
   24 
   25 escapeLazyByteStringWith :: Word8 -> (Word8 -> Bool) -> L.ByteString -> Builder
   26 escapeLazyByteStringWith e c = primMapLazyByteStringBounded (word8EscapedWith e c)
   27 
   28 escapeByteStringCharsWith :: Char -> [Char] -> S.ByteString -> Builder                
   29 escapeByteStringCharsWith e c = primMapByteStringBounded (char8EscapedWith e c)
   30 -}
   31                                                                    
   32 escapeLazyByteStringCharsWith :: Char -> [Char] -> L.ByteString -> Builder
   33 escapeLazyByteStringCharsWith e c = primMapLazyByteStringBounded (char8EscapedWith e c)
   34 
   35 escapeTextWith :: Char -> [Char] -> Text -> Builder
   36 escapeTextWith e c = encodeUtf8BuilderEscaped (char8EscapedWith e c)