@@ -18,13 +18,8 @@ import Distribution.Utils.Generic (toUTF8BS)
1818import Prelude ()
1919
2020import qualified Data.ByteString as BS
21- import qualified Text.Parsec as Parsec
22-
23- #if MIN_VERSION_text(2,0,0)
2421import Data.Text.Internal.Encoding.Utf8
25- #else
26- import Data.Bits
27- #endif
22+ import qualified Text.Parsec as Parsec
2823
2924-- | This is essentially a lazy bytestring, but chunks are glued with newline @\'\\n\'@.
3025data FieldLineStream
@@ -53,7 +48,6 @@ instance Monad m => Parsec.Stream FieldLineStream m Char where
5348 Nothing -> Just (' \n ' , s)
5449 Just (c, bs') -> Just (unconsChar c bs' (\ bs'' -> FLSCons bs'' s) s)
5550
56- #if MIN_VERSION_text(2,0,0)
5751unconsChar :: forall a . Word8 -> ByteString -> (ByteString -> a ) -> a -> (Char , a )
5852unconsChar c0 bs0 f next = go (utf8DecodeStart c0) bs0
5953 where
@@ -63,49 +57,6 @@ unconsChar c0 bs0 f next = go (utf8DecodeStart c0) bs0
6357 Incomplete state codePoint -> case BS. uncons bs of
6458 Nothing -> (replacementChar, next)
6559 Just (w, bs') -> go (utf8DecodeContinue w state codePoint) bs'
66- #else
67- -- Based on implementation 'decodeStringUtf8'
68- unconsChar :: forall a . Word8 -> ByteString -> (ByteString -> a ) -> a -> (Char , a )
69- unconsChar c0 bs0 f next
70- | c0 <= 0x7F = (chr (fromIntegral c0), f bs0)
71- | c0 <= 0xBF = (replacementChar, f bs0)
72- | c0 <= 0xDF = twoBytes
73- | c0 <= 0xEF = moreBytes 3 0x800 bs0 (fromIntegral $ c0 .&. 0xF )
74- | c0 <= 0xF7 = moreBytes 4 0x10000 bs0 (fromIntegral $ c0 .&. 0x7 )
75- | c0 <= 0xFB = moreBytes 5 0x200000 bs0 (fromIntegral $ c0 .&. 0x3 )
76- | c0 <= 0xFD = moreBytes 6 0x4000000 bs0 (fromIntegral $ c0 .&. 0x1 )
77- | otherwise = error $ " not implemented " ++ show c0
78- where
79- twoBytes = case BS. uncons bs0 of
80- Nothing -> (replacementChar, next)
81- Just (c1, bs1)
82- | c1 .&. 0xC0 == 0x80 ->
83- if d >= 0x80
84- then (chr d, f bs1)
85- else (replacementChar, f bs1)
86- | otherwise -> (replacementChar, f bs1)
87- where
88- d = (fromIntegral (c0 .&. 0x1F ) `shiftL` 6 ) .|. fromIntegral (c1 .&. 0x3F )
89-
90- moreBytes :: Int -> Int -> ByteString -> Int -> (Char , a )
91- moreBytes 1 overlong bs' acc
92- | overlong <= acc
93- , acc <= 0x10FFFF
94- , acc < 0xD800 || 0xDFFF < acc =
95- (chr acc, f bs')
96- | otherwise =
97- (replacementChar, f bs')
98- moreBytes byteCount overlong bs' acc = case BS. uncons bs' of
99- Nothing -> (replacementChar, f bs')
100- Just (cn, bs1)
101- | cn .&. 0xC0 == 0x80 ->
102- moreBytes
103- (byteCount - 1 )
104- overlong
105- bs1
106- ((acc `shiftL` 6 ) .|. fromIntegral cn .&. 0x3F )
107- | otherwise -> (replacementChar, f bs1)
108- #endif
10960
11061replacementChar :: Char
11162replacementChar = ' \xfffd '
0 commit comments