提交 4b0f29a4 编写于 作者: E Erick Tryzelaar

core: add char_at_reverse

上级 fe74a1c9
......@@ -1769,7 +1769,7 @@ impl Equiv<~str> for &'self str {
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 @@ pub struct 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 @@ fn test_utf16() {
}
}
#[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";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册