Skip to content

Commit

Permalink
Fix underflow in char_range_at_reverse
Browse files Browse the repository at this point in the history
Added char_range_at_reverse underflow test
  • Loading branch information
Kimundi committed Mar 31, 2013
1 parent 75d615d commit df66e8d
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/libcore/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1865,8 +1865,10 @@ pub struct CharRange {
* Given a byte position and a str, return the previous char and its position
*
* This function can be used to iterate over a unicode string in reverse.
*
* returns 0 for next index if called on start index 0
*/
fn char_range_at_reverse(ss: &str, start: uint) -> CharRange {
pub fn char_range_at_reverse(ss: &str, start: uint) -> CharRange {
let mut prev = start;

// while there is a previous byte == 10......
Expand All @@ -1875,7 +1877,12 @@ fn char_range_at_reverse(ss: &str, start: uint) -> CharRange {
}

// now refer to the initial byte of previous char
prev -= 1u;
if prev > 0u {
prev -= 1u;
} else {
prev = 0u;
}


let ch = char_at(ss, prev);
return CharRange {ch:ch, next:prev};
Expand Down Expand Up @@ -3761,4 +3768,10 @@ mod tests {
"12345555".cmp(& &"123456") == Less;
"22".cmp(& &"1234") == Greater;
}

#[test]
fn test_char_range_at_reverse_underflow() {
assert!(char_range_at_reverse("abc", 0).next == 0);
}

}

5 comments on commit df66e8d

@bors
Copy link
Contributor

@bors bors commented on df66e8d Mar 31, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from catamorphism
at Kimundi@df66e8d

@bors
Copy link
Contributor

@bors bors commented on df66e8d Mar 31, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging Kimundi/rust/incoming = df66e8d into auto

@bors
Copy link
Contributor

@bors bors commented on df66e8d Mar 31, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kimundi/rust/incoming = df66e8d merged ok, testing candidate = fb5f020

@bors
Copy link
Contributor

@bors bors commented on df66e8d Mar 31, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on df66e8d Mar 31, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding incoming to auto = fb5f020

Please sign in to comment.