aboutsummaryrefslogtreecommitdiffhomepage
path: root/Pipes/Text
diff options
context:
space:
mode:
authormichaelt <what_is_it_to_do_anything@yahoo.com>2014-01-14 22:05:12 -0500
committermichaelt <what_is_it_to_do_anything@yahoo.com>2014-01-14 22:05:12 -0500
commit3694350ac7b9c42fd64e0092a74cf0471a080058 (patch)
tree38ddadda59a3808422fc432d37b886c456adcb1d /Pipes/Text
parentcd4fd5dd5405ad8e324f43ee2bc81822bdece16c (diff)
downloadtext-pipes-3694350ac7b9c42fd64e0092a74cf0471a080058.tar.gz
text-pipes-3694350ac7b9c42fd64e0092a74cf0471a080058.tar.zst
text-pipes-3694350ac7b9c42fd64e0092a74cf0471a080058.zip
Use clunky Data.Text.IO when bytestring is not explicit
Diffstat (limited to 'Pipes/Text')
-rw-r--r--Pipes/Text/Internal.hs126
1 files changed, 87 insertions, 39 deletions
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 @@
6module Pipes.Text.Internal 6module Pipes.Text.Internal
7 ( Decoding(..) 7 ( Decoding(..)
8 , streamDecodeUtf8 8 , streamDecodeUtf8
9 , decodeSomeUtf8
9 ) where 10 ) where
10
11import Control.Exception (evaluate, try)
12#if __GLASGOW_HASKELL__ >= 702
13import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO) 11import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO)
14import Control.Monad.ST (ST, runST) 12import Control.Monad.ST (ST, runST)
15#else
16import Control.Monad.ST (unsafeIOToST, unsafeSTToIO, ST, runST)
17#endif
18import Data.Bits ((.&.)) 13import Data.Bits ((.&.))
19import Data.ByteString as B 14import Data.ByteString as B
20import Data.ByteString.Internal as B 15import Data.ByteString.Internal as B
21import Data.Text () 16import qualified Data.Text as T (null)
22import qualified Data.Text as T 17import Data.Text.Encoding.Error ()
23import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode) 18import Data.Text.Internal (Text, textP)
24import Data.Text.Internal (Text(..), safe, textP)
25import Data.Word (Word8, Word32)
26import Foreign.C.Types (CSize) 19import Foreign.C.Types (CSize)
27import Foreign.ForeignPtr (withForeignPtr) 20import Foreign.ForeignPtr (withForeignPtr)
28import Foreign.Marshal.Utils (with) 21import Foreign.Marshal.Utils (with)
29import Foreign.Ptr (Ptr, minusPtr, nullPtr, plusPtr) 22import Foreign.Ptr (Ptr, minusPtr, nullPtr, plusPtr)
30import Foreign.Storable (Storable, peek, poke) 23import Foreign.Storable (Storable, peek, poke)
31import GHC.Base hiding (ord) 24import GHC.Base (Char(..), Int(..), MutableByteArray#, ord#, iShiftRA#)
32import GHC.Word 25import GHC.Word (Word8, Word32)
33import qualified Data.Text.Array as A 26import qualified Data.Text.Array as A
34import GHC.Exts (Char(..), Int(..), chr#, ord#, word2Int#) 27
35import GHC.Word (Word8(..), Word16(..), Word32(..))
36
37import Data.Text.Unsafe (unsafeDupablePerformIO)
38
39#include "pipes_text_cbits.h" 28#include "pipes_text_cbits.h"
40 29
41-- | A stream oriented decoding result. 30-- | A stream oriented decoding result.
@@ -52,44 +41,102 @@ instance Show Decoding where
52 showChar ' ' . showsPrec prec' bs . 41 showChar ' ' . showsPrec prec' bs .
53 showString " _" 42 showString " _"
54 where prec = 10; prec' = prec + 1 43 where prec = 10; prec' = prec + 1
55 44
56newtype CodePoint = CodePoint Word32 deriving (Eq, Show, Num, Storable) 45newtype CodePoint = CodePoint Word32 deriving (Eq, Show, Num, Storable)
57newtype DecoderState = DecoderState Word32 deriving (Eq, Show, Num, Storable) 46newtype DecoderState = DecoderState Word32 deriving (Eq, Show, Num, Storable)
58 47
59streamDecodeUtf8 :: ByteString -> Decoding 48streamDecodeUtf8 :: ByteString -> Decoding
60streamDecodeUtf8 = decodeChunk B.empty 0 0 49streamDecodeUtf8 = decodeChunkUtf8 B.empty 0 0
50 where
51 decodeChunkUtf8 :: ByteString -> CodePoint -> DecoderState -> ByteString -> Decoding
52 decodeChunkUtf8 old codepoint0 state0 bs@(PS fp off len) =
53 runST $ do marray <- A.new (len+1)
54 unsafeIOToST (decodeChunkToBuffer marray)
55 where
56 decodeChunkToBuffer :: A.MArray s -> IO Decoding
57 decodeChunkToBuffer dest = withForeignPtr fp $ \ptr ->
58 with (0::CSize) $ \destOffPtr ->
59 with codepoint0 $ \codepointPtr ->
60 with state0 $ \statePtr ->
61 with nullPtr $ \curPtrPtr ->
62 do let end = ptr `plusPtr` (off + len)
63 curPtr = ptr `plusPtr` off
64 poke curPtrPtr curPtr
65 c_decode_utf8_with_state (A.maBA dest) destOffPtr curPtrPtr end codepointPtr statePtr
66 state <- peek statePtr
67 lastPtr <- peek curPtrPtr
68 codepoint <- peek codepointPtr
69 n <- peek destOffPtr
70 chunkText <- mkText dest n
71 let left = lastPtr `minusPtr` curPtr
72 remaining = B.drop left bs
73 accum = if T.null chunkText then B.append old remaining else remaining
74 return $! case state of
75 UTF8_REJECT -> Other chunkText accum -- We encountered an encoding error
76 _ -> Some chunkText accum (decodeChunkUtf8 accum codepoint state)
77 {-# INLINE decodeChunkToBuffer #-}
78 {-# INLINE decodeChunkUtf8 #-}
79{-# INLINE streamDecodeUtf8 #-}
61 80
62decodeChunk :: ByteString -> CodePoint -> DecoderState -> ByteString -> Decoding 81decodeSomeUtf8 :: ByteString -> (Text, ByteString)
63decodeChunk old codepoint0 state0 bs@(PS fp off len) = 82decodeSomeUtf8 bs@(PS fp off len) = runST $ do
64 runST $ do marray <- A.new (len+1) 83 dest <- A.new (len+1)
65 unsafeIOToST (decodeChunkToBuffer marray) 84 unsafeIOToST $
66 where 85 withForeignPtr fp $ \ptr ->
67 decodeChunkToBuffer :: A.MArray s -> IO Decoding 86 with (0::CSize) $ \destOffPtr ->
68 decodeChunkToBuffer dest = withForeignPtr fp $ \ptr -> 87 with (0::CodePoint) $ \codepointPtr ->
69 with (0::CSize) $ \destOffPtr -> 88 with (0::DecoderState) $ \statePtr ->
70 with codepoint0 $ \codepointPtr -> 89 with nullPtr $ \curPtrPtr ->
71 with state0 $ \statePtr ->
72 with nullPtr $ \curPtrPtr ->
73 do let end = ptr `plusPtr` (off + len) 90 do let end = ptr `plusPtr` (off + len)
74 curPtr = ptr `plusPtr` off 91 curPtr = ptr `plusPtr` off
75 poke curPtrPtr curPtr 92 poke curPtrPtr curPtr
76 c_decode_utf8_with_state (A.maBA dest) destOffPtr curPtrPtr end codepointPtr statePtr 93 c_decode_utf8_with_state (A.maBA dest) destOffPtr
94 curPtrPtr end codepointPtr statePtr
77 state <- peek statePtr 95 state <- peek statePtr
78 lastPtr <- peek curPtrPtr 96 lastPtr <- peek curPtrPtr
79 codepoint <- peek codepointPtr 97 codepoint <- peek codepointPtr
80 n <- peek destOffPtr 98 n <- peek destOffPtr
81 chunkText <- mkText dest n 99 chunkText <- unsafeSTToIO $ do arr <- A.unsafeFreeze dest
100 return $! textP arr 0 (fromIntegral n)
82 let left = lastPtr `minusPtr` curPtr 101 let left = lastPtr `minusPtr` curPtr
83 remaining = B.drop left bs 102 remaining = B.drop left bs
84 accum = if T.null chunkText then B.append old remaining else remaining 103 return $! (chunkText, remaining)
85 return $ case state of 104{-# INLINE decodeSomeUtf8 #-}
86 UTF8_REJECT -> Other chunkText accum -- We encountered an encoding error 105
87 _ -> Some chunkText accum (decodeChunk accum codepoint state) 106-- decodeSomeUtf8 :: ByteString -> (Text, ByteString)
107-- decodeSomeUtf8 bs@(PS fp off len) =
108-- runST $ do marray <- A.new (len+1)
109-- unsafeIOToST (decodeChunkToBuffer marray)
110--
111-- where
112-- decodeChunkToBuffer :: A.MArray s -> IO (Text, ByteString)
113-- decodeChunkToBuffer dest = withForeignPtr fp $ \ptr ->
114-- with (0::CSize) $ \destOffPtr ->
115-- with (0::CodePoint) $ \codepointPtr ->
116-- with (0::DecoderState) $ \statePtr ->
117-- with nullPtr $ \curPtrPtr ->
118-- do let end = ptr `plusPtr` (off + len)
119-- curPtr = ptr `plusPtr` off
120-- poke curPtrPtr curPtr
121-- c_decode_utf8_with_state (A.maBA dest) destOffPtr curPtrPtr end codepointPtr statePtr
122-- state <- peek statePtr
123-- lastPtr <- peek curPtrPtr
124-- codepoint <- peek codepointPtr
125-- n <- peek destOffPtr
126-- chunkText <- unsafeSTToIO $ do arr <- A.unsafeFreeze dest
127-- return $! textP arr 0 (fromIntegral n)
128-- let left = lastPtr `minusPtr` curPtr
129-- remaining = B.drop left bs
130-- return $! (chunkText, remaining)
131-- {-# INLINE decodeChunkToBuffer #-}
132-- {-# INLINE decodeSomeUtf8 #-}
133
88 134
89 135
90mkText :: A.MArray s -> CSize -> IO Text 136mkText :: A.MArray s -> CSize -> IO Text
91mkText dest n = unsafeSTToIO $ do arr <- A.unsafeFreeze dest 137mkText dest n = unsafeSTToIO $ do arr <- A.unsafeFreeze dest
92 return $! textP arr 0 (fromIntegral n) 138 return $! textP arr 0 (fromIntegral n)
139{-# INLINE mkText #-}
93 140
94ord :: Char -> Int 141ord :: Char -> Int
95ord (C# c#) = I# (ord# c#) 142ord (C# c#) = I# (ord# c#)
@@ -107,6 +154,7 @@ unsafeWrite marr i c
107 lo = fromIntegral $ (m `shiftR` 10) + 0xD800 154 lo = fromIntegral $ (m `shiftR` 10) + 0xD800
108 hi = fromIntegral $ (m .&. 0x3FF) + 0xDC00 155 hi = fromIntegral $ (m .&. 0x3FF) + 0xDC00
109 shiftR (I# x#) (I# i#) = I# (x# `iShiftRA#` i#) 156 shiftR (I# x#) (I# i#) = I# (x# `iShiftRA#` i#)
157 {-# INLINE shiftR #-}
110{-# INLINE unsafeWrite #-} 158{-# INLINE unsafeWrite #-}
111 159
112foreign import ccall unsafe "_hs_pipes_text_decode_utf8_state" c_decode_utf8_with_state 160foreign import ccall unsafe "_hs_pipes_text_decode_utf8_state" c_decode_utf8_with_state