on 751bf24 help sign in
text/Data/Text/Encoding.hs
95 lines 3.0 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 standard encodings.
15--
16-- To make use of a much larger variety of encodings, use the @text-icu@
17-- package.
19module Data.Text.Encoding
20 (
21 -- * Decoding ByteStrings to Text
22 decodeASCII
23 , decodeUtf8
24 , decodeUtf16LE
25 , decodeUtf16BE
26 , decodeUtf32LE
27 , decodeUtf32BE
29 -- * Encoding Text to ByteStrings
30 , encodeUtf8
31 , encodeUtf16LE
32 , encodeUtf16BE
33 , encodeUtf32LE
34 , encodeUtf32BE
35 ) where
37import Data.ByteString (ByteString)
38import qualified Data.Text.Fusion as F
39import qualified Data.Text.Encoding.Fusion as E
40import Data.Text.Internal (Text)
42-- | Decode a 'ByteString' containing 7-bit ASCII encoded text.
43decodeASCII :: ByteString -> Text
44decodeASCII bs = F.unstream (E.streamASCII bs)
45{-# INLINE decodeASCII #-}
47-- | Decode a 'ByteString' containing UTF-8 encoded text.
48decodeUtf8 :: ByteString -> Text
49decodeUtf8 bs = F.unstream (E.streamUtf8 bs)
50{-# INLINE decodeUtf8 #-}
52-- | Encode text using UTF-8 encoding.
53encodeUtf8 :: Text -> ByteString
54encodeUtf8 txt = E.unstream (E.restreamUtf8 (F.stream txt))
55{-# INLINE encodeUtf8 #-}
57-- | Decode text from little endian UTF-16 encoding.
58decodeUtf16LE :: ByteString -> Text
59decodeUtf16LE bs = F.unstream (E.streamUtf16LE bs)
60{-# INLINE decodeUtf16LE #-}
62-- | Decode text from big endian UTF-16 encoding.
63decodeUtf16BE :: ByteString -> Text
64decodeUtf16BE bs = F.unstream (E.streamUtf16BE bs)
65{-# INLINE decodeUtf16BE #-}
67-- | Encode text using little endian UTF-16 encoding.
68encodeUtf16LE :: Text -> ByteString
69encodeUtf16LE txt = E.unstream (E.restreamUtf16LE (F.stream txt))
70{-# INLINE encodeUtf16LE #-}
72-- | Encode text using big endian UTF-16 encoding.
73encodeUtf16BE :: Text -> ByteString
74encodeUtf16BE txt = E.unstream (E.restreamUtf16BE (F.stream txt))
75{-# INLINE encodeUtf16BE #-}
77-- | Decode text from little endian UTF-32 encoding.
78decodeUtf32LE :: ByteString -> Text
79decodeUtf32LE bs = F.unstream (E.streamUtf32LE bs)
80{-# INLINE decodeUtf32LE #-}
82-- | Decode text from big endian UTF-32 encoding.
83decodeUtf32BE :: ByteString -> Text
84decodeUtf32BE bs = F.unstream (E.streamUtf32BE bs)
85{-# INLINE decodeUtf32BE #-}
87-- | Encode text using little endian UTF-32 encoding.
88encodeUtf32LE :: Text -> ByteString
89encodeUtf32LE txt = E.unstream (E.restreamUtf32LE (F.stream txt))
90{-# INLINE encodeUtf32LE #-}
92-- | Encode text using big endian UTF-32 encoding.
93encodeUtf32BE :: Text -> ByteString
94encodeUtf32BE txt = E.unstream (E.restreamUtf32BE (F.stream txt))
95{-# INLINE encodeUtf32BE #-}