core: add char_at_reverse

This commit is contained in:
Erick Tryzelaar 2013-03-14 21:32:11 -10:00
parent fe74a1c9a2
commit 4b0f29a466

View File

@ -1769,7 +1769,7 @@ pub pure fn char_range_at(s: &str, i: uint) -> CharRange {
return CharRange {ch: val as char, next: i};
}
/// Pluck a character out of a string
/// Plucks the `n`th character from the beginning of a string
pub pure fn char_at(s: &str, i: uint) -> char {
return char_range_at(s, i).ch;
}
@ -1799,6 +1799,11 @@ pure fn char_range_at_reverse(ss: &str, start: uint) -> CharRange {
return CharRange {ch:ch, next:prev};
}
/// Plucks the `n`th character from the end of a string
pub pure fn char_at_reverse(s: &str, i: uint) -> char {
char_range_at_reverse(s, i).ch
}
/**
* Loop through a substring, char by char
*
@ -2274,6 +2279,7 @@ pub trait StrSlice {
pure fn to_owned(&self) -> ~str;
pure fn to_managed(&self) -> @str;
pure fn char_at(&self, i: uint) -> char;
pure fn char_at_reverse(&self, i: uint) -> char;
fn to_bytes(&self) -> ~[u8];
}
@ -2419,6 +2425,11 @@ impl StrSlice for &'self str {
#[inline]
pure fn char_at(&self, i: uint) -> char { char_at(*self, i) }
#[inline]
pure fn char_at_reverse(&self, i: uint) -> char {
char_at_reverse(*self, i)
}
fn to_bytes(&self) -> ~[u8] { to_bytes(*self) }
}
@ -3426,6 +3437,28 @@ mod tests {
}
}
#[test]
fn test_char_at() {
let s = ~"ศไทย中华Việt Nam";
let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
let mut pos = 0;
for v.each |ch| {
fail_unless!(s.char_at(pos) == *ch);
pos += from_char(*ch).len();
}
}
#[test]
fn test_char_at_reverse() {
let s = ~"ศไทย中华Việt Nam";
let v = ~['ศ','ไ','ท','ย','中','华','V','i','ệ','t',' ','N','a','m'];
let mut pos = s.len();
for v.each_reverse |ch| {
fail_unless!(s.char_at_reverse(pos) == *ch);
pos -= from_char(*ch).len();
}
}
#[test]
fn test_each_char() {
let s = ~"abc";