{-# LANGUAGE PackageImports #-}
{-# LANGUAGE Strict #-}
module Data.String.Interpolate.Conversion.Encoding
( bsToTextBuilder, lbsToTextBuilder, encodeCharUTF8 )
where
import qualified Data.ByteString as B
import qualified Data.ByteString.Builder as LB
import qualified Data.ByteString.Lazy as LB
import qualified Data.Text.Lazy.Builder as LT
import qualified "utf8-string" Data.ByteString.Lazy.UTF8 as LUTF8
import qualified "utf8-string" Data.ByteString.UTF8 as UTF8
bsToTextBuilder :: B.ByteString -> LT.Builder
bsToTextBuilder = UTF8.foldr (\char bldr -> LT.singleton char <> bldr) mempty
lbsToTextBuilder :: LB.ByteString -> LT.Builder
lbsToTextBuilder = LUTF8.foldr (\char bldr -> LT.singleton char <> bldr) mempty
encodeCharUTF8 :: Char -> LB.Builder
encodeCharUTF8 c =
let normalized = case c of
'\xFFFE' -> '\xFFFD'
'\xFFFF' -> '\xFFFD'
_ -> c
in LB.charUtf8 normalized