Add textUtf8 for copying text to builder with text-2.0 and newer

This commit is contained in:
Andrew Martin 2023-01-19 11:24:34 -05:00
parent 95845870ae
commit 0405c0eb88
2 changed files with 18 additions and 0 deletions

View file

@ -59,6 +59,7 @@ library
, primitive-unlifted >=0.1.2 && <0.2 , primitive-unlifted >=0.1.2 && <0.2
, run-st >=0.1 && <0.2 , run-st >=0.1 && <0.2
, template-haskell >=2.16 , template-haskell >=2.16
, text >=1.2 && <2.2
, text-short >=0.1.3 && <0.2 , text-short >=0.1.3 && <0.2
, wide-word >=0.1.0.9 && <0.2 , wide-word >=0.1.0.9 && <0.2
, zigzag , zigzag

View file

@ -1,3 +1,4 @@
{-# language CPP #-}
{-# language BangPatterns #-} {-# language BangPatterns #-}
{-# language DataKinds #-} {-# language DataKinds #-}
{-# language DuplicateRecordFields #-} {-# language DuplicateRecordFields #-}
@ -27,6 +28,9 @@ module Data.Bytes.Builder
, insert , insert
, byteArray , byteArray
, shortByteString , shortByteString
#if MIN_VERSION_text(2,0,0)
, textUtf8
#endif
, shortTextUtf8 , shortTextUtf8
, shortTextJsonString , shortTextJsonString
, cstring , cstring
@ -184,6 +188,12 @@ import qualified Data.Text.Short as TS
import qualified GHC.Exts as Exts import qualified GHC.Exts as Exts
import qualified Op as Op import qualified Op as Op
#if MIN_VERSION_text(2,0,0)
import Data.Text (Text)
import qualified Data.Text.Internal as I
import qualified Data.Text.Array as A
#endif
-- | Run a builder. -- | Run a builder.
run :: run ::
Int -- ^ Size of initial chunk (use 4080 if uncertain) Int -- ^ Size of initial chunk (use 4080 if uncertain)
@ -794,6 +804,13 @@ shortTextUtf8 a =
let ba = shortTextToByteArray a let ba = shortTextToByteArray a
in bytes (Bytes ba 0 (PM.sizeofByteArray ba)) in bytes (Bytes ba 0 (PM.sizeofByteArray ba))
#if MIN_VERSION_text(2,0,0)
-- | Create a builder from text. The text will be UTF-8 encoded.
textUtf8 :: Text -> Builder
textUtf8 (I.Text (A.ByteArray b) off len) =
bytes (Bytes (ByteArray b) off len)
#endif
-- | Create a builder from text. The text will be UTF-8 encoded, -- | Create a builder from text. The text will be UTF-8 encoded,
-- and JSON special characters will be escaped. Additionally, the -- and JSON special characters will be escaped. Additionally, the
-- result is surrounded by double quotes. For example: -- result is surrounded by double quotes. For example: