-- |
-- Module      : Streamly.Internal.Unicode.Array
-- Copyright   : (c) 2018 Composewell Technologies
--
-- License     : BSD-3-Clause
-- Maintainer  : streamly@composewell.com
-- Stability   : experimental
-- Portability : GHC

-- XXX Remove this or move this to Unicode.Utf32 making "Array Char" as a
-- newtype wrapper for Utf32. Is this any better than the [Char] (String) type?
-- This provides random access and the length of the string in O(1). Also,
-- better append performance.
--
module Streamly.Internal.Unicode.Array
    (
    -- * Setup
    -- | To execute the code examples provided in this module in ghci, please
    -- run the following commands first.
    --
    -- $setup

    -- * Streams of Strings
      lines
    , words
    , unlines
    , unwords
    )
where

import Control.Monad.IO.Class (MonadIO)
import Streamly.Data.Stream (Stream)
import Streamly.Internal.Data.Array (Array)

import qualified Streamly.Data.Array as A
import qualified Streamly.Internal.Unicode.Stream as S

import Prelude hiding (String, lines, words, unlines, unwords)

-- $setup
-- >>> :m
-- >>> :set -XOverloadedStrings
-- >>> import Prelude hiding (String, lines, words, unlines, unwords)
-- >>> import qualified Streamly.Data.Stream as Stream
-- >>> import qualified Streamly.Data.Fold as Fold
-- >>> import qualified Streamly.Internal.Unicode.Array as Unicode

-- | Break a string up into a stream of strings at newline characters.
-- The resulting strings do not contain newlines.
--
-- > lines = S.lines A.write
--
-- >>> Stream.fold Fold.toList $ Unicode.lines $ Stream.fromList "lines\nthis\nstring\n\n\n"
-- [fromList "lines",fromList "this",fromList "string",fromList "",fromList ""]
--
{-# INLINE lines #-}
lines :: MonadIO m => Stream m Char -> Stream m (Array Char)
lines :: forall (m :: * -> *).
MonadIO m =>
Stream m Char -> Stream m (Array Char)
lines = Fold m Char (Array Char) -> Stream m Char -> Stream m (Array Char)
forall (m :: * -> *) b.
Monad m =>
Fold m Char b -> Stream m Char -> Stream m b
S.lines Fold m Char (Array Char)
forall (m :: * -> *) a. (MonadIO m, Unbox a) => Fold m a (Array a)
A.write

-- | Break a string up into a stream of strings, which were delimited
-- by characters representing white space.
--
-- > words = S.words A.write
--
-- >>> Stream.fold Fold.toList $ Unicode.words $ Stream.fromList "A  newline\nis considered white space?"
-- [fromList "A",fromList "newline",fromList "is",fromList "considered",fromList "white",fromList "space?"]
--
{-# INLINE words #-}
words :: MonadIO m => Stream m Char -> Stream m (Array Char)
words :: forall (m :: * -> *).
MonadIO m =>
Stream m Char -> Stream m (Array Char)
words = Fold m Char (Array Char) -> Stream m Char -> Stream m (Array Char)
forall (m :: * -> *) b.
Monad m =>
Fold m Char b -> Stream m Char -> Stream m b
S.words Fold m Char (Array Char)
forall (m :: * -> *) a. (MonadIO m, Unbox a) => Fold m a (Array a)
A.write

-- | Flattens the stream of @Array Char@, after appending a terminating
-- newline to each string.
--
-- 'unlines' is an inverse operation to 'lines'.
--
-- >>> Stream.fold Fold.toList $ Unicode.unlines $ Stream.fromList ["lines", "this", "string"]
-- "lines\nthis\nstring\n"
--
-- > unlines = S.unlines A.read
--
-- Note that, in general
--
-- > unlines . lines /= id
{-# INLINE unlines #-}
unlines :: MonadIO m => Stream m (Array Char) -> Stream m Char
unlines :: forall (m :: * -> *).
MonadIO m =>
Stream m (Array Char) -> Stream m Char
unlines = Unfold m (Array Char) Char
-> Stream m (Array Char) -> Stream m Char
forall (m :: * -> *) a.
MonadIO m =>
Unfold m a Char -> Stream m a -> Stream m Char
S.unlines Unfold m (Array Char) Char
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader

-- | Flattens the stream of @Array Char@, after appending a separating
-- space to each string.
--
-- 'unwords' is an inverse operation to 'words'.
--
-- >>> Stream.fold Fold.toList $ Unicode.unwords $ Stream.fromList ["unwords", "this", "string"]
-- "unwords this string"
--
-- > unwords = S.unwords A.read
--
-- Note that, in general
--
-- > unwords . words /= id
{-# INLINE unwords #-}
unwords :: MonadIO m => Stream m (Array Char) -> Stream m Char
unwords :: forall (m :: * -> *).
MonadIO m =>
Stream m (Array Char) -> Stream m Char
unwords = Unfold m (Array Char) Char
-> Stream m (Array Char) -> Stream m Char
forall (m :: * -> *) a.
MonadIO m =>
Unfold m a Char -> Stream m a -> Stream m Char
S.unwords Unfold m (Array Char) Char
forall (m :: * -> *) a. (Monad m, Unbox a) => Unfold m (Array a) a
A.reader