-- Hoogle documentation, generated by Haddock
-- See Hoogle, http://www.haskell.org/hoogle/


-- | A rope data structure used by Yi
--   
--   A rope data structure used by Yi
@package yi-rope
@version 0.11


-- | This module defines a <tt>rope</tt> data structure for use in Yi. This
--   specific implementation uses a fingertree over Text.
--   
--   In contrast to our old implementation, we can now reap all the
--   benefits of Text: automatic unicode handling and blazing fast
--   implementation on underlying strings. This frees us from a lot of
--   book-keeping. We don't lose out on not using ByteString directly
--   because the old implementation encoded it into UTF8 anyway, making it
--   unsuitable for storing anything but text.
module Yi.Rope

-- | A <a>YiString</a> is a <a>FingerTree</a> with cached char and line
--   counts over chunks of <a>Text</a>.
data YiString

-- | See <a>fromText</a>.
fromString :: String -> YiString

-- | Converts a <a>Text</a> into a <a>YiString</a> using
--   <a>defaultChunkSize</a>-sized chunks for the underlying tree.
fromText :: Text -> YiString

-- | See <a>fromText'</a>.
fromString' :: Int -> String -> YiString

-- | This is like <a>fromText</a> but it allows the user to specify the
--   chunk size to be used. Uses <a>defaultChunkSize</a> if the given size
--   is &lt;= 0.
fromText' :: Int -> Text -> YiString

-- | See <a>toText</a>.
toString :: YiString -> String

-- | See <a>toReverseText</a>.
--   
--   Note that it is actually ~4.5 times faster to use <a>toReverseText</a>
--   and unpack the result than to convert to <a>String</a> and use
--   <a>reverse</a>.
toReverseString :: YiString -> String

-- | Consider whether you really need to use this!
toText :: YiString -> Text

-- | Spits out the underlying string, reversed.
--   
--   Note that this is actually slightly faster than manually unrolling the
--   tree from the end, <a>reverse</a>ing each chunk and <a>concat</a>ing,
--   at least with -O2 which you really need to be using with <a>Text</a>
--   anyway.
toReverseText :: YiString -> Text

-- | Checks if the given <a>YiString</a> is actually empty.
null :: YiString -> Bool

-- | Creates an empty <a>YiString</a>.
empty :: YiString

-- | Takes the first n given characters.
take :: Int -> YiString -> YiString

-- | Drops the first n characters.
drop :: Int -> YiString -> YiString

-- | Length of the whole underlying string.
--   
--   Amortized constant time.
length :: YiString -> Int

-- | Reverse the whole underlying string.
--   
--   This involves reversing the order of the chunks as well as content of
--   the chunks. We use a little optimisation here that re-uses the content
--   of each chunk but this exposes a potential problem: after many
--   transformations, our chunks size might become quite varied (but never
--   more than the default size), perhaps we should periodically rechunk
--   the tree to recover nice sizes?
reverse :: YiString -> YiString

-- | Count the number of newlines in the underlying string. This is
--   actually amortized constant time as we cache this information in the
--   underlying tree.
countNewLines :: YiString -> Int

-- | This is like <a>lines'</a> but it does *not* preserve newlines.
--   
--   Specifically, we just strip the newlines from the result of
--   <a>lines'</a>.
--   
--   This behaves slightly differently than the old split: the number of
--   resulting strings here is equal to the number of newline characters in
--   the underlying string. This is much more consistent than the old
--   behaviour which blindly used <tt>ByteString</tt>s split and stitched
--   the result back together which was inconsistent with the rest of the
--   interface which worked with number of newlines.
lines :: YiString -> [YiString]

-- | Splits the <a>YiString</a> into a list of <a>YiString</a> each
--   containing a line.
--   
--   Note that in old implementation this allowed an arbitrary character to
--   split on. If you want to do that, manually convert <a>toText</a> and
--   use <a>splitOn</a> to suit your needs. This case is optimised for
--   newlines only which seems to have been the only use of the original
--   function.
--   
--   The newlines are preserved so this should hold:
--   
--   <pre>
--   'toText' . 'concat' . 'lines'' ≡ 'toText'
--   </pre>
--   
--   but the underlying structure might change: notably, chunks will most
--   likely change sizes.
lines' :: YiString -> [YiString]

-- | Joins up lines by a newline character. It does not leave a newline
--   after the last line. If you want a more classical <a>unlines</a>
--   behaviour, use <a>map</a> along with <a>snoc</a>.
unlines :: [YiString] -> YiString

-- | Splits the string at given character position.
--   
--   If <tt>position &lt;= 0</tt> then the left string is empty and the
--   right string contains everything else.
--   
--   If <tt>position &gt;= length of the string</tt> then the left string
--   contains everything and the right string is empty.
--   
--   Implementation note: the way this works is by splitting the underlying
--   finger at a closest chunk that goes *over* the given position (see
--   <a>split</a>). This either results in a perfect split at which point
--   we're done or more commonly, it leaves as few characters short and we
--   need to take few characters from the first chunk of the right side of
--   the split. We do precisely that.
--   
--   All together, this split is only as expensive as underlying
--   <a>split</a>, the cost of splitting a chunk into two, the cost of one
--   cons and one cons of a chunk and lastly the cost of <a>splitAt</a> of
--   the underlying <a>Text</a>. It turns out to be fairly fast all
--   together.
splitAt :: Int -> YiString -> (YiString, YiString)

-- | Splits the underlying string before the given line number.
--   Zero-indexed lines.
--   
--   Splitting at line &lt;= 0 gives you an empty string. Splitting at
--   <tt>n &gt; 0</tt> gives you the first n lines.
--   
--   Also see <a>splitAtLine'</a>.
splitAtLine :: Int -> YiString -> (YiString, YiString)

-- | Add a <a>Char</a> in front of a <a>YiString</a>.
cons :: Char -> YiString -> YiString

-- | Add a <a>Char</a> in the back of a <a>YiString</a>.
snoc :: YiString -> Char -> YiString

-- | Single character <a>YiString</a>. Consider whether it's worth creating
--   this, maybe you can use <a>cons</a> or <a>snoc</a> instead?
singleton :: Char -> YiString

-- | Take the first character of the underlying string if possible.
head :: YiString -> Maybe Char

-- | Take the last character of the underlying string if possible.
last :: YiString -> Maybe Char

-- | Append two <a>YiString</a>s.
--   
--   We take the extra time to optimise this append for many small
--   insertions. With naive append of the inner fingertree with
--   <a>&gt;&lt;</a>, it is often the case that we end up with a large
--   collection of tiny chunks. This function instead tries to join the
--   underlying trees at outermost chunks up to <a>defaultChunkSize</a>
--   which while slower, should improve memory usage.
--   
--   I suspect that this pays for itself as we'd spend more time computing
--   over all the little chunks than few large ones anyway.
append :: YiString -> YiString -> YiString

-- | Concat a list of <a>YiString</a>s.
concat :: [YiString] -> YiString

-- | <a>YiString</a> specialised <tt>any</tt>.
--   
--   Implementation note: this currently just does any by doing ‘TX.Text’
--   conversions upon consecutive chunks. We should be able to speed it up
--   by running it in parallel over multiple chunks.
any :: (Char -> Bool) -> YiString -> Bool

-- | <a>YiString</a> specialised <tt>all</tt>.
--   
--   See the implementation note for <a>any</a>.
all :: (Char -> Bool) -> YiString -> Bool

-- | The usual <a>dropWhile</a> optimised for <a>YiString</a>s.
dropWhile :: (Char -> Bool) -> YiString -> YiString

-- | The usual <a>takeWhile</a> optimised for <a>YiString</a>s.
takeWhile :: (Char -> Bool) -> YiString -> YiString

-- | As <a>dropWhile</a> but drops from the end instead.
dropWhileEnd :: (Char -> Bool) -> YiString -> YiString

-- | Like <a>takeWhile</a> but takes from the end instead.
takeWhileEnd :: (Char -> Bool) -> YiString -> YiString

-- | Concatenates the list of <a>YiString</a>s after inserting the
--   user-provided <a>YiString</a> between the elements.
--   
--   Empty <a>YiString</a>s are not ignored and will end up as strings of
--   length 1. If you don't want this, it's up to you to pre-process the
--   list. Just as with <a>intersperse</a>, it is up to the user to
--   pre-process the list.
intercalate :: YiString -> [YiString] -> YiString

-- | Intersperses the given character between the <a>YiString</a>s. This is
--   useful when you have a bunch of strings you just want to separate
--   something with, comma or a dash. Note that it only inserts the
--   character between the elements.
--   
--   What's more, the result is a single <a>YiString</a>. You can easily
--   achieve a version that blindly inserts elements to the back by mapping
--   over the list instead of using this function.
--   
--   You can think of it as a specialised version of <a>intercalate</a>.
--   Note that what this does <b>not</b> do is intersperse characters into
--   the underlying text, you should convert and use <a>intersperse</a> for
--   that instead.
intersperse :: Char -> [YiString] -> YiString

-- | Filters the characters from the underlying string.
--   
--   <pre>
--   &gt;&gt;&gt; filter (/= 'a') "bac"
--   "bc"
--   </pre>
filter :: (Char -> Bool) -> YiString -> YiString

-- | Maps the characters over the underlying string.
map :: (Char -> Char) -> YiString -> YiString

-- | Splits the given <a>YiString</a> into a list of words, where spaces
--   are determined by <a>isSpace</a>. No empty strings are in the result
--   list.
words :: YiString -> [YiString]

-- | Join given <a>YiString</a>s with a space. Empty lines will be filtered
--   out first.
unwords :: [YiString] -> YiString

-- | Splits the <a>YiString</a> on characters matching the predicate, like
--   <a>split</a>.
--   
--   For splitting on newlines use <a>lines</a> or <a>lines'</a> instead.
--   
--   Implementation note: GHC actually makes this naive implementation
--   about as fast and in cases with lots of splits, faster, as a
--   hand-rolled version on chunks with appends which is quite amazing in
--   itself.
split :: (Char -> Bool) -> YiString -> [YiString]

-- | Takes every character but the last one: returns Nothing on empty
--   string.
init :: YiString -> Maybe YiString

-- | Takes the tail of the underlying string. If the string is empty to
--   begin with, returns Nothing.
tail :: YiString -> Maybe YiString

-- | Returns a pair whose first element is the longest prefix (possibly
--   empty) of t of elements that satisfy p, and whose second is the
--   remainder of the string. See also <a>span</a>.
--   
--   This implementation uses <a>splitAt</a> which actually is just as fast
--   as hand-unrolling the tree. GHC sure is great!
span :: (Char -> Bool) -> YiString -> (YiString, YiString)

-- | Just like <a>span</a> but with the predicate negated.
break :: (Char -> Bool) -> YiString -> (YiString, YiString)

-- | Left fold.
--   
--   Benchmarks show that folding is actually Pretty Damn Slow™: consider
--   whether folding is really the best thing to use in your scenario.
foldl' :: (a -> Char -> a) -> a -> YiString -> a

-- | Replicate the given YiString set number of times, concatenating the
--   results. Also see <a>replicateChar</a>.
replicate :: Int -> YiString -> YiString

-- | Replicate the given character set number of times and pack the result
--   into a <a>YiString</a>.
--   
--   <pre>
--   &gt;&gt;&gt; replicateChar 4 ' '
--   "    "
--   </pre>
replicateChar :: Int -> Char -> YiString

-- | Reads file into the rope, also returning the <tt>ConverterName</tt>
--   that was used for decoding. You should resupply this to
--   <a>writeFile</a> if you're aiming to preserve the original encoding.
--   
--   If we fail to guess the encoding used, error message is given instead.
--   
--   It is up to the user to handle exceptions not directly related to
--   character decoding.
readFile :: FilePath -> IO (Either Text YiString)

-- | Write a <a>YiString</a> into the given file.
--   
--   It's up to the user to handle exceptions.
writeFile :: FilePath -> YiString -> IO ()
fromRope :: YiString -> FingerTree Size YiChunk

-- | Helper function doing conversions of to and from underlying
--   <a>Text</a>. You should aim to implement everything in terms of
--   <a>YiString</a> instead.
--   
--   Please note that this maps over each <b>chunk</b> so this can only be
--   used with layout-agnostic functions. For example
--   
--   <pre>
--   &gt;&gt;&gt; let t = 'fromString' "abc" &lt;&gt; 'fromString' "def"
--   
--   &gt;&gt;&gt; 'toString' $ 'withText' 'TX.reverse' t
--   "cbafed"
--   </pre>
--   
--   Probably doesn't do what you wanted, but <a>toUpper</a> would.
--   Specifically, for any <tt>f : <a>Text</a> → <a>Text</a></tt>,
--   <a>withText</a> will only do the ‘expected’ thing iff
--   
--   <pre>
--   f x &lt;&gt; f y ≡ f (x &lt;&gt; y)
--   </pre>
--   
--   which should look very familiar.
withText :: (Text -> Text) -> YiString -> YiString

-- | Maps over each <b>chunk</b> which means this function is UNSAFE! If
--   you use this with functions which don't preserve <a>Size</a>, that is
--   the chunk length and number of newlines, things will break really,
--   really badly. You should not need to use this.
--   
--   Also see <a>unsafeFmap</a>
unsafeWithText :: (Text -> Text) -> YiString -> YiString
instance Data.Binary.Class.Binary Yi.Rope.YiString
instance GHC.Classes.Eq Yi.Rope.Size
instance GHC.Classes.Eq Yi.Rope.YiChunk
instance GHC.Classes.Eq Yi.Rope.YiString
instance GHC.Internal.Data.String.IsString Yi.Rope.YiString
instance Data.FingerTree.Measured Yi.Rope.Size Yi.Rope.YiChunk
instance GHC.Internal.Base.Monoid Yi.Rope.Size
instance GHC.Internal.Base.Monoid Yi.Rope.YiString
instance Control.DeepSeq.NFData Yi.Rope.Size
instance Control.DeepSeq.NFData Yi.Rope.YiChunk
instance Control.DeepSeq.NFData Yi.Rope.YiString
instance GHC.Classes.Ord Yi.Rope.YiString
instance GHC.Internal.Base.Semigroup Yi.Rope.Size
instance GHC.Internal.Base.Semigroup Yi.Rope.YiString
instance GHC.Internal.Show.Show Yi.Rope.Size
instance GHC.Internal.Show.Show Yi.Rope.YiChunk
instance GHC.Internal.Show.Show Yi.Rope.YiString
