@@ -255,14 +255,18 @@ Section: Iterators
255
255
*/
256
256
257
257
/// External iterator for a string's characters.
258
+ /// Use with the `std::iterator` module.
258
259
#[ deriving( Clone ) ]
259
260
pub struct CharIterator < ' self > {
261
+ /// The slice remaining to be iterated
260
262
priv string : & ' self str ,
261
263
}
262
264
263
265
impl < ' self > Iterator < char > for CharIterator < ' self > {
264
266
#[ inline]
265
267
fn next ( & mut self ) -> Option < char > {
268
+ // Decode the next codepoint, then update
269
+ // the slice to be just the remaining part
266
270
if self . string . len ( ) != 0 {
267
271
let CharRange { ch, next} = self . string . char_range_at ( 0 ) ;
268
272
unsafe {
@@ -300,13 +304,16 @@ impl<'self> DoubleEndedIterator<char> for CharIterator<'self> {
300
304
/// Use with the `std::iterator` module.
301
305
#[ deriving( Clone ) ]
302
306
pub struct CharOffsetIterator < ' self > {
307
+ /// The original string to be iterated
303
308
priv string : & ' self str ,
304
309
priv iter : CharIterator < ' self > ,
305
310
}
306
311
307
312
impl < ' self > Iterator < ( uint , char ) > for CharOffsetIterator < ' self > {
308
313
#[ inline]
309
314
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
310
317
let offset = do self . string . as_imm_buf |a, _| {
311
318
do self . iter . string . as_imm_buf |b, _| {
312
319
b as uint - a as uint
@@ -1281,7 +1288,8 @@ impl<'self> StrSlice<'self> for &'self str {
1281
1288
CharOffsetIterator{string: *self, iter: self.iter()}
1282
1289
}
1283
1290
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.
1285
1293
#[inline]
1286
1294
fn char_offset_rev_iter(&self) -> CharOffsetRevIterator<'self> {
1287
1295
self.char_offset_iter().invert()
0 commit comments