2-- Module : Data.Text.Lazy.Encoding
3-- Copyright : (c) 2009, 2010 Bryan O'Sullivan
6-- Maintainer : bos@serpentine.com, rtomharper@googlemail.com,
8-- Stability : experimental
9-- Portability : portable
11-- Functions for converting lazy 'Text' values to and from lazy
12-- 'ByteString', using several standard encodings.
14-- To make use of a much larger variety of encodings, use the @text-icu@
17module Data.Text.Lazy.Encoding
19 -- * Decoding ByteStrings to Text
26 -- ** Controllable error handling
33 -- * Encoding Text to ByteStrings
41import qualified Data.ByteString.Lazy as B
42import Data.Text.Encoding.Error (OnDecodeError, strictDecode)
43import qualified Data.Text.Encoding as TE
44import qualified Data.Text.Lazy.Fusion as F
45import Data.Text.Lazy.Internal (Text(..), chunk, foldrChunks)
46import qualified Data.Text.Lazy.Encoding.Fusion as E
48-- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
49decodeASCII :: B.ByteString -> Text
50decodeASCII bs = foldr (chunk . TE.decodeASCII) Empty (B.toChunks bs)
51{-# INLINE decodeASCII #-}
53-- | Decode a 'ByteString' containing UTF-8 encoded text.
54decodeUtf8With :: OnDecodeError -> B.ByteString -> Text
55decodeUtf8With onErr bs = F.unstream (E.streamUtf8 onErr bs)
56{-# INLINE decodeUtf8With #-}
58-- | Decode a 'ByteString' containing UTF-8 encoded text.
59decodeUtf8 :: B.ByteString -> Text
60decodeUtf8 = decodeUtf8With strictDecode
61{-# INLINE decodeUtf8 #-}
63-- | Encode text using UTF-8 encoding.
64encodeUtf8 :: Text -> B.ByteString
65encodeUtf8 txt = E.unstream (E.restreamUtf8 (F.stream txt))
66{-# INLINE encodeUtf8 #-}
68-- | Decode text from little endian UTF-16 encoding.
69decodeUtf16LEWith :: OnDecodeError -> B.ByteString -> Text
70decodeUtf16LEWith onErr bs = F.unstream (E.streamUtf16LE onErr bs)
71{-# INLINE decodeUtf16LEWith #-}
73-- | Decode text from little endian UTF-16 encoding.
74decodeUtf16LE :: B.ByteString -> Text
75decodeUtf16LE = decodeUtf16LEWith strictDecode
76{-# INLINE decodeUtf16LE #-}
78-- | Decode text from big endian UTF-16 encoding.
79decodeUtf16BEWith :: OnDecodeError -> B.ByteString -> Text
80decodeUtf16BEWith onErr bs = F.unstream (E.streamUtf16BE onErr bs)
81{-# INLINE decodeUtf16BEWith #-}
83-- | Decode text from big endian UTF-16 encoding.
84decodeUtf16BE :: B.ByteString -> Text
85decodeUtf16BE = decodeUtf16BEWith strictDecode
86{-# INLINE decodeUtf16BE #-}
88-- | Encode text using little endian UTF-16 encoding.
89encodeUtf16LE :: Text -> B.ByteString
90encodeUtf16LE txt = B.fromChunks (foldrChunks ((:) . TE.encodeUtf16LE) [] txt)
91{-# INLINE encodeUtf16LE #-}
93-- | Encode text using big endian UTF-16 encoding.
94encodeUtf16BE :: Text -> B.ByteString
95encodeUtf16BE txt = B.fromChunks (foldrChunks ((:) . TE.encodeUtf16BE) [] txt)
96{-# INLINE encodeUtf16BE #-}
98-- | Decode text from little endian UTF-32 encoding.
99decodeUtf32LEWith :: OnDecodeError -> B.ByteString -> Text
100decodeUtf32LEWith onErr bs = F.unstream (E.streamUtf32LE onErr bs)
101{-# INLINE decodeUtf32LEWith #-}
103-- | Decode text from little endian UTF-32 encoding.
104decodeUtf32LE :: B.ByteString -> Text
105decodeUtf32LE = decodeUtf32LEWith strictDecode
106{-# INLINE decodeUtf32LE #-}
108-- | Decode text from big endian UTF-32 encoding.
109decodeUtf32BEWith :: OnDecodeError -> B.ByteString -> Text
110decodeUtf32BEWith onErr bs = F.unstream (E.streamUtf32BE onErr bs)
111{-# INLINE decodeUtf32BEWith #-}
113-- | Decode text from big endian UTF-32 encoding.
114decodeUtf32BE :: B.ByteString -> Text
115decodeUtf32BE = decodeUtf32BEWith strictDecode
116{-# INLINE decodeUtf32BE #-}
118-- | Encode text using little endian UTF-32 encoding.
119encodeUtf32LE :: Text -> B.ByteString
120encodeUtf32LE txt = B.fromChunks (foldrChunks ((:) . TE.encodeUtf32LE) [] txt)
121{-# INLINE encodeUtf32LE #-}
123-- | Encode text using big endian UTF-32 encoding.
124encodeUtf32BE :: Text -> B.ByteString
125encodeUtf32BE txt = B.fromChunks (foldrChunks ((:) . TE.encodeUtf32BE) [] txt)
126{-# INLINE encodeUtf32BE #-}
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list