@@ -1769,7 +1769,7 @@ pub pure fn char_range_at(s: &str, i: uint) -> CharRange {
1769
1769
return CharRange { ch : val as char , next : i} ;
1770
1770
}
1771
1771
1772
- /// Pluck a character out of a string
1772
+ /// Plucks the `n`th character from the beginning of a string
1773
1773
pub pure fn char_at ( s : & str , i : uint ) -> char {
1774
1774
return char_range_at ( s, i) . ch ;
1775
1775
}
@@ -1799,6 +1799,11 @@ pure fn char_range_at_reverse(ss: &str, start: uint) -> CharRange {
1799
1799
return CharRange { ch : ch, next : prev} ;
1800
1800
}
1801
1801
1802
+ /// Plucks the `n`th character from the end of a string
1803
+ pub pure fn char_at_reverse ( s : & str , i : uint ) -> char {
1804
+ char_range_at_reverse ( s, i) . ch
1805
+ }
1806
+
1802
1807
/**
1803
1808
* Loop through a substring, char by char
1804
1809
*
@@ -2274,6 +2279,7 @@ pub trait StrSlice {
2274
2279
pure fn to_owned(&self) -> ~str;
2275
2280
pure fn to_managed(&self) -> @str;
2276
2281
pure fn char_at(&self, i: uint) -> char;
2282
+ pure fn char_at_reverse(&self, i: uint) -> char;
2277
2283
fn to_bytes(&self) -> ~[u8];
2278
2284
}
2279
2285
@@ -2419,6 +2425,11 @@ impl StrSlice for &'self str {
2419
2425
#[inline]
2420
2426
pure fn char_at(&self, i: uint) -> char { char_at(*self, i) }
2421
2427
2428
+ #[inline]
2429
+ pure fn char_at_reverse(&self, i: uint) -> char {
2430
+ char_at_reverse(*self, i)
2431
+ }
2432
+
2422
2433
fn to_bytes(&self) -> ~[u8] { to_bytes(*self) }
2423
2434
}
2424
2435
@@ -3426,6 +3437,28 @@ mod tests {
3426
3437
}
3427
3438
}
3428
3439
3440
+ #[ test]
3441
+ fn test_char_at( ) {
3442
+ let s = ~"ศไทย中华Việt Nam ";
3443
+ let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
3444
+ let mut pos = 0;
3445
+ for v.each |ch| {
3446
+ fail_unless!(s.char_at(pos) == *ch);
3447
+ pos += from_char(*ch).len();
3448
+ }
3449
+ }
3450
+
3451
+ #[test]
3452
+ fn test_char_at_reverse() {
3453
+ let s = ~" ศไทย中华Việt Nam ";
3454
+ let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
3455
+ let mut pos = s.len();
3456
+ for v.each_reverse |ch| {
3457
+ fail_unless!(s.char_at_reverse(pos) == *ch);
3458
+ pos -= from_char(*ch).len();
3459
+ }
3460
+ }
3461
+
3429
3462
#[test]
3430
3463
fn test_each_char() {
3431
3464
let s = ~" abc";
0 commit comments