Skip to content

Commit

Permalink
Implement Data.Text.unpack directly, without streaming
Browse files Browse the repository at this point in the history
  • Loading branch information
Bodigrim committed Aug 15, 2024
1 parent 6ed3cae commit 25a7d7b
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/Data/Text/Show.hs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{-# LANGUAGE CPP, MagicHash #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE MagicHash #-}
{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE ViewPatterns #-}

{-# OPTIONS_GHC -fno-warn-orphans #-}
Expand All @@ -26,12 +28,11 @@ module Data.Text.Show
import Control.Monad.ST (ST, runST)
import Data.Text.Internal (Text(..), empty, safe, pack)
import Data.Text.Internal.Encoding.Utf8 (utf8Length)
import Data.Text.Internal.Fusion (stream)
import Data.Text.Internal.Unsafe.Char (unsafeWrite)
import Data.Text.Unsafe (Iter(..), iterArray)
import GHC.Exts (Ptr(..), Int(..), Addr#, indexWord8OffAddr#)
import GHC.Word (Word8(..))
import qualified Data.Text.Array as A
import qualified Data.Text.Internal.Fusion.Common as S
#if !MIN_VERSION_ghc_prim(0,7,0)
import Foreign.C.String (CString)
import Foreign.C.Types (CSize(..))
Expand All @@ -52,7 +53,13 @@ unpack ::
HasCallStack =>
#endif
Text -> String
unpack = S.unstreamList . stream
unpack (Text arr off len) = go 0
where
go !i
| i >= len = []
| otherwise = c : go (i + l)
where
!(Iter c l) = iterArray arr (off+i)
{-# INLINE [1] unpack #-}

-- | /O(n)/ Convert a null-terminated
Expand Down

0 comments on commit 25a7d7b

Please sign in to comment.