From 3694350ac7b9c42fd64e0092a74cf0471a080058 Mon Sep 17 00:00:00 2001 From: michaelt Date: Tue, 14 Jan 2014 22:05:12 -0500 Subject: Use clunky Data.Text.IO when bytestring is not explicit --- Pipes/Text.hs | 37 +++++++++------ Pipes/Text/Internal.hs | 126 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 110 insertions(+), 53 deletions(-) (limited to 'Pipes') diff --git a/Pipes/Text.hs b/Pipes/Text.hs index 74d2023..cf493e9 100644 --- a/Pipes/Text.hs +++ b/Pipes/Text.hs @@ -1,4 +1,4 @@ -{-# LANGUAGE RankNTypes, TypeFamilies #-} +{-# LANGUAGE RankNTypes, TypeFamilies, BangPatterns #-} {-| This module provides @pipes@ utilities for \"text streams\", which are streams of 'Text' chunks. The individual chunks are uniformly @strict@, but @@ -206,30 +206,36 @@ import Prelude hiding ( -- | Convert a lazy 'TL.Text' into a 'Producer' of strict 'Text's fromLazy :: (Monad m) => TL.Text -> Producer' Text m () fromLazy = foldrChunks (\e a -> yield e >> a) (return ()) -{-# INLINABLE fromLazy #-} +{-# INLINE fromLazy #-} -- | Stream text from 'stdin' -stdin :: MonadIO m => Producer Text m (Producer ByteString m ()) +stdin :: MonadIO m => Producer Text m () stdin = fromHandle IO.stdin -{-# INLINABLE stdin #-} +{-# INLINE stdin #-} {-| Convert a 'IO.Handle' into a text stream using a text size - determined by the good sense of the text library. + determined by the good sense of the text library; note that this + is distinctly slower than @decideUtf8 (Pipes.ByteString.fromHandle h)@ + but uses the system encoding and has other `Data.Text.IO` features -} -fromHandle :: MonadIO m => IO.Handle -> Producer Text m (Producer ByteString m ()) -fromHandle h = decodeUtf8 (PB.fromHandle h) -{-# INLINE fromHandle#-} +fromHandle :: MonadIO m => IO.Handle -> Producer Text m () +fromHandle h = go where + go = do txt <- liftIO (T.hGetChunk h) + unless (T.null txt) $ do yield txt + go +{-# INLINABLE fromHandle#-} -{-| Stream text from a file using Pipes.Safe + +{-| Stream text from a file in the simple fashion of @Data.Text.IO@ >>> runSafeT $ runEffect $ Text.readFile "hello.hs" >-> Text.map toUpper >-> hoist lift Text.stdout MAIN = PUTSTRLN "HELLO WORLD" -} -readFile :: (MonadSafe m) => FilePath -> Producer Text m (Producer ByteString m ()) +readFile :: MonadSafe m => FilePath -> Producer Text m () readFile file = Safe.withFile file IO.ReadMode fromHandle -{-# INLINABLE readFile #-} +{-# INLINE readFile #-} {-| Stream lines of text from stdin (for testing in ghci etc.) @@ -249,7 +255,7 @@ stdinLn = go where txt <- liftIO (T.hGetLine IO.stdin) yield txt go - +{-# INLINABLE stdinLn #-} {-| Stream text to 'stdout' @@ -305,6 +311,7 @@ toHandle h = for cat (liftIO . T.hPutStr h) -- | Stream text into a file. Uses @pipes-safe@. writeFile :: (MonadSafe m) => FilePath -> Consumer' Text m () writeFile file = Safe.withFile file IO.WriteMode toHandle +{-# INLINE writeFile #-} -- | Apply a transformation to each 'Char' in the stream map :: (Monad m) => (Char -> Char) -> Pipe Text Text m r @@ -592,10 +599,10 @@ count c p = P.fold (+) 0 id (p >-> P.map (fromIntegral . T.count c)) decodeUtf8 :: Monad m => Producer ByteString m r -> Producer Text m (Producer ByteString m r) decodeUtf8 = go B.empty PE.streamDecodeUtf8 where - go carry dec0 p = do + go !carry dec0 p = do x <- lift (next p) case x of Left r -> if B.null carry - then return (return r) -- all input was consumed + then return (return r) -- all bytestrinput was consumed else return (do yield carry -- a potentially valid fragment remains return r) @@ -605,6 +612,8 @@ decodeUtf8 = go B.empty PE.streamDecodeUtf8 where PE.Other text bs -> do yield text return (do yield bs -- an invalid blob remains p') +{-# INLINABLE decodeUtf8 #-} + -- | Splits a 'Producer' after the given number of characters splitAt diff --git a/Pipes/Text/Internal.hs b/Pipes/Text/Internal.hs index 73d6fa4..7e5b044 100644 --- a/Pipes/Text/Internal.hs +++ b/Pipes/Text/Internal.hs @@ -6,36 +6,25 @@ module Pipes.Text.Internal ( Decoding(..) , streamDecodeUtf8 + , decodeSomeUtf8 ) where - -import Control.Exception (evaluate, try) -#if __GLASGOW_HASKELL__ >= 702 import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO) import Control.Monad.ST (ST, runST) -#else -import Control.Monad.ST (unsafeIOToST, unsafeSTToIO, ST, runST) -#endif import Data.Bits ((.&.)) -import Data.ByteString as B -import Data.ByteString.Internal as B -import Data.Text () -import qualified Data.Text as T -import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode) -import Data.Text.Internal (Text(..), safe, textP) -import Data.Word (Word8, Word32) +import Data.ByteString as B +import Data.ByteString.Internal as B +import qualified Data.Text as T (null) +import Data.Text.Encoding.Error () +import Data.Text.Internal (Text, textP) import Foreign.C.Types (CSize) import Foreign.ForeignPtr (withForeignPtr) import Foreign.Marshal.Utils (with) import Foreign.Ptr (Ptr, minusPtr, nullPtr, plusPtr) import Foreign.Storable (Storable, peek, poke) -import GHC.Base hiding (ord) -import GHC.Word +import GHC.Base (Char(..), Int(..), MutableByteArray#, ord#, iShiftRA#) +import GHC.Word (Word8, Word32) import qualified Data.Text.Array as A -import GHC.Exts (Char(..), Int(..), chr#, ord#, word2Int#) -import GHC.Word (Word8(..), Word16(..), Word32(..)) - -import Data.Text.Unsafe (unsafeDupablePerformIO) - + #include "pipes_text_cbits.h" -- | A stream oriented decoding result. @@ -52,44 +41,102 @@ instance Show Decoding where showChar ' ' . showsPrec prec' bs . showString " _" where prec = 10; prec' = prec + 1 - + newtype CodePoint = CodePoint Word32 deriving (Eq, Show, Num, Storable) newtype DecoderState = DecoderState Word32 deriving (Eq, Show, Num, Storable) streamDecodeUtf8 :: ByteString -> Decoding -streamDecodeUtf8 = decodeChunk B.empty 0 0 +streamDecodeUtf8 = decodeChunkUtf8 B.empty 0 0 + where + decodeChunkUtf8 :: ByteString -> CodePoint -> DecoderState -> ByteString -> Decoding + decodeChunkUtf8 old codepoint0 state0 bs@(PS fp off len) = + runST $ do marray <- A.new (len+1) + unsafeIOToST (decodeChunkToBuffer marray) + where + decodeChunkToBuffer :: A.MArray s -> IO Decoding + decodeChunkToBuffer dest = withForeignPtr fp $ \ptr -> + with (0::CSize) $ \destOffPtr -> + with codepoint0 $ \codepointPtr -> + with state0 $ \statePtr -> + with nullPtr $ \curPtrPtr -> + do let end = ptr `plusPtr` (off + len) + curPtr = ptr `plusPtr` off + poke curPtrPtr curPtr + c_decode_utf8_with_state (A.maBA dest) destOffPtr curPtrPtr end codepointPtr statePtr + state <- peek statePtr + lastPtr <- peek curPtrPtr + codepoint <- peek codepointPtr + n <- peek destOffPtr + chunkText <- mkText dest n + let left = lastPtr `minusPtr` curPtr + remaining = B.drop left bs + accum = if T.null chunkText then B.append old remaining else remaining + return $! case state of + UTF8_REJECT -> Other chunkText accum -- We encountered an encoding error + _ -> Some chunkText accum (decodeChunkUtf8 accum codepoint state) + {-# INLINE decodeChunkToBuffer #-} + {-# INLINE decodeChunkUtf8 #-} +{-# INLINE streamDecodeUtf8 #-} -decodeChunk :: ByteString -> CodePoint -> DecoderState -> ByteString -> Decoding -decodeChunk old codepoint0 state0 bs@(PS fp off len) = - runST $ do marray <- A.new (len+1) - unsafeIOToST (decodeChunkToBuffer marray) - where - decodeChunkToBuffer :: A.MArray s -> IO Decoding - decodeChunkToBuffer dest = withForeignPtr fp $ \ptr -> - with (0::CSize) $ \destOffPtr -> - with codepoint0 $ \codepointPtr -> - with state0 $ \statePtr -> - with nullPtr $ \curPtrPtr -> +decodeSomeUtf8 :: ByteString -> (Text, ByteString) +decodeSomeUtf8 bs@(PS fp off len) = runST $ do + dest <- A.new (len+1) + unsafeIOToST $ + withForeignPtr fp $ \ptr -> + with (0::CSize) $ \destOffPtr -> + with (0::CodePoint) $ \codepointPtr -> + with (0::DecoderState) $ \statePtr -> + with nullPtr $ \curPtrPtr -> do let end = ptr `plusPtr` (off + len) curPtr = ptr `plusPtr` off poke curPtrPtr curPtr - c_decode_utf8_with_state (A.maBA dest) destOffPtr curPtrPtr end codepointPtr statePtr + c_decode_utf8_with_state (A.maBA dest) destOffPtr + curPtrPtr end codepointPtr statePtr state <- peek statePtr lastPtr <- peek curPtrPtr codepoint <- peek codepointPtr n <- peek destOffPtr - chunkText <- mkText dest n + chunkText <- unsafeSTToIO $ do arr <- A.unsafeFreeze dest + return $! textP arr 0 (fromIntegral n) let left = lastPtr `minusPtr` curPtr remaining = B.drop left bs - accum = if T.null chunkText then B.append old remaining else remaining - return $ case state of - UTF8_REJECT -> Other chunkText accum -- We encountered an encoding error - _ -> Some chunkText accum (decodeChunk accum codepoint state) + return $! (chunkText, remaining) +{-# INLINE decodeSomeUtf8 #-} + +-- decodeSomeUtf8 :: ByteString -> (Text, ByteString) +-- decodeSomeUtf8 bs@(PS fp off len) = +-- runST $ do marray <- A.new (len+1) +-- unsafeIOToST (decodeChunkToBuffer marray) +-- +-- where +-- decodeChunkToBuffer :: A.MArray s -> IO (Text, ByteString) +-- decodeChunkToBuffer dest = withForeignPtr fp $ \ptr -> +-- with (0::CSize) $ \destOffPtr -> +-- with (0::CodePoint) $ \codepointPtr -> +-- with (0::DecoderState) $ \statePtr -> +-- with nullPtr $ \curPtrPtr -> +-- do let end = ptr `plusPtr` (off + len) +-- curPtr = ptr `plusPtr` off +-- poke curPtrPtr curPtr +-- c_decode_utf8_with_state (A.maBA dest) destOffPtr curPtrPtr end codepointPtr statePtr +-- state <- peek statePtr +-- lastPtr <- peek curPtrPtr +-- codepoint <- peek codepointPtr +-- n <- peek destOffPtr +-- chunkText <- unsafeSTToIO $ do arr <- A.unsafeFreeze dest +-- return $! textP arr 0 (fromIntegral n) +-- let left = lastPtr `minusPtr` curPtr +-- remaining = B.drop left bs +-- return $! (chunkText, remaining) +-- {-# INLINE decodeChunkToBuffer #-} +-- {-# INLINE decodeSomeUtf8 #-} + mkText :: A.MArray s -> CSize -> IO Text mkText dest n = unsafeSTToIO $ do arr <- A.unsafeFreeze dest return $! textP arr 0 (fromIntegral n) +{-# INLINE mkText #-} ord :: Char -> Int ord (C# c#) = I# (ord# c#) @@ -107,6 +154,7 @@ unsafeWrite marr i c lo = fromIntegral $ (m `shiftR` 10) + 0xD800 hi = fromIntegral $ (m .&. 0x3FF) + 0xDC00 shiftR (I# x#) (I# i#) = I# (x# `iShiftRA#` i#) + {-# INLINE shiftR #-} {-# INLINE unsafeWrite #-} foreign import ccall unsafe "_hs_pipes_text_decode_utf8_state" c_decode_utf8_with_state -- cgit v1.2.3