2-- Module : Streamly.Internal.Unicode.Stream
3-- Copyright : (c) 2018 Composewell Technologies
4-- (c) Bjoern Hoehrmann 2008-2009
6-- License : BSD-3-Clause
7-- Maintainer : streamly@composewell.com
8-- Stability : experimental
12module Streamly.Internal.Unicode.Stream
14 -- * Construction (Decoding)
22 -- ** Resumable UTF-8 Decoding
27 , resumeDecodeUtf8Either
29 -- ** UTF-8 Array Stream Decoding
34 -- * Elimination (Encoding)
46 -- * Operations on character strings
47 , strip -- (dropAround isSpace)
58 -- * StreamD UTF8 Encoding / Decoding transformations.
66 , resumeDecodeUtf8EitherD
80import Control.Monad.IO.Class (MonadIO, liftIO)
81import Data.Bits (shiftR, shiftL, (.|.), (.&.))
82import Data.Char (chr, ord)
83import Data.Word (Word8)
84import Foreign.ForeignPtr (touchForeignPtr)
85import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
86import Foreign.Storable (Storable(..))
87import Fusion.Plugin.Types (Fuse(..))
88import GHC.Base (assert, unsafeChr)
89import GHC.ForeignPtr (ForeignPtr (..))
90import GHC.IO.Encoding.Failure (isSurrogate)
91import GHC.Ptr (Ptr (..), plusPtr)
92import System.IO.Unsafe (unsafePerformIO)
93import Streamly.Data.Fold (Fold)
94import Streamly.Data.Array.Foreign (Array)
95import Streamly.Internal.Data.Unfold (Unfold)
96import Streamly.Internal.Data.SVar (adaptState)
97import Streamly.Internal.Data.Stream.IsStream (IsStream)
98import Streamly.Internal.Data.Stream.Serial (SerialT)
99import Streamly.Internal.Data.Stream.StreamD (Stream(..), Step (..))
100import Streamly.Internal.Data.Tuple.Strict (Tuple'(..))
102import qualified Streamly.Internal.Data.Unfold as Unfold
103import qualified Streamly.Internal.Data.Stream.Serial as Serial
104import qualified Streamly.Internal.Data.Array.Foreign as Array
105import qualified Streamly.Internal.Data.Array.Foreign.Type as A
106import qualified Streamly.Internal.Data.Stream.IsStream as S
107import qualified Streamly.Internal.Data.Stream.StreamD as D
109import Prelude hiding (lines, words, unlines, unwords)
113-- >>> import Prelude hiding (lines, words, unlines, unwords)
114-- >>> import qualified Streamly.Prelude as Stream
115-- >>> import qualified Streamly.Data.Fold as Fold
116-- >>> import Streamly.Internal.Unicode.Stream
118-------------------------------------------------------------------------------
120-------------------------------------------------------------------------------
122-- | Decode a stream of bytes to Unicode characters by mapping each byte to a
123-- corresponding Unicode 'Char' in 0-255 range.
125-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
128{-# INLINE decodeLatin1 #-}
129decodeLatin1 :: (IsStream t, Monad m) => t m Word8 -> t m Char
130decodeLatin1 = S.map (unsafeChr . fromIntegral)
132-------------------------------------------------------------------------------
134-------------------------------------------------------------------------------
136-- | Encode a stream of Unicode characters to bytes by mapping each character
137-- to a byte in 0-255 range. Throws an error if the input stream contains
138-- characters beyond 255.
141{-# INLINE encodeLatin1' #-}
142encodeLatin1' :: (IsStream t, Monad m) => t m Char -> t m Word8
143encodeLatin1' = S.map convert
146 let codepoint = ord c
147 in if codepoint > 255
148 then error $ "Streamly.String.encodeLatin1 invalid " ++
149 "input char codepoint " ++ show codepoint
150 else fromIntegral codepoint
152-- | Like 'encodeLatin1'' but silently truncates and maps input characters beyond
153-- 255 to (incorrect) chars in 0-255 range. No error or exception is thrown
154-- when such truncation occurs.
156-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
158-- /Since: 0.8.0 (Lenient Behaviour)/
159{-# INLINE encodeLatin1 #-}
160encodeLatin1 :: (IsStream t, Monad m) => t m Char -> t m Word8
161encodeLatin1 = S.map (fromIntegral . ord)
163-- | Like 'encodeLatin1' but drops the input characters beyond 255.
166{-# INLINE encodeLatin1_ #-}
167encodeLatin1_ :: (IsStream t, Monad m) => t m Char -> t m Word8
168encodeLatin1_ = S.map (fromIntegral . ord) . S.filter (<= chr 255)
170-- | Same as 'encodeLatin1'
172{-# DEPRECATED encodeLatin1Lax "Please use 'encodeLatin1' instead" #-}
173{-# INLINE encodeLatin1Lax #-}
174encodeLatin1Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
175encodeLatin1Lax = encodeLatin1
177-------------------------------------------------------------------------------
179-------------------------------------------------------------------------------
181-- Int helps in cheaper conversion from Int to Char
183type DecodeState = Word8
185-- We can divide the errors in three general categories:
186-- * A non-starter was encountered in a begin state
187-- * A starter was encountered without completing a codepoint
188-- * The last codepoint was not complete (input underflow)
190-- Need to separate resumable and non-resumable error. In case of non-resumable
191-- error we can also provide the failing byte. In case of resumable error the
192-- state can be opaque.
194data DecodeError = DecodeError !DecodeState !CodePoint deriving Show
196-- See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
198-- XXX Use names decodeSuccess = 0, decodeFailure = 12
200decodeTable :: [Word8]
202 -- The first part of the table maps bytes to character classes that
203 -- to reduce the size of the transition table and create bitmasks.
204 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
205 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
206 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
207 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
208 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
209 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
210 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
211 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
213 -- The second part is a transition table that maps a combination
214 -- of a state of the automaton and a character class to a state.
215 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
216 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
217 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
218 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
219 12,36,12,12,12,12,12,12,12,12,12,12
222{-# NOINLINE utf8d #-}
223utf8d :: A.Array Word8
226 -- Aligning to cacheline makes a barely noticeable difference
227 -- XXX currently alignment is not implemented for unmanaged allocation
228 $ D.fold (A.writeNAlignedUnmanaged 64 (length decodeTable))
229 (D.fromList decodeTable)
231-- | Return element at the specified index without checking the bounds.
232-- and without touching the foreign ptr.
233{-# INLINE_NORMAL unsafePeekElemOff #-}
234unsafePeekElemOff :: forall a. Storable a => Ptr a -> Int -> a
235unsafePeekElemOff p i = let !x = A.unsafeInlineIO $ peekElemOff p i in x
237-- decode is split into two separate cases to avoid branching instructions.
238-- From the higher level flow we already know which case we are in so we can
239-- call the appropriate decode function.
241-- When the state is 0
242{-# INLINE decode0 #-}
243decode0 :: Ptr Word8 -> Word8 -> Tuple' DecodeState CodePoint
245 let !t = table `unsafePeekElemOff` fromIntegral byte
246 !codep' = (0xff `shiftR` fromIntegral t) .&. fromIntegral byte
247 !state' = table `unsafePeekElemOff` (256 + fromIntegral t)
248 in assert ((byte > 0x7f || error showByte)
249 && (state' /= 0 || error (showByte ++ showTable)))
250 (Tuple' state' codep')
255 let !(Ptr addr) = table
256 end = table `plusPtr` 364
257 in A.Array (ForeignPtr addr undefined) end :: A.Array Word8
258 showByte = "Streamly: decode0: byte: " ++ show byte
259 showTable = " table: " ++ show utf8table
261-- When the state is not 0
262{-# INLINE decode1 #-}
268 -> Tuple' DecodeState CodePoint
269decode1 table state codep byte =
270 -- Remember codep is Int type!
271 -- Can it be unsafe to convert the resulting Int to Char?
272 let !t = table `unsafePeekElemOff` fromIntegral byte
273 !codep' = (fromIntegral byte .&. 0x3f) .|. (codep `shiftL` 6)
274 !state' = table `unsafePeekElemOff`
275 (256 + fromIntegral state + fromIntegral t)
276 in assert (codep' <= 0x10FFFF
277 || error (showByte ++ showState state codep))
278 (Tuple' state' codep')
282 let !(Ptr addr) = table
283 end = table `plusPtr` 364
284 in A.Array (ForeignPtr addr undefined) end :: A.Array Word8
285 showByte = "Streamly: decode1: byte: " ++ show byte
287 " state: " ++ show st ++
288 " codepoint: " ++ show cp ++
289 " table: " ++ show utf8table
291-------------------------------------------------------------------------------
292-- Resumable UTF-8 decoding
293-------------------------------------------------------------------------------
295{-# ANN type UTF8DecodeState Fuse #-}
296data UTF8DecodeState s a
298 | UTF8DecodeInit1 s Word8
299 | UTF8DecodeFirst s Word8
300 | UTF8Decoding s !DecodeState !CodePoint
301 | YieldAndContinue a (UTF8DecodeState s a)
304{-# INLINE_NORMAL resumeDecodeUtf8EitherD #-}
305resumeDecodeUtf8EitherD
310 -> Stream m (Either DecodeError Char)
311resumeDecodeUtf8EitherD dst codep (Stream step state) =
312 let A.Array p _ = utf8d
313 !ptr = unsafeForeignPtrToPtr p
316 then UTF8DecodeInit state
317 else UTF8Decoding state dst codep
318 in Stream (step' ptr) stt
320 {-# INLINE_LATE step' #-}
321 step' _ gst (UTF8DecodeInit st) = do
322 r <- step (adaptState gst) st
324 Yield x s -> Skip (UTF8DecodeInit1 s x)
325 Skip s -> Skip (UTF8DecodeInit s)
328 step' _ _ (UTF8DecodeInit1 st x) = do
329 -- Note: It is important to use a ">" instead of a "<=" test
330 -- here for GHC to generate code layout for default branch
331 -- prediction for the common case. This is fragile and might
332 -- change with the compiler versions, we need a more reliable
333 -- "likely" primitive to control branch predication.
336 return $ Skip $ YieldAndContinue
337 (Right $ unsafeChr (fromIntegral x))
339 -- Using a separate state here generates a jump to a
340 -- separate code block in the core which seems to perform
341 -- slightly better for the non-ascii case.
342 True -> return $ Skip $ UTF8DecodeFirst st x
344 -- XXX should we merge it with UTF8DecodeInit1?
345 step' table _ (UTF8DecodeFirst st x) = do
346 let (Tuple' sv cp) = decode0 table x
350 Skip $ YieldAndContinue (Left $ DecodeError 0 (fromIntegral x))
352 0 -> error "unreachable state"
353 _ -> Skip (UTF8Decoding st sv cp)
355 -- We recover by trying the new byte x a starter of a new codepoint.
356 -- XXX on error need to report the next byte "x" as well.
357 -- XXX need to use the same recovery in array decoding routine as well
358 step' table gst (UTF8Decoding st statePtr codepointPtr) = do
359 r <- step (adaptState gst) st
362 let (Tuple' sv cp) = decode1 table statePtr codepointPtr x
365 0 -> Skip $ YieldAndContinue (Right $ unsafeChr cp)
368 Skip $ YieldAndContinue (Left $ DecodeError statePtr codepointPtr)
369 (UTF8DecodeInit1 s x)
370 _ -> Skip (UTF8Decoding s sv cp)
371 Skip s -> return $ Skip (UTF8Decoding s statePtr codepointPtr)
372 Stop -> return $ Skip $ YieldAndContinue (Left $ DecodeError statePtr codepointPtr) Done
374 step' _ _ (YieldAndContinue c s) = return $ Yield c s
375 step' _ _ Done = return Stop
377-- XXX We can use just one API, and define InitState = 0 and InitCodePoint = 0
378-- to use as starting state.
380{-# INLINE_NORMAL decodeUtf8EitherD #-}
381decodeUtf8EitherD :: Monad m
382 => Stream m Word8 -> Stream m (Either DecodeError Char)
383decodeUtf8EitherD = resumeDecodeUtf8EitherD 0 0
388{-# INLINE decodeUtf8Either #-}
389decodeUtf8Either :: (Monad m, IsStream t)
390 => t m Word8 -> t m (Either DecodeError Char)
391decodeUtf8Either = D.fromStreamD . decodeUtf8EitherD . D.toStreamD
396{-# INLINE resumeDecodeUtf8Either #-}
397resumeDecodeUtf8Either
398 :: (Monad m, IsStream t)
402 -> t m (Either DecodeError Char)
403resumeDecodeUtf8Either st cp =
404 D.fromStreamD . resumeDecodeUtf8EitherD st cp . D.toStreamD
406-------------------------------------------------------------------------------
408-------------------------------------------------------------------------------
410data CodingFailureMode
411 = TransliterateCodingFailure
412 | ErrorOnCodingFailure
413 | DropOnCodingFailure
416{-# INLINE replacementChar #-}
417replacementChar :: Char
418replacementChar = '\xFFFD'
420-- XXX write it as a parser and use parseMany to decode a stream, need to check
421-- if that preserves the same performance. Or we can use a resumable parser
422-- that parses a chunk at a time.
424-- XXX Implement this in terms of decodeUtf8Either. Need to make sure that
425-- decodeUtf8Either preserves the performance characterstics.
427{-# INLINE_NORMAL decodeUtf8WithD #-}
428decodeUtf8WithD :: Monad m
429 => CodingFailureMode -> Stream m Word8 -> Stream m Char
430decodeUtf8WithD cfm (Stream step state) =
431 let A.Array p _ = utf8d
432 !ptr = unsafeForeignPtrToPtr p
433 in Stream (step' ptr) (UTF8DecodeInit state)
437 prefix = "Streamly.Internal.Data.Stream.StreamD.decodeUtf8With: "
439 {-# INLINE handleError #-}
442 ErrorOnCodingFailure -> error e
443 TransliterateCodingFailure -> YieldAndContinue replacementChar s
444 DropOnCodingFailure -> s
446 {-# INLINE handleUnderflow #-}
449 ErrorOnCodingFailure -> error $ prefix ++ "Not enough input"
450 TransliterateCodingFailure -> YieldAndContinue replacementChar Done
451 DropOnCodingFailure -> Done
453 {-# INLINE_LATE step' #-}
454 step' _ gst (UTF8DecodeInit st) = do
455 r <- step (adaptState gst) st
457 Yield x s -> Skip (UTF8DecodeInit1 s x)
458 Skip s -> Skip (UTF8DecodeInit s)
461 step' _ _ (UTF8DecodeInit1 st x) = do
462 -- Note: It is important to use a ">" instead of a "<=" test
463 -- here for GHC to generate code layout for default branch
464 -- prediction for the common case. This is fragile and might
465 -- change with the compiler versions, we need a more reliable
466 -- "likely" primitive to control branch predication.
469 return $ Skip $ YieldAndContinue
470 (unsafeChr (fromIntegral x))
472 -- Using a separate state here generates a jump to a
473 -- separate code block in the core which seems to perform
474 -- slightly better for the non-ascii case.
475 True -> return $ Skip $ UTF8DecodeFirst st x
477 -- XXX should we merge it with UTF8DecodeInit1?
478 step' table _ (UTF8DecodeFirst st x) = do
479 let (Tuple' sv cp) = decode0 table x
483 let msg = prefix ++ "Invalid first UTF8 byte " ++ show x
484 in Skip $ handleError msg (UTF8DecodeInit st)
485 0 -> error "unreachable state"
486 _ -> Skip (UTF8Decoding st sv cp)
488 -- We recover by trying the new byte x as a starter of a new codepoint.
489 -- XXX need to use the same recovery in array decoding routine as well
490 step' table gst (UTF8Decoding st statePtr codepointPtr) = do
491 r <- step (adaptState gst) st
494 let (Tuple' sv cp) = decode1 table statePtr codepointPtr x
496 0 -> Skip $ YieldAndContinue
497 (unsafeChr cp) (UTF8DecodeInit s)
500 ++ "Invalid subsequent UTF8 byte "
504 ++ " accumulated value "
506 in Skip $ handleError msg (UTF8DecodeInit1 s x)
507 _ -> Skip (UTF8Decoding s sv cp)
509 Skip (UTF8Decoding s statePtr codepointPtr)
510 Stop -> return $ Skip handleUnderflow
512 step' _ _ (YieldAndContinue c s) = return $ Yield c s
513 step' _ _ Done = return Stop
515{-# INLINE decodeUtf8D #-}
516decodeUtf8D :: Monad m => Stream m Word8 -> Stream m Char
517decodeUtf8D = decodeUtf8WithD TransliterateCodingFailure
519-- | Decode a UTF-8 encoded bytestream to a stream of Unicode characters.
520-- Any invalid codepoint encountered is replaced with the unicode replacement
523-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
525-- /Since: 0.8.0 (Lenient Behaviour)/
526{-# INLINE decodeUtf8 #-}
527decodeUtf8 :: (Monad m, IsStream t) => t m Word8 -> t m Char
528decodeUtf8 = D.fromStreamD . decodeUtf8D . D.toStreamD
530{-# INLINE decodeUtf8D' #-}
531decodeUtf8D' :: Monad m => Stream m Word8 -> Stream m Char
532decodeUtf8D' = decodeUtf8WithD ErrorOnCodingFailure
534-- | Decode a UTF-8 encoded bytestream to a stream of Unicode characters.
535-- The function throws an error if an invalid codepoint is encountered.
538{-# INLINE decodeUtf8' #-}
539decodeUtf8' :: (Monad m, IsStream t) => t m Word8 -> t m Char
540decodeUtf8' = D.fromStreamD . decodeUtf8D' . D.toStreamD
542{-# INLINE decodeUtf8D_ #-}
543decodeUtf8D_ :: Monad m => Stream m Word8 -> Stream m Char
544decodeUtf8D_ = decodeUtf8WithD DropOnCodingFailure
546-- | Decode a UTF-8 encoded bytestream to a stream of Unicode characters.
547-- Any invalid codepoint encountered is dropped.
550{-# INLINE decodeUtf8_ #-}
551decodeUtf8_ :: (Monad m, IsStream t) => t m Word8 -> t m Char
552decodeUtf8_ = D.fromStreamD . decodeUtf8D_ . D.toStreamD
554-- | Same as 'decodeUtf8'
556{-# DEPRECATED decodeUtf8Lax "Please use 'decodeUtf8' instead" #-}
557{-# INLINE decodeUtf8Lax #-}
558decodeUtf8Lax :: (IsStream t, Monad m) => t m Word8 -> t m Char
559decodeUtf8Lax = decodeUtf8
561-------------------------------------------------------------------------------
562-- Decoding Array Streams
563-------------------------------------------------------------------------------
565{-# ANN type FlattenState Fuse #-}
567 = OuterLoop s !(Maybe (DecodeState, CodePoint))
568 | InnerLoopDecodeInit s (ForeignPtr a) !(Ptr a) !(Ptr a)
569 | InnerLoopDecodeFirst s (ForeignPtr a) !(Ptr a) !(Ptr a) Word8
570 | InnerLoopDecoding s (ForeignPtr a) !(Ptr a) !(Ptr a)
571 !DecodeState !CodePoint
572 | YAndC !Char (FlattenState s a) -- These constructors can be
573 -- encoded in the UTF8DecodeState
574 -- type, I prefer to keep these
575 -- flat even though that means
576 -- coming up with new names
579-- The normal decodeUtf8 above should fuse with flattenArrays
580-- to create this exact code but it doesn't for some reason, as of now this
581-- remains the fastest way I could figure out to decodeUtf8.
583-- XXX Add Proper error messages
584{-# INLINE_NORMAL decodeUtf8ArraysWithD #-}
585decodeUtf8ArraysWithD ::
588 -> Stream m (A.Array Word8)
590decodeUtf8ArraysWithD cfm (Stream step state) =
591 let A.Array p _ = utf8d
592 !ptr = unsafeForeignPtrToPtr p
593 in Stream (step' ptr) (OuterLoop state Nothing)
595 {-# INLINE transliterateOrError #-}
596 transliterateOrError e s =
598 ErrorOnCodingFailure -> error e
599 TransliterateCodingFailure -> YAndC replacementChar s
600 DropOnCodingFailure -> s
601 {-# INLINE inputUnderflow #-}
604 ErrorOnCodingFailure ->
606 show "Streamly.Internal.Data.Stream.StreamD."
607 ++ "decodeUtf8ArraysWith: Input Underflow"
608 TransliterateCodingFailure -> YAndC replacementChar D
609 DropOnCodingFailure -> D
610 {-# INLINE_LATE step' #-}
611 step' _ gst (OuterLoop st Nothing) = do
612 r <- step (adaptState gst) st
615 Yield A.Array {..} s ->
616 let p = unsafeForeignPtrToPtr aStart
617 in Skip (InnerLoopDecodeInit s aStart p aEnd)
618 Skip s -> Skip (OuterLoop s Nothing)
620 step' _ gst (OuterLoop st dst@(Just (ds, cp))) = do
621 r <- step (adaptState gst) st
624 Yield A.Array {..} s ->
625 let p = unsafeForeignPtrToPtr aStart
626 in Skip (InnerLoopDecoding s aStart p aEnd ds cp)
627 Skip s -> Skip (OuterLoop s dst)
628 Stop -> Skip inputUnderflow
629 step' _ _ (InnerLoopDecodeInit st startf p end)
631 liftIO $ touchForeignPtr startf
632 return $ Skip $ OuterLoop st Nothing
633 step' _ _ (InnerLoopDecodeInit st startf p end) = do
635 -- Note: It is important to use a ">" instead of a "<=" test here for
636 -- GHC to generate code layout for default branch prediction for the
637 -- common case. This is fragile and might change with the compiler
638 -- versions, we need a more reliable "likely" primitive to control
639 -- branch predication.
642 return $ Skip $ YAndC
643 (unsafeChr (fromIntegral x))
644 (InnerLoopDecodeInit st startf (p `plusPtr` 1) end)
645 -- Using a separate state here generates a jump to a separate code
646 -- block in the core which seems to perform slightly better for the
648 True -> return $ Skip $ InnerLoopDecodeFirst st startf p end x
650 step' table _ (InnerLoopDecodeFirst st startf p end x) = do
651 let (Tuple' sv cp) = decode0 table x
658 "Streamly.Internal.Data.Stream.StreamD."
659 ++ "decodeUtf8ArraysWith: Invalid UTF8"
660 ++ " codepoint encountered"
662 (InnerLoopDecodeInit st startf (p `plusPtr` 1) end)
663 0 -> error "unreachable state"
664 _ -> Skip (InnerLoopDecoding st startf (p `plusPtr` 1) end sv cp)
665 step' _ _ (InnerLoopDecoding st startf p end sv cp)
667 liftIO $ touchForeignPtr startf
668 return $ Skip $ OuterLoop st (Just (sv, cp))
669 step' table _ (InnerLoopDecoding st startf p end statePtr codepointPtr) = do
671 let (Tuple' sv cp) = decode1 table statePtr codepointPtr x
678 (InnerLoopDecodeInit st startf (p `plusPtr` 1) end)
683 "Streamly.Internal.Data.Stream.StreamD."
684 ++ "decodeUtf8ArraysWith: Invalid UTF8"
685 ++ " codepoint encountered"
687 (InnerLoopDecodeInit st startf (p `plusPtr` 1) end)
690 (InnerLoopDecoding st startf (p `plusPtr` 1) end sv cp)
691 step' _ _ (YAndC c s) = return $ Yield c s
692 step' _ _ D = return Stop
694{-# INLINE decodeUtf8ArraysD #-}
697 => Stream m (A.Array Word8)
699decodeUtf8ArraysD = decodeUtf8ArraysWithD TransliterateCodingFailure
704{-# INLINE decodeUtf8Arrays #-}
706 (MonadIO m, IsStream t) => t m (Array Word8) -> t m Char
708 D.fromStreamD . decodeUtf8ArraysD . D.toStreamD
710{-# INLINE decodeUtf8ArraysD' #-}
713 => Stream m (A.Array Word8)
715decodeUtf8ArraysD' = decodeUtf8ArraysWithD ErrorOnCodingFailure
720{-# INLINE decodeUtf8Arrays' #-}
721decodeUtf8Arrays' :: (MonadIO m, IsStream t) => t m (Array Word8) -> t m Char
722decodeUtf8Arrays' = D.fromStreamD . decodeUtf8ArraysD' . D.toStreamD
724{-# INLINE decodeUtf8ArraysD_ #-}
727 => Stream m (A.Array Word8)
729decodeUtf8ArraysD_ = decodeUtf8ArraysWithD DropOnCodingFailure
734{-# INLINE decodeUtf8Arrays_ #-}
736 (MonadIO m, IsStream t) => t m (Array Word8) -> t m Char
738 D.fromStreamD . decodeUtf8ArraysD_ . D.toStreamD
740-------------------------------------------------------------------------------
741-- Encoding Unicode (UTF-8) Characters
742-------------------------------------------------------------------------------
744data WList = WCons !Word8 !WList | WNil
746-- UTF-8 primitives, Lifted from GHC.IO.Encoding.UTF8.
750ord2 c = assert (n >= 0x80 && n <= 0x07ff) (WCons x1 (WCons x2 WNil))
753 x1 = fromIntegral $ (n `shiftR` 6) + 0xC0
754 x2 = fromIntegral $ (n .&. 0x3F) + 0x80
758ord3 c = assert (n >= 0x0800 && n <= 0xffff) (WCons x1 (WCons x2 (WCons x3 WNil)))
761 x1 = fromIntegral $ (n `shiftR` 12) + 0xE0
762 x2 = fromIntegral $ ((n `shiftR` 6) .&. 0x3F) + 0x80
763 x3 = fromIntegral $ (n .&. 0x3F) + 0x80
767ord4 c = assert (n >= 0x10000) (WCons x1 (WCons x2 (WCons x3 (WCons x4 WNil))))
770 x1 = fromIntegral $ (n `shiftR` 18) + 0xF0
771 x2 = fromIntegral $ ((n `shiftR` 12) .&. 0x3F) + 0x80
772 x3 = fromIntegral $ ((n `shiftR` 6) .&. 0x3F) + 0x80
773 x4 = fromIntegral $ (n .&. 0x3F) + 0x80
775{-# ANN type EncodeState Fuse #-}
776data EncodeState s = EncodeState s !WList
778{-# ANN type InvalidAction Fuse #-}
780 DropInvalid | ErrorInvalid | IgnoreInvalid | ReplaceInvalid
782replaceInvalid :: s -> Step (EncodeState s) a
784 Skip $ EncodeState s (WCons 239 (WCons 191 (WCons 189 WNil)))
786dropInvalid :: s -> Step (EncodeState s) a
787dropInvalid s = Skip (EncodeState s WNil)
789errorOnInvalid :: s -> Step (EncodeState s) a
792 show "Streamly.Internal.Data.Stream.StreamD.encodeUtf8:"
793 ++ "Encountered a surrogate"
795{-# INLINE_NORMAL encodeUtf8DGeneric #-}
801encodeUtf8DGeneric act (Stream step state) =
802 Stream step' (EncodeState state WNil)
806 {-# INLINE_LATE step' #-}
807 step' gst (EncodeState st WNil) = do
808 r <- step (adaptState gst) st
814 Yield (fromIntegral x) (EncodeState s WNil)
815 | x <= 0x7FF -> Skip (EncodeState s (ord2 c))
821 else Skip (EncodeState s (ord3 c))
825 then errorOnInvalid s
826 else Skip (EncodeState s (ord3 c))
829 Skip (EncodeState s (ord3 c))
833 then replaceInvalid s
834 else Skip (EncodeState s (ord3 c))
836 | otherwise -> Skip (EncodeState s (ord4 c))
837 Skip s -> Skip (EncodeState s WNil)
839 step' _ (EncodeState s (WCons x xs)) = return $ Yield x (EncodeState s xs)
841-- More yield points improve performance, but I am not sure if they can cause
842-- too much code bloat or some trouble with fusion. So keeping only two yield
843-- points for now, one for the ascii chars (fast path) and one for all other
845{-# INLINE_NORMAL encodeUtf8D' #-}
846encodeUtf8D' :: Monad m => Stream m Char -> Stream m Word8
847encodeUtf8D' = encodeUtf8DGeneric ErrorInvalid
849-- | Encode a stream of Unicode characters to a UTF-8 encoded bytestream. When
850-- any invalid character (U+D800-U+D8FF) is encountered in the input stream the
851-- function errors out.
854{-# INLINE encodeUtf8' #-}
855encodeUtf8' :: (Monad m, IsStream t) => t m Char -> t m Word8
856encodeUtf8' = D.fromStreamD . encodeUtf8D' . D.toStreamD
858-- | See section "3.9 Unicode Encoding Forms" in
859-- https://www.unicode.org/versions/Unicode13.0.0/UnicodeStandard-13.0.pdf
861{-# INLINE_NORMAL encodeUtf8D #-}
862encodeUtf8D :: Monad m => Stream m Char -> Stream m Word8
863encodeUtf8D = encodeUtf8DGeneric ReplaceInvalid
865-- | Encode a stream of Unicode characters to a UTF-8 encoded bytestream. Any
866-- Invalid characters (U+D800-U+D8FF) in the input stream are replaced by the
867-- Unicode replacement character U+FFFD.
869-- /Since: 0.7.0 ("Streamly.Data.Unicode.Stream")/
871-- /Since: 0.8.0 (Lenient Behaviour)/
872{-# INLINE encodeUtf8 #-}
873encodeUtf8 :: (Monad m, IsStream t) => t m Char -> t m Word8
874encodeUtf8 = D.fromStreamD . encodeUtf8D . D.toStreamD
876{-# INLINE_NORMAL encodeUtf8D_ #-}
877encodeUtf8D_ :: Monad m => Stream m Char -> Stream m Word8
878encodeUtf8D_ = encodeUtf8DGeneric DropInvalid
880-- | Encode a stream of Unicode characters to a UTF-8 encoded bytestream. Any
881-- Invalid characters (U+D800-U+D8FF) in the input stream are dropped.
884{-# INLINE encodeUtf8_ #-}
885encodeUtf8_ :: (Monad m, IsStream t) => t m Char -> t m Word8
886encodeUtf8_ = D.fromStreamD . encodeUtf8D_ . D.toStreamD
888-- | Same as 'encodeUtf8'
890{-# DEPRECATED encodeUtf8Lax "Please use 'encodeUtf8' instead" #-}
891{-# INLINE encodeUtf8Lax #-}
892encodeUtf8Lax :: (IsStream t, Monad m) => t m Char -> t m Word8
893encodeUtf8Lax = encodeUtf8
895-------------------------------------------------------------------------------
896-- Encode streams of containers
897-------------------------------------------------------------------------------
899-- | Encode a container to @Array Word8@ provided an unfold to covert it to a
900-- Char stream and an encoding function.
903{-# INLINE encodeObject #-}
904encodeObject :: MonadIO m =>
905 (SerialT m Char -> SerialT m Word8)
909encodeObject encode u = S.fold Array.write . encode . S.unfold u
911-- | Encode a stream of container objects using the supplied encoding scheme.
912-- Each object is encoded as an @Array Word8@.
915{-# INLINE encodeObjects #-}
916encodeObjects :: (MonadIO m, IsStream t) =>
917 (SerialT m Char -> SerialT m Word8)
921encodeObjects encode u = Serial.mapM (encodeObject encode u)
923-- | Encode a stream of 'String' using the supplied encoding scheme. Each
924-- string is encoded as an @Array Word8@.
927{-# INLINE encodeStrings #-}
928encodeStrings :: (MonadIO m, IsStream t) =>
929 (SerialT m Char -> SerialT m Word8) -> t m String -> t m (Array Word8)
930encodeStrings encode = encodeObjects encode Unfold.fromList
933-------------------------------------------------------------------------------
934-- Utility operations on strings
935-------------------------------------------------------------------------------
937strip :: IsStream t => t m Char -> t m Char
940stripTail :: IsStream t => t m Char -> t m Char
944-- | Remove leading whitespace from a string.
946-- > stripHead = S.dropWhile isSpace
949{-# INLINE stripHead #-}
950stripHead :: (Monad m, IsStream t) => t m Char -> t m Char
951stripHead = S.dropWhile isSpace
953-- | Fold each line of the stream using the supplied 'Fold'
954-- and stream the result.
956-- >>> Stream.toList $ lines Fold.toList (Stream.fromList "lines\nthis\nstring\n\n\n")
957-- ["lines","this","string","",""]
959-- > lines = S.splitOnSuffix (== '\n')
963lines :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
964lines = S.splitOnSuffix (== '\n')
966foreign import ccall unsafe "u_iswspace"
967 iswspace :: Int -> Int
969-- | Code copied from base/Data.Char to INLINE it
970{-# INLINE isSpace #-}
971isSpace :: Char -> Bool
973 | uc <= 0x377 = uc == 32 || uc - 0x9 <= 4 || uc == 0xa0
974 | otherwise = iswspace (ord c) /= 0
976 uc = fromIntegral (ord c) :: Word
978-- | Fold each word of the stream using the supplied 'Fold'
979-- and stream the result.
981-- >>> Stream.toList $ words Fold.toList (Stream.fromList "fold these words")
982-- ["fold","these","words"]
984-- > words = S.wordsBy isSpace
988words :: (Monad m, IsStream t) => Fold m Char b -> t m Char -> t m b
989words = S.wordsBy isSpace
991-- | Unfold a stream to character streams using the supplied 'Unfold'
992-- and concat the results suffixing a newline character @\\n@ to each stream.
995-- unlines = Stream.interposeSuffix '\n'
996-- unlines = Stream.intercalateSuffix Unfold.fromList "\n"
1000{-# INLINE unlines #-}
1001unlines :: (MonadIO m, IsStream t) => Unfold m a Char -> t m a -> t m Char
1002unlines = S.interposeSuffix '\n'
1004-- | Unfold the elements of a stream to character streams using the supplied
1005-- 'Unfold' and concat the results with a whitespace character infixed between
1009-- unwords = Stream.interpose ' '
1010-- unwords = Stream.intercalate Unfold.fromList " "
1014{-# INLINE unwords #-}
1015unwords :: (MonadIO m, IsStream t) => Unfold m a Char -> t m a -> t m Char
1016unwords = S.interpose ' '
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list