@@ -1,17 +1,7 @@
4−{-# LANGUAGE ForeignFunctionInterface #-}
5−{-# LANGUAGE MagicHash #-}
6−{-# LANGUAGE UnliftedFFITypes #-}
9module Data.Aeson.Parser.Unescape (
1module Data.Aeson.Parser.Unescape (
10 unescapeText
2 unescapeText
15import Control.Exception (evaluate, throw, try)
5import Control.Exception (evaluate, throw, try)
16import Control.Monad (when)
6import Control.Monad (when)
17import Data.ByteString as B
7import Data.ByteString as B
@@ -276,52 +266,6 @@ throwDecodeError =
276 throw (DecodeError desc Nothing)
266 throw (DecodeError desc Nothing)
277{-# INLINE throwDecodeError #-}
267{-# INLINE throwDecodeError #-}
283−import Control.Exception (evaluate, throw, try)
284−import Control.Monad.ST.Unsafe (unsafeIOToST, unsafeSTToIO)
285−import Data.ByteString as B
286−import Data.ByteString.Internal as B hiding (c2w)
287−import qualified Data.Text.Array as A
288−import Data.Text.Encoding.Error (UnicodeException (..))
289−import Data.Text.Internal (Text (..))
290−import Data.Text.Internal.Private (runText)
291−import Data.Text.Unsafe (unsafeDupablePerformIO)
292−import Data.Word (Word8)
293−import Foreign.C.Types (CInt (..), CSize (..))
294−import Foreign.ForeignPtr (withForeignPtr)
295−import Foreign.Marshal.Utils (with)
296−import Foreign.Ptr (Ptr, plusPtr)
297−import Foreign.Storable (peek)
298−import GHC.Base (MutableByteArray#)
300−foreign import ccall unsafe "_js_decode_string" c_js_decode
301− :: MutableByteArray# s -> Ptr CSize
302− -> Ptr Word8 -> Ptr Word8 -> IO CInt
304−unescapeText' :: ByteString -> Text
305−unescapeText' (PS fp off len) = runText $ \done -> do
306− let go dest = withForeignPtr fp $ \ptr ->
307− with (0::CSize) $ \destOffPtr -> do
308− let end = ptr `plusPtr` (off + len)
310− res <- c_js_decode (A.maBA dest) destOffPtr curPtr end
313− n <- peek destOffPtr
314− unsafeSTToIO (done dest (fromIntegral n))
316− throw (DecodeError desc Nothing)
317− loop (ptr `plusPtr` off)
318− (unsafeIOToST . go) =<< A.new len
320− desc = "Data.Text.Internal.Encoding.decodeUtf8: Invalid UTF-8 stream"
321−{-# INLINE unescapeText' #-}
325unescapeText :: ByteString -> Either UnicodeException Text
269unescapeText :: ByteString -> Either UnicodeException Text
326unescapeText = unsafeDupablePerformIO . try . evaluate . unescapeText'
270unescapeText = unsafeDupablePerformIO . try . evaluate . unescapeText'
327{-# INLINE unescapeText #-}
271{-# INLINE unescapeText #-}