Mojo struct
CodepointSliceIter
struct CodepointSliceIter[mut: Bool, //, origin: Origin[mut], forward: Bool = True]
Iterator for StringSlice
over substring slices containing a single Unicode codepoint.
The forward
parameter only controls the behavior of the __next__()
method used for normal iteration. Calls to next()
will always take an
element from the front of the iterator, and calls to next_back()
will
always take an element from the end.
Parameters
- mut (
Bool
): Whether the slice is mutable. - origin (
Origin[mut]
): The origin of the underlying string data. - forward (
Bool
): The iteration direction.False
is backwards.
Implemented traits
AnyType
,
Copyable
,
ExplicitlyCopyable
,
Movable
,
UnknownDestructibility
Methods
__next__
__next__(mut self) -> StringSlice[origin]
Get the next codepoint in the underlying string slice.
This returns the next single-codepoint substring slice encoded in the underlying string, and advances the iterator state.
If forward
is set to False
, this will return the next codepoint
from the end of the string.
This function will abort if this iterator has been exhausted.
Returns:
The next character in the string.
__has_next__
__has_next__(self) -> Bool
Returns True if there are still elements in this iterator.
Returns:
A boolean indicating if there are still elements in this iterator.
__len__
__len__(self) -> Int
Returns the remaining length of this iterator in Codepoint
s.
The value returned from this method indicates the number of subsequent
calls to next()
that will return a value.
Returns:
Number of codepoints remaining in this iterator.
peek_next
peek_next(self) -> Optional[StringSlice[origin]]
Check what the next single-codepoint slice in this iterator is, without advancing the iterator state.
Repeated calls to this method will return the same value.
Examples
peek_next()
does not advance the iterator, so repeated calls will
return the same value:
from collections.string import StringSlice, Codepoint
from testing import assert_equal
var input = StringSlice("123")
var iter = input.codepoint_slices()
assert_equal(iter.peek_next().value(), "1")
assert_equal(iter.peek_next().value(), "1")
assert_equal(iter.peek_next().value(), "1")
# A call to `next()` return the same value as `peek_next()` had,
# but also advance the iterator.
assert_equal(iter.next().value(), "1")
# Later `peek_next()` calls will return the _new_ next character:
assert_equal(iter.peek_next().value(), "2")
from collections.string import StringSlice, Codepoint
from testing import assert_equal
var input = StringSlice("123")
var iter = input.codepoint_slices()
assert_equal(iter.peek_next().value(), "1")
assert_equal(iter.peek_next().value(), "1")
assert_equal(iter.peek_next().value(), "1")
# A call to `next()` return the same value as `peek_next()` had,
# but also advance the iterator.
assert_equal(iter.next().value(), "1")
# Later `peek_next()` calls will return the _new_ next character:
assert_equal(iter.peek_next().value(), "2")
.
Returns:
The next codepoint slice in the underlying string, or None if the string is empty.
peek_back
peek_back(mut self) -> Optional[StringSlice[origin]]
Check what the last single-codepoint slice in this iterator is, without advancing the iterator state.
Repeated calls to this method will return the same value.
Examples
peek_back()
does not advance the iterator, so repeated calls will
return the same value:
from collections.string import StringSlice, Codepoint
from testing import assert_equal
var input = StringSlice("123")
var iter = input.codepoint_slices()
# Repeated calls to `peek_back()` return the same value.
assert_equal(iter.peek_back().value(), "3")
assert_equal(iter.peek_back().value(), "3")
assert_equal(iter.peek_back().value(), "3")
# A call to `next_back()` return the same value as `peek_back()` had,
# but also advance the iterator.
assert_equal(iter.next_back().value(), "3")
# Later `peek_back()` calls will return the _new_ next character:
assert_equal(iter.peek_back().value(), "2")
from collections.string import StringSlice, Codepoint
from testing import assert_equal
var input = StringSlice("123")
var iter = input.codepoint_slices()
# Repeated calls to `peek_back()` return the same value.
assert_equal(iter.peek_back().value(), "3")
assert_equal(iter.peek_back().value(), "3")
assert_equal(iter.peek_back().value(), "3")
# A call to `next_back()` return the same value as `peek_back()` had,
# but also advance the iterator.
assert_equal(iter.next_back().value(), "3")
# Later `peek_back()` calls will return the _new_ next character:
assert_equal(iter.peek_back().value(), "2")
.
Returns:
The last codepoint slice in the underlying string, or None if the string is empty.
next
next(mut self) -> Optional[StringSlice[origin]]
Get the next codepoint slice in the underlying string slice, or None if the iterator is empty.
This returns the next single-codepoint substring encoded in the underlying string, and advances the iterator state.
Returns:
A character if the string is not empty, otherwise None.
next_back
next_back(mut self) -> Optional[StringSlice[origin]]
Get the last single-codepoint slice in this iterator is, or None if the iterator is empty.
This returns the last codepoint slice in this iterator, and advances the iterator state.
Returns:
The last codepoint slice in the underlying string, or None if the string is empty.
Was this page helpful?
Thank you! We'll create more content like this.
Thank you for helping us improve!