on 802c8a8 help sign in
text/Data/Text/Encoding.hs
99 lines 3.1 kB history raw
1-- |
2-- Module : Data.Text.Encoding
3-- Copyright : (c) Tom Harper 2008-2009,
4-- (c) Bryan O'Sullivan 2009,
5-- (c) Duncan Coutts 2009
6--
7-- License : BSD-style
8-- Maintainer : rtharper@aftereternity.co.uk, bos@serpentine.com,
9-- duncan@haskell.org
10-- Stability : experimental
11-- Portability : portable
12--
13-- Functions for converting 'Text' values to and from 'ByteString',
14-- using several common encodings.
16module Data.Text.Encoding
17 (
18 -- * Decoding ByteStrings to Text
19 decodeASCII
20 , decodeUtf8
21 , decodeUtf16LE
22 , decodeUtf16BE
23 , decodeUtf32LE
24 , decodeUtf32BE
26 -- * Encoding Text to ByteStrings
27 , encodeASCII
28 , encodeUtf8
29 , encodeUtf16LE
30 , encodeUtf16BE
31 , encodeUtf32LE
32 , encodeUtf32BE
33 ) where
35import Data.ByteString (ByteString)
36import qualified Data.Text.Fusion as F
37import qualified Data.Text.Encoding.Fusion as E
38import Data.Text.Internal (Text)
40-- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
41decodeASCII :: ByteString -> Text
42decodeASCII bs = F.unstream (E.streamASCII bs)
43{-# INLINE decodeASCII #-}
45-- | Decode a 'ByteString' containing UTF-8 encoded text.
46decodeUtf8 :: ByteString -> Text
47decodeUtf8 bs = F.unstream (E.streamUtf8 bs)
48{-# INLINE decodeUtf8 #-}
50-- | Encode text using a 7-bit ASCII representation. /Note/: non-ASCII
51-- characters in the input 'Text' will be /truncated/.
52encodeASCII :: Text -> ByteString
53encodeASCII txt = E.unstream (E.restreamASCII (F.stream txt))
54{-# INLINE encodeASCII #-}
56-- | Encode text using UTF-8 encoding.
57encodeUtf8 :: Text -> ByteString
58encodeUtf8 txt = E.unstream (E.restreamUtf8 (F.stream txt))
59{-# INLINE encodeUtf8 #-}
61-- | Decode text from little endian UTF-16 encoding.
62decodeUtf16LE :: ByteString -> Text
63decodeUtf16LE bs = F.unstream (E.streamUtf16LE bs)
64{-# INLINE decodeUtf16LE #-}
66-- | Decode text from big endian UTF-16 encoding.
67decodeUtf16BE :: ByteString -> Text
68decodeUtf16BE bs = F.unstream (E.streamUtf16BE bs)
69{-# INLINE decodeUtf16BE #-}
71-- | Encode text using little endian UTF-16 encoding.
72encodeUtf16LE :: Text -> ByteString
73encodeUtf16LE txt = E.unstream (E.restreamUtf16LE (F.stream txt))
74{-# INLINE encodeUtf16LE #-}
76-- | Encode text using big endian UTF-16 encoding.
77encodeUtf16BE :: Text -> ByteString
78encodeUtf16BE txt = E.unstream (E.restreamUtf16BE (F.stream txt))
79{-# INLINE encodeUtf16BE #-}
81-- | Decode text from little endian UTF-32 encoding.
82decodeUtf32LE :: ByteString -> Text
83decodeUtf32LE bs = F.unstream (E.streamUtf32LE bs)
84{-# INLINE decodeUtf32LE #-}
86-- | Decode text from big endian UTF-32 encoding.
87decodeUtf32BE :: ByteString -> Text
88decodeUtf32BE bs = F.unstream (E.streamUtf32LE bs)
89{-# INLINE decodeUtf32BE #-}
91-- | Encode text using little endian UTF-32 encoding.
92encodeUtf32LE :: Text -> ByteString
93encodeUtf32LE txt = E.unstream (E.restreamUtf32LE (F.stream txt))
94{-# INLINE encodeUtf32LE #-}
96-- | Encode text using big endian UTF-32 encoding.
97encodeUtf32BE :: Text -> ByteString
98encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
99{-# INLINE encodeUtf32BE #-}