Skip to content

Commit 30ab96b

Browse files
author
blake2-ppc
committed
std::str: Improve comments for CharIterator
1 parent 5eff3e1 commit 30ab96b

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/libstd/str.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,14 +255,18 @@ Section: Iterators
255255
*/
256256

257257
/// External iterator for a string's characters.
258+
/// Use with the `std::iterator` module.
258259
#[deriving(Clone)]
259260
pub struct CharIterator<'self> {
261+
/// The slice remaining to be iterated
260262
priv string: &'self str,
261263
}
262264

263265
impl<'self> Iterator<char> for CharIterator<'self> {
264266
#[inline]
265267
fn next(&mut self) -> Option<char> {
268+
// Decode the next codepoint, then update
269+
// the slice to be just the remaining part
266270
if self.string.len() != 0 {
267271
let CharRange {ch, next} = self.string.char_range_at(0);
268272
unsafe {
@@ -300,13 +304,16 @@ impl<'self> DoubleEndedIterator<char> for CharIterator<'self> {
300304
/// Use with the `std::iterator` module.
301305
#[deriving(Clone)]
302306
pub struct CharOffsetIterator<'self> {
307+
/// The original string to be iterated
303308
priv string: &'self str,
304309
priv iter: CharIterator<'self>,
305310
}
306311

307312
impl<'self> Iterator<(uint, char)> for CharOffsetIterator<'self> {
308313
#[inline]
309314
fn next(&mut self) -> Option<(uint, char)> {
315+
// Compute the byte offset by using the pointer offset between
316+
// the original string slice and the iterator's remaining part
310317
let offset = do self.string.as_imm_buf |a, _| {
311318
do self.iter.string.as_imm_buf |b, _| {
312319
b as uint - a as uint
@@ -1281,7 +1288,8 @@ impl<'self> StrSlice<'self> for &'self str {
12811288
CharOffsetIterator{string: *self, iter: self.iter()}
12821289
}
12831290
1284-
/// An iterator over the characters of `self` and their byte offsets.
1291+
/// An iterator over the characters of `self` and their byte offsets,
1292+
/// in reverse order.
12851293
#[inline]
12861294
fn char_offset_rev_iter(&self) -> CharOffsetRevIterator<'self> {
12871295
self.char_offset_iter().invert()

0 commit comments

Comments
 (0)