[−][src]Module ropey::iter
Iterators over a Rope
's data.
The iterators in Ropey can be created from both Rope
s and RopeSlice
s.
When created from a RopeSlice
, they iterate over only the data that the
RopeSlice
refers to. For the Lines
and Chunks
iterators, the data
of the first and last yielded item will be correctly truncated to match
the bounds of the RopeSlice
.
Reverse iteration
All iterators in Ropey operate as a cursor that can move both forwards
and backwards over its contents. Doing this is accomplished via the
next()
and prev()
methods of each iterator.
Conceptually, an iterator in Ropey is always positioned between the
elements it iterates over, and returns an element when it jumps over it
via the next()
or prev()
methods.
For example, given the text "abc"
and a Chars
iterator starting at the
beginning of the text, you would get the following sequence of states and
return values by repeatedly calling next()
(the vertical bar represents
the position of the iterator):
|abc
a|bc
->Some('a')
ab|c
->Some('b')
abc|
->Some('c')
abc|
->None
The prev()
method operates identically, except moving in the opposite
direction.
Creating iterators at any position
Iterators in Ropey can be created starting at any position in the text.
This is accomplished with the various bytes_at()
, chars_at()
, etc.
methods of Rope
and RopeSlice
.
When an iterator is created this way, it is positioned such that a call to
next()
will return the specified element, and a call to prev()
will
return the element just before the specified one.
Importantly, iterators created this way still have access to the entire
contents of the Rope
/RopeSlice
they were created from—the
contents before the specified position is not truncated. For example, you
can create a Chars
iterator starting at the end of a Rope
, and then
use the prev()
method to iterate backwards over all of that Rope
's
chars.
Structs
Bytes | An iterator over a |
Chars | An iterator over a |
Chunks | An iterator over a |
Lines | An iterator over a |