@@ -1,5 +1,8 @@
1{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
1{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
2 UnliftedFFITypes #-}
2 UnliftedFFITypes #-}
3+#if __GLASGOW_HASKELL__ >= 702
4+{-# LANGUAGE Trustworthy #-}
4-- Module : Data.Text.Encoding
7-- Module : Data.Text.Encoding
5-- Copyright : (c) 2009, 2010, 2011 Bryan O'Sullivan,
8-- Copyright : (c) 2009, 2010, 2011 Bryan O'Sullivan,
@@ -23,6 +26,7 @@ module Data.Text.Encoding
23 -- * Decoding ByteStrings to Text
26 -- * Decoding ByteStrings to Text
24 -- $strict
27 -- $strict
25 decodeASCII
28 decodeASCII
26 , decodeUtf8
30 , decodeUtf8
27 , decodeUtf16LE
31 , decodeUtf16LE
28 , decodeUtf16BE
32 , decodeUtf16BE
@@ -57,7 +61,7 @@ import Data.Bits ((.&.))
57import Data.ByteString as B
61import Data.ByteString as B
58import Data.ByteString.Internal as B
62import Data.ByteString.Internal as B
59import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
63import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60−import Data.Text.Internal (Text(..))
64+import Data.Text.Internal (Text(..), textP)
61import Data.Text.Private (runText)
65import Data.Text.Private (runText)
62import Data.Text.UnsafeChar (ord, unsafeWrite)
66import Data.Text.UnsafeChar (ord, unsafeWrite)
63import Data.Text.UnsafeShift (shiftL, shiftR)
67import Data.Text.UnsafeShift (shiftL, shiftR)
@@ -90,11 +94,23 @@ import qualified Data.Text.Fusion as F
90-- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
94-- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
91-- encoded text.
95-- encoded text.
93−-- This function is deprecated. Use 'decodeUtf8' instead.
97+-- This function is deprecated. Use 'decodeLatin1' instead.
94decodeASCII :: ByteString -> Text
98decodeASCII :: ByteString -> Text
95decodeASCII = decodeUtf8
99decodeASCII = decodeUtf8
96{-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
100{-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
102+-- | Decode a 'ByteString' containing Latin-1 (aka ISO-8859-1) encoded text.
104+-- 'decodeLatin1' is semantically equivalent to
105+-- @Data.Text.pack . Data.ByteString.Char8.unpack@
106+decodeLatin1 :: ByteString -> Text
107+decodeLatin1 (PS fp off len) = textP a 0 len
109+ a = A.run (A.new len >>= unsafeIOToST . go)
110+ go dest = withForeignPtr fp $ \ptr -> do
111+ c_decode_latin1 (A.maBA dest) (ptr `plusPtr` off) (ptr `plusPtr` (off+len))
98-- | Decode a 'ByteString' containing UTF-8 encoded text.
114-- | Decode a 'ByteString' containing UTF-8 encoded text.
99decodeUtf8With :: OnDecodeError -> ByteString -> Text
115decodeUtf8With :: OnDecodeError -> ByteString -> Text
100decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
116decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
@@ -279,3 +295,6 @@ encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
279foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
295foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
280 :: MutableByteArray# s -> Ptr CSize
296 :: MutableByteArray# s -> Ptr CSize
281 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
297 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
299+foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
300+ :: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()