module Data.Conduit.Text
(
Codec
, encode
, decode
, utf8
, utf16_le
, utf16_be
, utf32_le
, utf32_be
, ascii
, iso8859_1
, lines
, linesBounded
, TextException (..)
, takeWhile
, dropWhile
, take
, drop
, foldLines
, withLine
, Data.Conduit.Text.decodeUtf8
, decodeUtf8Lenient
, encodeUtf8
, detectUtf
) where
import qualified Prelude
import Prelude hiding (head, drop, takeWhile, lines, zip, zip3, zipWith, zipWith3, take, dropWhile)
import qualified Control.Exception as Exc
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as B8
import Data.Char (ord)
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
import Data.Word (Word8)
import Data.Typeable (Typeable)
import Data.Conduit
import qualified Data.Conduit.List as CL
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Resource (MonadThrow, monadThrow)
import Control.Monad (unless)
import Data.Streaming.Text
data Codec = Codec
{ codecName :: T.Text
, codecEncode
:: T.Text
-> (B.ByteString, Maybe (TextException, T.Text))
, codecDecode
:: B.ByteString
-> (T.Text, Either
(TextException, B.ByteString)
B.ByteString)
}
| NewCodec T.Text (T.Text -> B.ByteString) (B.ByteString -> DecodeResult)
instance Show Codec where
showsPrec d c =
let (cnst, name) = case c of
Codec t _ _ -> ("Codec ", t)
NewCodec t _ _ -> ("NewCodec ", t)
in showParen (d > 10) $ showString cnst . shows name
lines :: Monad m => Conduit T.Text m T.Text
lines =
awaitText T.empty
where
awaitText buf = await >>= maybe (finish buf) (process buf)
finish buf = unless (T.null buf) (yield buf)
process buf text = yieldLines $ buf `T.append` text
yieldLines buf =
let (line, rest) = T.break (== '\n') buf
in case T.uncons rest of
Just (_, rest') -> yield line >> yieldLines rest'
_ -> awaitText line
linesBounded :: MonadThrow m => Int -> Conduit T.Text m T.Text
linesBounded maxLineLen =
awaitText 0 T.empty
where
awaitText len buf = await >>= maybe (finish buf) (process len buf)
finish buf = unless (T.null buf) (yield buf)
process len buf text =
let (line, rest) = T.break (== '\n') text
len' = len + T.length line
in if len' > maxLineLen
then lift $ monadThrow (LengthExceeded maxLineLen)
else case T.uncons rest of
Just (_, rest') ->
yield (buf `T.append` line) >> process 0 T.empty rest'
_ ->
awaitText len' $ buf `T.append` text
encode :: MonadThrow m => Codec -> Conduit T.Text m B.ByteString
encode (NewCodec _ enc _) = CL.map enc
encode codec = CL.mapM $ \t -> do
let (bs, mexc) = codecEncode codec t
maybe (return bs) (monadThrow . fst) mexc
decodeNew
:: Monad m
=> (Int -> B.ByteString -> T.Text -> B.ByteString -> Conduit B.ByteString m T.Text)
-> t
-> Int
-> (B.ByteString -> DecodeResult)
-> Conduit B.ByteString m T.Text
decodeNew onFailure name =
loop
where
loop consumed dec =
await >>= maybe finish go
where
finish =
case dec B.empty of
DecodeResultSuccess _ _ -> return ()
DecodeResultFailure t rest -> onFailure consumed B.empty t rest
go bs | B.null bs = loop consumed dec
go bs =
case dec bs of
DecodeResultSuccess t dec' -> do
let consumed' = consumed + B.length bs
next = do
unless (T.null t) (yield t)
loop consumed' dec'
in consumed' `seq` next
DecodeResultFailure t rest -> onFailure consumed bs t rest
decodeUtf8Lenient :: Monad m => Conduit B.ByteString m T.Text
decodeUtf8Lenient =
decodeNew onFailure "UTF8-lenient" 0 Data.Streaming.Text.decodeUtf8
where
onFailure _consumed _bs t rest = do
unless (T.null t) (yield t)
case B.uncons rest of
Nothing -> return ()
Just (_, rest') -> do
unless (B.null rest') (leftover rest')
yield $ T.singleton '\xFFFD'
decodeUtf8Lenient
decode :: MonadThrow m => Codec -> Conduit B.ByteString m T.Text
decode (NewCodec name _ start) =
decodeNew onFailure name 0 start
where
onFailure consumed bs t rest = do
unless (T.null t) (yield t)
leftover rest
let consumed' = consumed + B.length bs B.length rest
monadThrow $ NewDecodeException name consumed' (B.take 4 rest)
decode codec =
loop id
where
loop front = await >>= maybe (finish front) (go front)
finish front =
case B.uncons $ front B.empty of
Nothing -> return ()
Just (w, _) -> lift $ monadThrow $ DecodeException codec w
go front bs' =
case extra of
Left (exc, _) -> lift $ monadThrow exc
Right bs'' -> yield text >> loop (B.append bs'')
where
(text, extra) = codecDecode codec bs
bs = front bs'
data TextException = DecodeException Codec Word8
| EncodeException Codec Char
| LengthExceeded Int
| TextException Exc.SomeException
| NewDecodeException !T.Text !Int !B.ByteString
deriving Typeable
instance Show TextException where
show (DecodeException codec w) = concat
[ "Error decoding legacy Data.Conduit.Text codec "
, show codec
, " when parsing byte: "
, show w
]
show (EncodeException codec c) = concat
[ "Error encoding legacy Data.Conduit.Text codec "
, show codec
, " when parsing char: "
, show c
]
show (LengthExceeded i) = "Data.Conduit.Text.linesBounded: line too long: " ++ show i
show (TextException se) = "Data.Conduit.Text.TextException: " ++ show se
show (NewDecodeException codec consumed next) = concat
[ "Data.Conduit.Text.decode: Error decoding stream of "
, T.unpack codec
, " bytes. Error encountered in stream at offset "
, show consumed
, ". Encountered at byte sequence "
, show next
]
instance Exc.Exception TextException
utf8 :: Codec
utf8 = NewCodec (T.pack "UTF-8") TE.encodeUtf8 Data.Streaming.Text.decodeUtf8
utf16_le :: Codec
utf16_le = NewCodec (T.pack "UTF-16-LE") TE.encodeUtf16LE decodeUtf16LE
utf16_be :: Codec
utf16_be = NewCodec (T.pack "UTF-16-BE") TE.encodeUtf16BE decodeUtf16BE
utf32_le :: Codec
utf32_le = NewCodec (T.pack "UTF-32-LE") TE.encodeUtf32LE decodeUtf32LE
utf32_be :: Codec
utf32_be = NewCodec (T.pack "UTF-32-BE") TE.encodeUtf32BE decodeUtf32BE
ascii :: Codec
ascii = Codec name enc dec where
name = T.pack "ASCII"
enc text = (bytes, extra) where
(safe, unsafe) = T.span (\c -> ord c <= 0x7F) text
bytes = B8.pack (T.unpack safe)
extra = if T.null unsafe
then Nothing
else Just (EncodeException ascii (T.head unsafe), unsafe)
dec bytes = (text, extra) where
(safe, unsafe) = B.span (<= 0x7F) bytes
text = T.pack (B8.unpack safe)
extra = if B.null unsafe
then Right B.empty
else Left (DecodeException ascii (B.head unsafe), unsafe)
iso8859_1 :: Codec
iso8859_1 = Codec name enc dec where
name = T.pack "ISO-8859-1"
enc text = (bytes, extra) where
(safe, unsafe) = T.span (\c -> ord c <= 0xFF) text
bytes = B8.pack (T.unpack safe)
extra = if T.null unsafe
then Nothing
else Just (EncodeException iso8859_1 (T.head unsafe), unsafe)
dec bytes = (T.pack (B8.unpack bytes), Right B.empty)
takeWhile :: Monad m
=> (Char -> Bool)
-> Conduit T.Text m T.Text
takeWhile p =
loop
where
loop = await >>= maybe (return ()) go
go t =
case T.span p t of
(x, y)
| T.null y -> yield x >> loop
| otherwise -> yield x >> leftover y
dropWhile :: Monad m
=> (Char -> Bool)
-> Consumer T.Text m ()
dropWhile p =
loop
where
loop = await >>= maybe (return ()) go
go t
| T.null x = loop
| otherwise = leftover x
where
x = T.dropWhile p t
take :: Monad m => Int -> Conduit T.Text m T.Text
take =
loop
where
loop i = await >>= maybe (return ()) (go i)
go i t
| diff == 0 = yield t
| diff < 0 =
let (x, y) = T.splitAt i t
in yield x >> leftover y
| otherwise = yield t >> loop diff
where
diff = i T.length t
drop :: Monad m => Int -> Consumer T.Text m ()
drop =
loop
where
loop i = await >>= maybe (return ()) (go i)
go i t
| diff == 0 = return ()
| diff < 0 = leftover $ T.drop i t
| otherwise = loop diff
where
diff = i T.length t
foldLines :: Monad m
=> (a -> ConduitM T.Text o m a)
-> a
-> ConduitM T.Text o m a
foldLines f =
start
where
start a = CL.peek >>= maybe (return a) (const $ loop $ f a)
loop consumer = do
a <- takeWhile (/= '\n') =$= do
a <- CL.map (T.filter (/= '\r')) =$= consumer
CL.sinkNull
return a
drop 1
start a
withLine :: Monad m
=> Sink T.Text m a
-> Consumer T.Text m (Maybe a)
withLine consumer = toConsumer $ do
mx <- CL.peek
case mx of
Nothing -> return Nothing
Just _ -> do
x <- takeWhile (/= '\n') =$ do
x <- CL.map (T.filter (/= '\r')) =$ consumer
CL.sinkNull
return x
drop 1
return $ Just x
decodeUtf8 :: MonadThrow m => Conduit B.ByteString m T.Text
decodeUtf8 = decode utf8
encodeUtf8 :: Monad m => Conduit T.Text m B.ByteString
encodeUtf8 = CL.map TE.encodeUtf8
detectUtf :: MonadThrow m => Conduit B.ByteString m T.Text
detectUtf =
go id
where
go front = await >>= maybe (close front) (push front)
push front bs'
| B.length bs < 4 = go $ B.append bs
| otherwise = leftDecode bs
where bs = front bs'
close front = leftDecode $ front B.empty
leftDecode bs = leftover bsOut >> decode codec
where
bsOut = B.append (B.drop toDrop x) y
(x, y) = B.splitAt 4 bs
(toDrop, codec) =
case B.unpack x of
[0x00, 0x00, 0xFE, 0xFF] -> (4, utf32_be)
[0xFF, 0xFE, 0x00, 0x00] -> (4, utf32_le)
0xFE : 0xFF: _ -> (2, utf16_be)
0xFF : 0xFE: _ -> (2, utf16_le)
0xEF : 0xBB: 0xBF : _ -> (3, utf8)
_ -> (0, utf8)