diff options
| author | Huon Wilson <dbau.pp+github@gmail.com> | 2013-03-25 01:17:40 +1100 |
|---|---|---|
| committer | Huon Wilson <dbau.pp+github@gmail.com> | 2013-03-25 01:17:40 +1100 |
| commit | 29e8b6ea9b63c5cc1cd91cc5eb756820f7fe50b7 (patch) | |
| tree | 4aa90491f08117fb078a76bd02e8ad3d52d1c3f0 /src | |
| parent | 12df65470fb8c5d57fb4f94d37557daa6282173f (diff) | |
| download | rust-29e8b6ea9b63c5cc1cd91cc5eb756820f7fe50b7.tar.gz rust-29e8b6ea9b63c5cc1cd91cc5eb756820f7fe50b7.zip | |
libcore: implement `str::each_char_reverse` correctly.
Previously, `each_char_reverse` was not iterating at all, so the testcase never ran properly.
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/str.rs | 19 |
1 files changed, 9 insertions, 10 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs index f26d9ee3492..92358c6a5e9 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -1020,22 +1020,21 @@ pub fn each_chari(s: &str, it: &fn(uint, char) -> bool) { /// Iterates over the chars in a string in reverse #[inline(always)] pub fn each_char_reverse(s: &str, it: &fn(char) -> bool) { - let mut pos = 0; - let len = s.char_len(); - while pos > 0 { - let CharRange {ch, next} = char_range_at_reverse(s, pos); - pos = next; - if !it(ch) { break; } - } + each_chari_reverse(s, |_, c| it(c)) } // Iterates over the chars in a string in reverse, with indices #[inline(always)] pub fn each_chari_reverse(s: &str, it: &fn(uint, char) -> bool) { + let mut pos = s.len(); let mut ch_pos = s.char_len(); - for s.each_char_reverse |ch| { + while pos > 0 { + let CharRange {ch, next} = char_range_at_reverse(s, pos); + pos = next; ch_pos -= 1; + if !it(ch_pos, ch) { break; } + } } @@ -3661,10 +3660,10 @@ mod tests { fn test_each_char_reverse() { let s = ~"ศไทย中华Việt Nam"; let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m']; - let mut pos = 0; + let mut pos = v.len(); for s.each_char_reverse |ch| { + pos -= 1; fail_unless!(ch == v[pos]); - pos += 1; } } |
