File tree 1 file changed +15
-2
lines changed
1 file changed +15
-2
lines changed Original file line number Diff line number Diff line change @@ -1865,8 +1865,10 @@ pub struct CharRange {
1865
1865
* Given a byte position and a str, return the previous char and its position
1866
1866
*
1867
1867
* This function can be used to iterate over a unicode string in reverse.
1868
+ *
1869
+ * returns 0 for next index if called on start index 0
1868
1870
*/
1869
- fn char_range_at_reverse ( ss : & str , start : uint ) -> CharRange {
1871
+ pub fn char_range_at_reverse ( ss : & str , start : uint ) -> CharRange {
1870
1872
let mut prev = start;
1871
1873
1872
1874
// while there is a previous byte == 10......
@@ -1875,7 +1877,12 @@ fn char_range_at_reverse(ss: &str, start: uint) -> CharRange {
1875
1877
}
1876
1878
1877
1879
// now refer to the initial byte of previous char
1878
- prev -= 1 u;
1880
+ if prev > 0 u {
1881
+ prev -= 1 u;
1882
+ } else {
1883
+ prev = 0 u;
1884
+ }
1885
+
1879
1886
1880
1887
let ch = char_at ( ss, prev) ;
1881
1888
return CharRange { ch : ch, next : prev} ;
@@ -3761,4 +3768,10 @@ mod tests {
3761
3768
" 12345555 ".cmp(& &" 123456 ") == Less;
3762
3769
" 22 ".cmp(& &" 1234 ") == Greater;
3763
3770
}
3771
+
3772
+ #[test]
3773
+ fn test_char_range_at_reverse_underflow() {
3774
+ assert!(char_range_at_reverse(" abc" , 0 ) . next == 0 ) ;
3775
+ }
3776
+
3764
3777
}
You can’t perform that action at this time.
0 commit comments