@@ -23,6 +23,7 @@ module Data.Text.Encoding
23 -- * Decoding ByteStrings to Text
23 -- * Decoding ByteStrings to Text
24 -- $strict
24 -- $strict
25 decodeASCII
25 decodeASCII
26 , decodeUtf8
27 , decodeUtf8
27 , decodeUtf16LE
28 , decodeUtf16LE
28 , decodeUtf16BE
29 , decodeUtf16BE
@@ -57,7 +58,7 @@ import Data.Bits ((.&.))
57import Data.ByteString as B
58import Data.ByteString as B
58import Data.ByteString.Internal as B
59import Data.ByteString.Internal as B
59import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60import Data.Text.Encoding.Error (OnDecodeError, UnicodeException, strictDecode)
60−import Data.Text.Internal (Text(..))
61+import Data.Text.Internal (Text(..), textP)
61import Data.Text.Private (runText)
62import Data.Text.Private (runText)
62import Data.Text.UnsafeChar (ord, unsafeWrite)
63import Data.Text.UnsafeChar (ord, unsafeWrite)
63import Data.Text.UnsafeShift (shiftL, shiftR)
64import Data.Text.UnsafeShift (shiftL, shiftR)
@@ -90,11 +91,23 @@ import qualified Data.Text.Fusion as F
90-- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
91-- | /Deprecated/. Decode a 'ByteString' containing 7-bit ASCII
91-- encoded text.
92-- encoded text.
93−-- This function is deprecated. Use 'decodeUtf8' instead.
94+-- This function is deprecated. Use 'decodeLatin1' instead.
94decodeASCII :: ByteString -> Text
95decodeASCII :: ByteString -> Text
95decodeASCII = decodeUtf8
96decodeASCII = decodeUtf8
96{-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
97{-# DEPRECATED decodeASCII "Use decodeUtf8 instead" #-}
99+-- | Decode a 'ByteString' containing Latin-1 (aka ISO-8859-1) encoded text.
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
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))
98-- | Decode a 'ByteString' containing UTF-8 encoded text.
111-- | Decode a 'ByteString' containing UTF-8 encoded text.
99decodeUtf8With :: OnDecodeError -> ByteString -> Text
112decodeUtf8With :: OnDecodeError -> ByteString -> Text
100decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
113decodeUtf8With onErr (PS fp off len) = runText $ \done -> do
@@ -279,3 +292,6 @@ encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
279foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
292foreign import ccall unsafe "_hs_text_decode_utf8" c_decode_utf8
280 :: MutableByteArray# s -> Ptr CSize
293 :: MutableByteArray# s -> Ptr CSize
281 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
294 -> Ptr Word8 -> Ptr Word8 -> IO (Ptr Word8)
296+foreign import ccall unsafe "_hs_text_decode_latin1" c_decode_latin1
297+ :: MutableByteArray# s -> Ptr Word8 -> Ptr Word8 -> IO ()