@@ -23,6 +23,7 @@ module Data.Text.Encoding
2323 -- * Decoding ByteStrings to Text
2424 -- $strict
2525 decodeASCII
26+ , decodeLatin1
2627 , decodeUtf8
2728 , decodeUtf16LE
2829 , decodeUtf16BE
@@ -57,7 +58,7 @@ import Data.Bits ((.&.))
5758import Data.ByteString as B
5859import Data.ByteString.Internal as B
5960import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60−import Data.Text.Internal (Text(..))
61+import Data.Text.Internal (Text(..), textP)
6162import Data.Text.Private (runText)
6263import Data.Text.UnsafeChar (ord, unsafeWrite)
6364import Data.Text.UnsafeShift (shiftL, shiftR)
@@ -90,11 +91,23 @@ import qualified Data.Text.Fusion as F
9091-- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
9192-- encoded text.
9293--
93−-- This function is deprecated. Use 'decodeUtf8' instead.
94+-- This function is deprecated. Use 'decodeLatin1' instead.
9495decodeASCII :: ByteString -> Text
9596decodeASCII = decodeUtf8
9697{-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
9798
99+-- | Decode a 'ByteString' containing Latin-1 (aka ISO-8859-1) encoded text.
100+--
101+-- 'decodeLatin1' is semantically equivalent to
102+-- @Data.Text.pack . Data.ByteString.Char8.unpack@
103+decodeLatin1 :: ByteString -> Text
104+decodeLatin1 (PS fp off len) = textP a 0 len
105+ where
106+ a = A.run (A.new len >>= unsafeIOToST . go)
107+ go dest = withForeignPtr fp $ \ptr -> do
108+ c_decode_latin1 (A.maBA dest) (ptr `plusPtr` off) (ptr `plusPtr` (off+len))
109+ return dest
110+
98111-- | Decode a 'ByteString' containing UTF-8 encoded text.
99112decodeUtf8With :: OnDecodeError -> ByteString -> Text
100113decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
@@ -279,3 +292,6 @@ encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
279292foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
280293 :: MutableByteArray# s -> Ptr CSize
281294 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
295+
296+foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
297+ :: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()