    {-# LANGUAGE CPP #-}

    module TextBuilder.Domains.ByteString where

    import qualified Data.ByteString as ByteString
    import qualified Data.Text.Array as TextArray
    import TextBuilder.Prelude
    import TextBuilderCore

    #if !MIN_VERSION_text(2,0,0)
    import qualified Data.Text.Encoding as TextEncoding
    #endif

    -- | UTF-8 bytestring. You can use it for converting ASCII values as well.
    --
    -- __Warning:__ It's your responsibility to ensure that the bytestring is properly encoded.
    --
    -- >>> unsafeUtf8ByteString "abc"
    -- "abc"
    --
    -- >>> import Data.Text.Encoding (encodeUtf8)
    -- >>> unsafeUtf8ByteString (encodeUtf8 "фывапролдж") == "фывапролдж"
    -- True
    {-# INLINEABLE unsafeUtf8ByteString #-}
    unsafeUtf8ByteString :: ByteString -> TextBuilder
    #if MIN_VERSION_text(2,0,0)
    unsafeUtf8ByteString :: ByteString -> TextBuilder
    unsafeUtf8ByteString ByteString
    byteString =
      Int -> (forall s. MArray s -> Int -> ST s Int) -> TextBuilder
    TextBuilder
        (ByteString -> Int
    ByteString.length ByteString
    byteString)
        ( \MArray s
    array ->
            -- TODO: Optimize to use memcpy or something similar.
            let step :: Word8 -> (Int -> ST s Int) -> Int -> ST s Int
    step Word8
    byte Int -> ST s Int
    next Int
    index = do
                  MArray s -> Int -> Word8 -> ST s ()
    forall s. MArray s -> Int -> Word8 -> ST s ()
    TextArray.unsafeWrite MArray s
    array Int
    index Word8
    byte
                  Int -> ST s Int
    next (Int -> Int
    forall a. Enum a => a -> a
    succ Int
    index)
             in (Word8 -> (Int -> ST s Int) -> Int -> ST s Int)
    -> (Int -> ST s Int) -> ByteString -> Int -> ST s Int
    forall a. (Word8 -> a -> a) -> a -> ByteString -> a
    ByteString.foldr Word8 -> (Int -> ST s Int) -> Int -> ST s Int
    step Int -> ST s Int
    forall a. a -> ST s a
    forall (m :: * -> *) a. Monad m => a -> m a
    return ByteString
    byteString
        )
    #else
    -- Using a suboptimal solution here since the older version of \"text\" is becoming less important with time.
    unsafeUtf8ByteString =
      text . TextEncoding.decodeUtf8
    #endif