Skip to content

Commit 58aa2ef

Browse files
author
blake2-ppc
committed
---
yaml --- r: 75704 b: refs/heads/master c: db3eb72 h: refs/heads/master v: v3
1 parent b0da52c commit 58aa2ef

File tree

2 files changed

+69
-36
lines changed

2 files changed

+69
-36
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: 8a5889d2a2eb4b2c9d41f6f3991fdd2622933047
2+
refs/heads/master: db3eb7291a3af1b88052f8ad87da79d62bd60b81
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 67c954e365970e4c2cd06f0c50724656d7010f45
55
refs/heads/try: 10089455287dcc3652b984ab4bfd6971e1b5f302

trunk/src/libstd/str.rs

Lines changed: 68 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -255,56 +255,94 @@ impl<'self, C: CharEq> CharEq for &'self [C] {
255255
Section: Iterators
256256
*/
257257

258-
/// External iterator for a string's characters and their byte offsets.
259-
/// Use with the `std::iterator` module.
258+
/// External iterator for a string's characters.
260259
#[deriving(Clone)]
261-
pub struct CharOffsetIterator<'self> {
262-
priv index_front: uint,
263-
priv index_back: uint,
260+
pub struct CharIterator<'self> {
264261
priv string: &'self str,
265262
}
266263

267-
impl<'self> Iterator<(uint, char)> for CharOffsetIterator<'self> {
264+
impl<'self> Iterator<char> for CharIterator<'self> {
268265
#[inline]
269-
fn next(&mut self) -> Option<(uint, char)> {
270-
if self.index_front < self.index_back {
271-
let CharRange {ch, next} = self.string.char_range_at(self.index_front);
272-
let index = self.index_front;
273-
self.index_front = next;
274-
Some((index, ch))
266+
fn next(&mut self) -> Option<char> {
267+
if self.string.len() != 0 {
268+
let CharRange {ch, next} = self.string.char_range_at(0);
269+
unsafe {
270+
self.string = raw::slice_unchecked(self.string, next, self.string.len());
271+
}
272+
Some(ch)
275273
} else {
276274
None
277275
}
278276
}
277+
278+
#[inline]
279+
fn size_hint(&self) -> (uint, Option<uint>) {
280+
(self.string.len().saturating_add(3)/4, Some(self.string.len()))
281+
}
279282
}
280283

281-
impl<'self> DoubleEndedIterator<(uint, char)> for CharOffsetIterator<'self> {
284+
impl<'self> DoubleEndedIterator<char> for CharIterator<'self> {
282285
#[inline]
283-
fn next_back(&mut self) -> Option<(uint, char)> {
284-
if self.index_front < self.index_back {
285-
let CharRange {ch, next} = self.string.char_range_at_reverse(self.index_back);
286-
self.index_back = next;
287-
Some((next, ch))
286+
fn next_back(&mut self) -> Option<char> {
287+
if self.string.len() != 0 {
288+
let CharRange {ch, next} = self.string.char_range_at_reverse(self.string.len());
289+
unsafe {
290+
self.string = raw::slice_unchecked(self.string, 0, next);
291+
}
292+
Some(ch)
288293
} else {
289294
None
290295
}
291296
}
292297
}
293298

294-
/// External iterator for a string's characters and their byte offsets in reverse order.
295-
/// Use with the `std::iterator` module.
296-
pub type CharOffsetRevIterator<'self> =
297-
Invert<CharOffsetIterator<'self>>;
298299

299-
/// External iterator for a string's characters.
300+
/// External iterator for a string's characters and their byte offsets.
300301
/// Use with the `std::iterator` module.
301-
pub type CharIterator<'self> =
302-
Map<'self, (uint, char), char, CharOffsetIterator<'self>>;
302+
#[deriving(Clone)]
303+
pub struct CharOffsetIterator<'self> {
304+
priv string: &'self str,
305+
priv iter: CharIterator<'self>,
306+
}
307+
308+
impl<'self> Iterator<(uint, char)> for CharOffsetIterator<'self> {
309+
#[inline]
310+
fn next(&mut self) -> Option<(uint, char)> {
311+
let offset = do self.string.as_imm_buf |a, _| {
312+
do self.iter.string.as_imm_buf |b, _| {
313+
b as uint - a as uint
314+
}
315+
};
316+
self.iter.next().map_move(|ch| (offset, ch))
317+
}
318+
319+
#[inline]
320+
fn size_hint(&self) -> (uint, Option<uint>) {
321+
self.iter.size_hint()
322+
}
323+
}
324+
325+
impl<'self> DoubleEndedIterator<(uint, char)> for CharOffsetIterator<'self> {
326+
#[inline]
327+
fn next_back(&mut self) -> Option<(uint, char)> {
328+
self.iter.next_back().map_move(|ch| {
329+
let offset = do self.string.as_imm_buf |a, _| {
330+
do self.iter.string.as_imm_buf |b, len| {
331+
b as uint - a as uint + len
332+
}
333+
};
334+
(offset, ch)
335+
})
336+
}
337+
}
303338

304339
/// External iterator for a string's characters in reverse order.
305340
/// Use with the `std::iterator` module.
306-
pub type CharRevIterator<'self> =
307-
Invert<Map<'self, (uint, char), char, CharOffsetIterator<'self>>>;
341+
pub type CharRevIterator<'self> = Invert<CharIterator<'self>>;
342+
343+
/// External iterator for a string's characters and their byte offsets in reverse order.
344+
/// Use with the `std::iterator` module.
345+
pub type CharOffsetRevIterator<'self> = Invert<CharOffsetIterator<'self>>;
308346

309347
/// External iterator for a string's bytes.
310348
/// Use with the `std::iterator` module.
@@ -313,8 +351,7 @@ pub type ByteIterator<'self> =
313351

314352
/// External iterator for a string's bytes in reverse order.
315353
/// Use with the `std::iterator` module.
316-
pub type ByteRevIterator<'self> =
317-
Invert<Map<'self, &'self u8, u8, vec::VecIterator<'self, u8>>>;
354+
pub type ByteRevIterator<'self> = Invert<ByteIterator<'self>>;
318355

319356
/// An iterator over the substrings of a string, separated by `sep`.
320357
#[deriving(Clone)]
@@ -1218,7 +1255,7 @@ impl<'self> StrSlice<'self> for &'self str {
12181255
/// ~~~
12191256
#[inline]
12201257
fn iter(&self) -> CharIterator<'self> {
1221-
self.char_offset_iter().map(|(_, c)| c)
1258+
CharIterator{string: *self}
12221259
}
12231260
12241261
/// An iterator over the characters of `self`, in reverse order.
@@ -1242,11 +1279,7 @@ impl<'self> StrSlice<'self> for &'self str {
12421279
/// An iterator over the characters of `self` and their byte offsets.
12431280
#[inline]
12441281
fn char_offset_iter(&self) -> CharOffsetIterator<'self> {
1245-
CharOffsetIterator {
1246-
index_front: 0,
1247-
index_back: self.len(),
1248-
string: *self
1249-
}
1282+
CharOffsetIterator{string: *self, iter: self.iter()}
12501283
}
12511284
12521285
/// An iterator over the characters of `self` and their byte offsets.

0 commit comments

Comments
 (0)