diff options
| author | bors <bors@rust-lang.org> | 2013-03-31 15:21:42 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-03-31 15:21:42 -0700 |
| commit | fb5f020cc2d1d0972d2726413d1ec053990675ad (patch) | |
| tree | ca785368ed8655c487368c6cb1130e577b125fa4 | |
| parent | 75d615d6f6d8fd8e931689106dfa22e4a6e662a2 (diff) | |
| parent | df66e8d4a197443c214e918a858208ecc38e9f1c (diff) | |
| download | rust-fb5f020cc2d1d0972d2726413d1ec053990675ad.tar.gz rust-fb5f020cc2d1d0972d2726413d1ec053990675ad.zip | |
auto merge of #5611 : Kimundi/rust/incoming, r=catamorphism
| -rw-r--r-- | src/libcore/str.rs | 17 |
1 files changed, 15 insertions, 2 deletions
diff --git a/src/libcore/str.rs b/src/libcore/str.rs index c54a1048c46..67fd37996cd 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -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...... @@ -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}; @@ -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); + } + } |
