Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 7 additions & 21 deletions src/Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,11 @@ import GHC.Base (eqInt, neInt, gtInt, geInt, ltInt, leInt)
import qualified GHC.Exts as Exts
import GHC.Int (Int8)
import GHC.Stack (HasCallStack)
import qualified Language.Haskell.TH.Lib as TH
import qualified Language.Haskell.TH.Syntax as TH
import qualified Language.Haskell.TH.Lift as TH
import Text.Printf (PrintfArg, formatArg, formatString)
import System.Posix.Types (CSsize(..))

#if MIN_VERSION_template_haskell(2,16,0)
import Data.Text.Foreign (asForeignPtr)
import System.IO.Unsafe (unsafePerformIO)
#endif

-- $setup
-- >>> :set -package transformers
Expand Down Expand Up @@ -451,27 +447,18 @@ instance Data Text where

-- | @since 1.2.4.0
instance TH.Lift Text where
#if MIN_VERSION_template_haskell(2,16,0)
lift txt = do
let (ptr, len) = unsafePerformIO $ asForeignPtr txt
case len of
0 -> TH.varE 'empty
0 -> [| empty |]
_ ->
let
bytesQ = TH.litE . TH.bytesPrimL $ TH.mkBytes ptr 0 (P.fromIntegral len)
lenQ = liftInt (P.fromIntegral len)
liftInt n = (TH.appE (TH.conE 'Exts.I#) (TH.litE (TH.IntPrimL n)))
in TH.varE 'unpackCStringLen# `TH.appE` bytesQ `TH.appE` lenQ
#else
lift = TH.appE (TH.varE 'pack) . TH.stringE . unpack
#endif
#if MIN_VERSION_template_haskell(2,17,0)
liftTyped = TH.unsafeCodeCoerce . TH.lift
#elif MIN_VERSION_template_haskell(2,16,0)
liftTyped = TH.unsafeTExpCoerce . TH.lift
#endif
bytesQ = TH.liftAddrCompat ptr 0 (P.fromIntegral len)
lenQ = TH.liftIntCompat (P.fromIntegral len)
in [| unpackCStringLen# $bytesQ $lenQ |]

liftTyped = TH.defaultLiftTyped

#if MIN_VERSION_template_haskell(2,16,0)
unpackCStringLen# :: Exts.Addr# -> Int -> Text
unpackCStringLen# addr# l = Text ba 0 l
where
Expand All @@ -480,7 +467,6 @@ unpackCStringLen# addr# l = Text ba 0 l
A.copyFromPointer marr 0 (Exts.Ptr addr#) l
A.unsafeFreeze marr
{-# NOINLINE unpackCStringLen# #-} -- set as NOINLINE to avoid generated code bloat
#endif

-- | @since 1.2.2.0
instance PrintfArg Text where
Expand Down
11 changes: 3 additions & 8 deletions src/Data/Text/Lazy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ import qualified GHC.CString as GHC
import qualified GHC.Exts as Exts
import GHC.Prim (Addr#)
import GHC.Stack (HasCallStack)
import qualified Language.Haskell.TH.Lib as TH
import qualified Language.Haskell.TH.Syntax as TH
import qualified Language.Haskell.TH.Lift as TH
import Text.Printf (PrintfArg, formatArg, formatString)

-- $fusion
Expand Down Expand Up @@ -393,12 +392,8 @@ instance Data Text where

-- | @since 1.2.4.0
instance TH.Lift Text where
lift = TH.appE (TH.varE 'fromStrict) . TH.lift . toStrict
#if MIN_VERSION_template_haskell(2,17,0)
liftTyped = TH.unsafeCodeCoerce . TH.lift
#elif MIN_VERSION_template_haskell(2,16,0)
liftTyped = TH.unsafeTExpCoerce . TH.lift
#endif
lift x = [| fromStrict $(TH.lift . toStrict $ x) |]
liftTyped = TH.defaultLiftTyped

-- | @since 1.2.2.0
instance PrintfArg Text where
Expand Down
4 changes: 2 additions & 2 deletions text.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ library
bytestring >= 0.10.4 && < 0.13,
deepseq >= 1.1 && < 1.6,
ghc-prim >= 0.2 && < 0.15,
template-haskell >= 2.5 && < 2.25
template-haskell-lift >= 0.1 && <0.2

if impl(ghc < 9.4)
build-depends: data-array-byte >= 0.1 && < 0.2
Expand Down Expand Up @@ -303,7 +303,7 @@ test-suite tests
tasty,
tasty-hunit,
tasty-quickcheck,
template-haskell,
template-haskell-lift,
temporary,
transformers,
text
Expand Down
Loading