diff options
Diffstat (limited to 'Pipes/Text/Internal/Decoding.hs')
-rw-r--r-- | Pipes/Text/Internal/Decoding.hs | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/Pipes/Text/Internal/Decoding.hs b/Pipes/Text/Internal/Decoding.hs index 531104a..4b4bbe6 100644 --- a/Pipes/Text/Internal/Decoding.hs +++ b/Pipes/Text/Internal/Decoding.hs | |||
@@ -2,9 +2,11 @@ | |||
2 | {-# LANGUAGE GeneralizedNewtypeDeriving, MagicHash, UnliftedFFITypes #-} | 2 | {-# LANGUAGE GeneralizedNewtypeDeriving, MagicHash, UnliftedFFITypes #-} |
3 | {-# LANGUAGE DeriveDataTypeable, RankNTypes #-} | 3 | {-# LANGUAGE DeriveDataTypeable, RankNTypes #-} |
4 | 4 | ||
5 | -- This module lifts assorted materials from Brian O'Sullivan's text package | 5 | {- | |
6 | -- especially Data.Text.Encoding in order to define a pipes-appropriate | 6 | This module lifts assorted materials from Brian O'Sullivan's text package |
7 | -- streamDecodeUtf8 | 7 | especially @Data.Text.Encoding@ in order to define a pipes-appropriate |
8 | 'streamDecodeUtf8' | ||
9 | -} | ||
8 | module Pipes.Text.Internal.Decoding | 10 | module Pipes.Text.Internal.Decoding |
9 | ( Decoding(..) | 11 | ( Decoding(..) |
10 | , streamDecodeUtf8 | 12 | , streamDecodeUtf8 |
@@ -41,9 +43,9 @@ import Data.Maybe (catMaybes) | |||
41 | 43 | ||
42 | 44 | ||
43 | 45 | ||
44 | -- | A stream oriented decoding result. | 46 | -- | A stream oriented decoding result. Distinct from the similar type in @Data.Text.Encoding@ |
45 | data Decoding = Some Text ByteString (ByteString -> Decoding) | 47 | data Decoding = Some Text ByteString (ByteString -> Decoding) -- | Text, continuation and any undecoded fragment. |
46 | | Other Text ByteString | 48 | | Other Text ByteString -- | Text followed by an undecodable ByteString |
47 | instance Show Decoding where | 49 | instance Show Decoding where |
48 | showsPrec d (Some t bs _) = showParen (d > prec) $ | 50 | showsPrec d (Some t bs _) = showParen (d > prec) $ |
49 | showString "Some " . showsPrec prec' t . | 51 | showString "Some " . showsPrec prec' t . |
@@ -59,6 +61,7 @@ instance Show Decoding where | |||
59 | newtype CodePoint = CodePoint Word32 deriving (Eq, Show, Num, Storable) | 61 | newtype CodePoint = CodePoint Word32 deriving (Eq, Show, Num, Storable) |
60 | newtype DecoderState = DecoderState Word32 deriving (Eq, Show, Num, Storable) | 62 | newtype DecoderState = DecoderState Word32 deriving (Eq, Show, Num, Storable) |
61 | 63 | ||
64 | -- | Resolve a 'ByteString' into 'Text' and a continuation that can handle further 'ByteStrings'. | ||
62 | streamDecodeUtf8 :: ByteString -> Decoding | 65 | streamDecodeUtf8 :: ByteString -> Decoding |
63 | streamDecodeUtf8 = decodeChunkUtf8 B.empty 0 0 | 66 | streamDecodeUtf8 = decodeChunkUtf8 B.empty 0 0 |
64 | where | 67 | where |
@@ -92,6 +95,7 @@ streamDecodeUtf8 = decodeChunkUtf8 B.empty 0 0 | |||
92 | {-# INLINE decodeChunkUtf8 #-} | 95 | {-# INLINE decodeChunkUtf8 #-} |
93 | {-# INLINE streamDecodeUtf8 #-} | 96 | {-# INLINE streamDecodeUtf8 #-} |
94 | 97 | ||
98 | -- | Resolve a ByteString into an initial segment of intelligible 'Text' and whatever is unintelligble | ||
95 | decodeSomeUtf8 :: ByteString -> (Text, ByteString) | 99 | decodeSomeUtf8 :: ByteString -> (Text, ByteString) |
96 | decodeSomeUtf8 bs@(PS fp off len) = runST $ do | 100 | decodeSomeUtf8 bs@(PS fp off len) = runST $ do |
97 | dest <- A.new (len+1) | 101 | dest <- A.new (len+1) |