@@ -1,5 +1,8 @@
11{-# LANGUAGE BangPatterns, CPP, ForeignFunctionInterface, MagicHash,
22 UnliftedFFITypes #-}
3+#if __GLASGOW_HASKELL__ >= 702
4+{-# LANGUAGE Trustworthy #-}
5+#endif
36-- |
47-- Module : Data.Text.Encoding
58-- Copyright : (c) 2009, 2010, 2011 Bryan O'Sullivan,
@@ -23,6 +26,7 @@ module Data.Text.Encoding
2326 -- * Decoding ByteStrings to Text
2427 -- $strict
2528 decodeASCII
29+ , decodeLatin1
2630 , decodeUtf8
2731 , decodeUtf16LE
2832 , decodeUtf16BE
@@ -57,7 +61,7 @@ import Data.Bits ((.&.))
5761import Data.ByteString as B
5862import Data.ByteString.Internal as B
5963import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60−import Data.Text.Internal (Text(..))
64+import Data.Text.Internal (Text(..), textP)
6165import Data.Text.Private (runText)
6266import Data.Text.UnsafeChar (ord, unsafeWrite)
6367import Data.Text.UnsafeShift (shiftL, shiftR)
@@ -90,11 +94,23 @@ import qualified Data.Text.Fusion as F
9094-- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
9195-- encoded text.
9296--
93−-- This function is deprecated. Use 'decodeUtf8' instead.
97+-- This function is deprecated. Use 'decodeLatin1' instead.
9498decodeASCII :: ByteString -> Text
9599decodeASCII = decodeUtf8
96100{-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
97101
102+-- | Decode a 'ByteString' containing Latin-1 (aka ISO-8859-1) encoded text.
103+--
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
108+ where
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))
112+ return dest
113+
98114-- | Decode a 'ByteString' containing UTF-8 encoded text.
99115decodeUtf8With :: OnDecodeError -> ByteString -> Text
100116decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
@@ -279,3 +295,6 @@ encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
279295foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
280296 :: MutableByteArray# s -> Ptr CSize
281297 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
298+
299+foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
300+ :: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()