libcore: implement str::each_char_reverse
correctly.
Previously, `each_char_reverse` was not iterating at all, so the testcase never ran properly.
This commit is contained in:
parent
12df65470f
commit
29e8b6ea9b
@ -1020,22 +1020,21 @@ pub fn each_chari(s: &str, it: &fn(uint, char) -> bool) {
|
|||||||
/// Iterates over the chars in a string in reverse
|
/// Iterates over the chars in a string in reverse
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn each_char_reverse(s: &str, it: &fn(char) -> bool) {
|
pub fn each_char_reverse(s: &str, it: &fn(char) -> bool) {
|
||||||
let mut pos = 0;
|
each_chari_reverse(s, |_, c| it(c))
|
||||||
let len = s.char_len();
|
|
||||||
while pos > 0 {
|
|
||||||
let CharRange {ch, next} = char_range_at_reverse(s, pos);
|
|
||||||
pos = next;
|
|
||||||
if !it(ch) { break; }
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Iterates over the chars in a string in reverse, with indices
|
// Iterates over the chars in a string in reverse, with indices
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn each_chari_reverse(s: &str, it: &fn(uint, char) -> bool) {
|
pub fn each_chari_reverse(s: &str, it: &fn(uint, char) -> bool) {
|
||||||
|
let mut pos = s.len();
|
||||||
let mut ch_pos = s.char_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;
|
ch_pos -= 1;
|
||||||
|
|
||||||
if !it(ch_pos, ch) { break; }
|
if !it(ch_pos, ch) { break; }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3661,10 +3660,10 @@ mod tests {
|
|||||||
fn test_each_char_reverse() {
|
fn test_each_char_reverse() {
|
||||||
let s = ~"ศไทย中华Việt Nam";
|
let s = ~"ศไทย中华Việt Nam";
|
||||||
let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
|
let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
|
||||||
let mut pos = 0;
|
let mut pos = v.len();
|
||||||
for s.each_char_reverse |ch| {
|
for s.each_char_reverse |ch| {
|
||||||
|
pos -= 1;
|
||||||
fail_unless!(ch == v[pos]);
|
fail_unless!(ch == v[pos]);
|
||||||
pos += 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user